【问题标题】:Tkinter Label text doesn't change every timeTkinter 标签文本不会每次都改变
【发布时间】:2018-04-15 21:32:42
【问题描述】:

Python 3.6。 这是我为 tkinter 制作的一个类,一旦我按下开始,self.compteur 的文本就会每秒更改一次:

motsCount = 0
temps_total = 3600

class ChronoAspi:
    def __init__(self, master):
        self.master = master
        self.mainframe = tkinter.Frame(self.master, background ='#28aae1')
        self.mainframe.pack(fill = tkinter.BOTH, expand= True)
        self.timer_text = tkinter.StringVar()
        self.timer_text.trace('w', self.build_timer)
        self.time_left = tkinter.IntVar()
        self.time_left.set(temps_total)
        self.running = True
        self.buildGrid()
        self.build_buttons()
        self.build_timer()
        self.build_compteur()
        self.update()

    def buildGrid(self):
        self.mainframe.columnconfigure(0, weight=1)
        self.mainframe.rowconfigure(0, weight=1)
        self.mainframe.rowconfigure(1, weight=1)
        self.mainframe.rowconfigure(2, weight=0)

    def build_buttons(self):
        buttons_frame = tkinter.Frame(self.mainframe)
        buttons_frame.grid(row=2, column=0, sticky='nsew', padx=10, pady=10)
        buttons_frame.columnconfigure(0, weight=1)
        buttons_frame.columnconfigure(1, weight=1)
        self.start_button = tkinter.Button(buttons_frame, text='Start', command=self.start_timer )
        self.stop_button = tkinter.Button(buttons_frame, text='Stop', command=self.stop_timer)
        self.start_button.grid(row=0, column=0, sticky = 'ew')
        self.stop_button.grid(row=0, column=1, sticky = 'ew')
        self.stop_button.config(state=tkinter.DISABLED)

    def build_timer(self, *args):
        timer = tkinter.Label(self.mainframe, text=self.timer_text.get(), background = '#28aae1', fg='white', font=("Helvetica", 30))
        timer.grid(row=1, column=0)

    def build_compteur(self, *args):
        self.compteur = tkinter.Label(self.mainframe, text='Aucun mot compté.', background = '#28aae1', fg='white', font=("Helvetica", 20))
        self.compteur.grid(row=0, column=0)

    def start_timer(self):
        self.time_left.set(temps_total)
        self.running = True
        self.stop_button.config(state=tkinter.NORMAL)
        self.start_button.config(state=tkinter.DISABLED)

    def stop_timer(self):
        self.running = False
        self.stop_button.config(state=tkinter.DISABLED)
        self.start_button.config(state=tkinter.NORMAL)

    def heures_minutes_secondes(self, seconds):
        return int(seconds/3600), int(seconds%3600/60), int(seconds%60)

    def update(self):
        global motsCount
        time_left = self.time_left.get()

        if self.running and time_left:
            heure, minutes, seconds = self.heures_minutes_secondes(time_left)
            self.timer_text.set('{:0>2}:{:0>2}:{:0>2}'.format(heure ,minutes, seconds) )
            self.time_left.set(time_left-1)
            motsCount += 1
        else:
            heure, minutes, seconds = self.heures_minutes_secondes(time_left)
            self.timer_text.set( '{:0>2}:{:0>2}:{:0>2}'.format(heure ,minutes, seconds))
            self.compteur['text'] = 'Compteur stoppé.'
            self.stop_timer()

        if motsCount > 0:
            self.compteur['text'] = str(motsCount) + ' mots comptés.'

        self.master.after(1000, self.update)

if __name__ == '__main__':
    root = tkinter.Tk()
    ChronoAspi(root)
    root.mainloop()

只要计时器在运行,self.compteur的文本就会每秒变化一次。但是当我点击self.stop_button 时,self.running 变为False 并且update() 函数中的else 部分被执行。所以计时器停止了,但 self.compteur 文字没有改变,我不知道为什么!

【问题讨论】:

  • 我尝试运行您的代码并在self.master.after 行中获得了IndentationError。我尝试缩进该行,并在motsCount += 1 行上得到UnboundLocalError。请发布实际演示您的问题的代码。
  • @Kevin 抱歉,我改了,帖子的复制粘贴错误。顺便说一句,问题解决了,谢谢!

标签: python python-3.x tkinter


【解决方案1】:

抱歉,我无法发表评论,但我认为您的 if 语句将在 self.stopTimer 返回后运行并将文本改回

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多