需要帮助忽略按钮按下。Tkinter

3
我正在制作一个秒表程序。如果计时器已经在运行,我无法让开始按钮不起作用。
当我搜索时,我看到相同的14年前的代码。我很难相信在过去的14年中所有这些人都独立地得出了相同的解决方案。
作为一个初学者,我真的很想知道我写的有什么问题,而不是复制/粘贴并继续。
from tkinter import *
import time
import datetime


class Window(Frame):

    def __init__(self, master = None):
        Frame.__init__(self, master)

        self.master = master

        self.init_window()

    def init_window(self):

        self.master.title('Stopwatch')

        self.pack(fill=BOTH, expand=1)

        quit_button = Button(self, text = 'Quit', command = self.client_exit)
        quit_button.config(width = 9)
        quit_button.place(x=230)

        start_button = Button(self, text = 'Start', command = self.timer_start)
        start_button.config(width= 10)
        start_button.place(x=0, y=0)

        stop_button = Button(self, text = 'Stop', command = self.timer_stop)
        stop_button.config(width = 10)
        stop_button.place(x=80)

        reset_button = Button(self, text = 'Reset', command = self.timer_reset)
        reset_button.config(width = 10)
        reset_button.place(x=160)

        self.is_timer_running = False

    def client_exit(self):
        exit()

    def timer_start(self):
        global sec1
        sec1 = time.time()
        if self.is_timer_running == False:
            self.is_timer_running = True
            def tick():
                if self.is_timer_running == True:
                    sec = time.time()
                    sec = datetime.timedelta(seconds = sec - sec1)
                    clock['text'] = sec
                    clock.after(100, tick)
            tick()

    def timer_stop(self):
        stop_time = time.time()
        if self.is_timer_running == True:
            self.is_timer_running = False
            def tick_stop():
                stop = datetime.timedelta(seconds = stop_time - sec1)
                clock['text'] = stop
            tick_stop()

    def timer_reset(self):
        self.is_timer_running = False
        clock['text'] = '00:00:00'

所以你想暂时禁用这些按钮? - Christian Dean
1个回答

4

在按钮被点击后立即将其状态设置为禁用(确保更新状态),然后在计时器停止运行时将其恢复为正常状态。

start_button.config(state = 'disabled')
start_button.update()

# do whatever you need to do i.e run the stop watch

start_button.update()
start_button.config(state = 'normal') 

感谢 @NickBonne 的进一步解释 :)

init_window() 中添加 self.start_button = start_buttonself.stop_button = stop_button。然后,您可以使用 self.start_button.config(state="disabled")


啊,Foo!你只比我快了几秒钟 ;) - Christian Dean
1
state="enabled" 是无效的,允许的状态有 "active""normal""disabled" - Dartmouth
我认为在这个应用程序中并不真正需要"active",如果我没记错的话,它模拟了按下按钮的操作,而在这种情况下会立即重新启动秒表。 - Dartmouth
如果我将您的解决方案添加到我的启动和停止按钮命令函数中,会导致名称错误。"start_button未定义"。除非将您的解决方案添加到init_window函数中,否则名称错误将持续存在。 - Nick Bonne
@Dartmouth:设置状态不会模拟再次按下按钮并启动手表。所有状态所做的就是改变一些颜色和边框选项。它不会模拟点击。 - Bryan Oakley
1
@NickBonne 你需要在 init_window() 函数中添加 self.start_button = start_buttonself.stop_button = stop_button。然后,你可以使用 self.start_button.config(state="disabled") - Dartmouth

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接