我有一个简单的例子:
from tkinter import *
root = Tk()
display = Label(root,text="Starting")
display.pack()
def change(): #Do on first press of button
global display
display.config(text="I just changed")
def change1(): #Do on second press of button
global display
display.config(text="I changed again")
def change2(): #Do on third press of button
global display
display.config(text="I changed once more")
button = Button(root,text="Press me",command=change)
button.pack()
很明显,在第一次按下按钮时,它会把标签上写着“开始”改为“我刚换过”,但我如何让它在连续按下同一个按钮时执行其他两个功能(change1和change2)?在
您可以设置一个计数器来确定要将标签文本更改为什么,然后当计数器达到最大值时重置它以处理进一步的按钮按下。实际上不需要
global
来更改这里的标签。在为此,您必须定义一个变量来跟踪按钮按下的时间。 您还必须创建一个更新函数来增加变量并测试变量的状态。在
例如:
相关问题 更多 >
编程相关推荐