Why start button is not performing the counter functionality

前端 未结 1 1341
被撕碎了的回忆
被撕碎了的回忆 2021-01-29 08:26
from tkinter import *
import tkinter as tk

Creating a counter

def a():    

    def counter_label(label):            
        counter=         


        
1条回答
  •  说谎
    说谎 (楼主)
    2021-01-29 08:59

    In function a() you never actually call the counter_label, so it doesn't start the count. And you need to define counter variable outside of the function so that you can use global keyword.

    Here is your code modified:

    from tkinter import *
    import tkinter as tk
    
    counter = 0                             #Defining counter so you can use it with global
    def a():    
        def counter_label(label):            
            counter=0
            def count():
                global counter
                counter += 1
                label.config(text=str(counter))
                label.after(1000,count)
            count()
    
        label=tk.Label(frame,fg="red")
        label.grid(row=0,column=1)
        counter_label(label)                #Calling the counter_label function
    
    ...
    

    0 讨论(0)
提交回复
热议问题