在Tkinter中使用同一按钮启动和停止功能

时间:2019-01-31 13:04:55

标签: python python-3.x tkinter

借助命令按钮,我可以在Tkinter中断开框架。但是有什么方法可以帮助您使用同一按钮来启动吗?

import tkinter as tk
counter = 0
def counter_label(label):
  def count():
    global counter
    counter+=1
    label.config(text=counter)
    label.after(1000, count)
  count()


root = tk.Tk()
root.title("Counting Seconds")
label = tk.Label(root, fg="green")
label.pack()
counter_label(label)
button = tk.Button(root, text='Stop', width=25, command=root.destroy)
button.pack()
root.mainloop()

建议将不胜感激

2 个答案:

答案 0 :(得分:1)

您可以简单地使用if / else语句来检查按钮文本是Start还是Stop,然后更改用于控制计数器的布尔变量,同时还更新按钮上的文本。

import tkinter as tk

counter = 0
active_counter = False


def count():
    if active_counter:
        global counter
        counter += 1
        label.config(text=counter)
        label.after(1000, count)


def start_stop():
    global active_counter
    if button['text'] == 'Start':
        active_counter = True
        count()
        button.config(text="Stop")
    else:
        active_counter = False
        button.config(text="Start")


root = tk.Tk()
root.title("Counting Seconds")
label = tk.Label(root, fg="green")
label.pack()
button = tk.Button(root, text='Start', width=25, command=start_stop)
button.pack()
root.mainloop()

这也是一个OOP示例:

import tkinter as tk


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("Counting Seconds")
        self.counter = 0
        self.active_counter = False
        self.label = tk.Label(self, fg="green")
        self.label.pack()
        self.button = tk.Button(self, text='Start', width=25, command=self.start_stop)
        self.button.pack()

    def count(self):
        if self.active_counter:
            self.counter += 1
            self.label.config(text=self.counter)
            self.label.after(1000, self.count)

    def start_stop(self):
        if self.button['text'] == 'Start':
            self.active_counter = True
            self.count()
            self.button.config(text="Stop")
        else:
            self.active_counter = False
            self.button.config(text="Start")


if __name__ == "__main__":
    App().mainloop()

答案 1 :(得分:0)

此代码过于复杂(我的回答),我建议对其进行改进。但是它显示了如何在启动和停止以及保留大多数代码的同时使用相同的按钮。

import tkinter as tk

def counter_label(label):
    a = 0
    label.config(text=str(a))
    def count():
        nonlocal a
        label.config(text=str(a))
        a += 1
        label.after(1000, count)
    return count

def start_stop(root, btn_text, counter):
    first = True
    def call():
        nonlocal first
        if first:
            counter()
            first = False
            btn_text.set('Stop')
        else:
            root.destroy()
    return call

root = tk.Tk()
root.title("Counting Seconds")
label = tk.Label(root, fg="green")
label.pack()
counter = counter_label(label)
btn_text = tk.StringVar()
button = tk.Button(root, textvariable=btn_text, width=25, command=start_stop(root, btn_text, counter))
btn_text.set('Start')
button.pack()
root.mainloop()