Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用Tkinter中相同按钮的启动和停止功能_Python_Python 3.x_Tkinter - Fatal编程技术网

Python 使用Tkinter中相同按钮的启动和停止功能

Python 使用Tkinter中相同按钮的启动和停止功能,python,python-3.x,tkinter,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() roo

在命令按钮的帮助下,我能够断开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()

建议将不胜感激

此代码过于复杂。我的答案是,我建议改进它。但它展示了如何使用相同的按钮来启动和停止,并保留大部分代码

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()

您可以简单地使用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()

你说的开始到底是什么意思?如果简化示例,比如删除串行依赖项,您的示例会更好。谢谢你的文件名是什么Hi,我已经改进了我的代码Hi Mike,谢谢你的回复,代码正在使用上面的示例,但是如果我有一个数组,它的值从串行端口接收,比如计数器=['22.34','22.35','26.66',],我想显示和更新标签文本,该怎么办。例如label1=label.configtext=counter[0],label2=label.configtext=counter[1],label3=label.configtext=counter[2]诸如此类的东西。@varuljain我确实看不出你有什么理由不能这样做。