Python Tkinter按钮保持按下状态
我正在制作一个使用button控件的代码tkinter,但当我按下按钮时,按钮会一直按下,直到按下按钮的功能未完成。我想这样做时,按钮将被按下,按钮将立即释放,然后做它的功能,它应该不会卡住。 下面是一段代码,它显示了发生这种情况的一个很好的示例:Python Tkinter按钮保持按下状态,python,python-3.x,button,tkinter,Python,Python 3.x,Button,Tkinter,我正在制作一个使用button控件的代码tkinter,但当我按下按钮时,按钮会一直按下,直到按下按钮的功能未完成。我想这样做时,按钮将被按下,按钮将立即释放,然后做它的功能,它应该不会卡住。 下面是一段代码,它显示了发生这种情况的一个很好的示例: from tkinter import * import time root = Tk() root.geometry('100x100+100+100') # size/position of root def callback(): # this
from tkinter import *
import time
root = Tk()
root.geometry('100x100+100+100') # size/position of root
def callback(): # this function will run on button press
print('Firing in 3')
time.sleep(3) # wait for 3 seconds
def main(): #function 'main'
b = Button(root, text="ᖴIᖇE", width=10,height=2, command=callback)# setting the button
b["background"] = 'red' #button color will be red
b["activebackground"] = 'yellow' #button color will be yellow for the time when the button will not be released
b.place(x=25,y=25) #placing the button
main() # using function 'main'
mainloop()
GUI程序通常在单个线程中驱动,该线程由正在使用的图形工具包的“主循环”控制。也就是说:一个程序通常会设置应用程序,并将控制权传递给工具箱,工具箱会运行一个紧密的循环,以回答所有用户(以及网络、文件等)事件,唯一再次运行的用户代码是在设置阶段编写的回调代码 同时,当您的代码在回调期间运行时,它会保留控件,这意味着当您的函数不返回时,工具箱将无法响应任何事件 必须做的是编写与GUI工具包协作的代码——也就是说,如果您需要时间间隔,创建生成进一步回调的事件。在tkinter的情况下,这是通过小部件的方法
.after
实现的:在这许多毫秒之后,将运行可调用的passed<另一方面,code>time.sleep会在那里停止单个线程,并且事件循环不会运行
在您的示例中,您可以简单地编写:
from tkinter import *
import time
root = Tk()
root.geometry('100x100+100+100') # size/position of root
def callback(): # this function will run on button press
print('Firing in 3')
root.after(3000, realcallback)
def realcallback():
print('Firing now!')
def main(): #function 'main'
b = Button(root, text="ᖴIᖇE", width=10,height=2, command=callback)# setting the button
b["background"] = 'red' #button color will be red
b["activebackground"] = 'yellow' #button color will be yellow for the time when the button will not be released
b.place(x=25,y=25) #placing the button
main() # using function 'main'
mainloop()
GUI程序通常在单个线程中驱动,该线程由正在使用的图形工具包的“主循环”控制。也就是说:一个程序通常会设置应用程序,并将控制权传递给工具箱,工具箱会运行一个紧密的循环,以回答所有用户(以及网络、文件等)事件,唯一再次运行的用户代码是在设置阶段编写的回调代码 同时,当您的代码在回调期间运行时,它会保留控件,这意味着当您的函数不返回时,工具箱将无法响应任何事件 必须做的是编写与GUI工具包协作的代码——也就是说,如果您需要时间间隔,创建生成进一步回调的事件。在tkinter的情况下,这是通过小部件的方法
.after
实现的:在这许多毫秒之后,将运行可调用的passed<另一方面,code>time.sleep会在那里停止单个线程,并且事件循环不会运行
在您的示例中,您可以简单地编写:
from tkinter import *
import time
root = Tk()
root.geometry('100x100+100+100') # size/position of root
def callback(): # this function will run on button press
print('Firing in 3')
root.after(3000, realcallback)
def realcallback():
print('Firing now!')
def main(): #function 'main'
b = Button(root, text="ᖴIᖇE", width=10,height=2, command=callback)# setting the button
b["background"] = 'red' #button color will be red
b["activebackground"] = 'yellow' #button color will be yellow for the time when the button will not be released
b.place(x=25,y=25) #placing the button
main() # using function 'main'
mainloop()
我想补充一点:
from tkinter import *
import time
root = Tk()
root.geometry('100x100+100+100') # size/position of root
def callback(): # this function will run on button press
root.update() #<- this works for me..
print('Firing in 3')
root.after(3000, realcallback)
def realcallback():
print('Firing now!')
def main(): #function 'main'
b = Button(root, text="ᖴIᖇE", width=10,height=2, command=callback)# setting the button
b["background"] = 'red' #button color will be red
b["activebackground"] = 'yellow' #button color will be yellow for the time when the button will not be released
b.place(x=25,y=25) #placing the button
main() # using function 'main'
mainloop()
从tkinter导入*
导入时间
root=Tk()
根部几何结构(“100x100+100+100”)#根部尺寸/位置
def callback():#此函数将在按下按钮时运行
root.update()#我想添加:
from tkinter import *
import time
root = Tk()
root.geometry('100x100+100+100') # size/position of root
def callback(): # this function will run on button press
root.update() #<- this works for me..
print('Firing in 3')
root.after(3000, realcallback)
def realcallback():
print('Firing now!')
def main(): #function 'main'
b = Button(root, text="ᖴIᖇE", width=10,height=2, command=callback)# setting the button
b["background"] = 'red' #button color will be red
b["activebackground"] = 'yellow' #button color will be yellow for the time when the button will not be released
b.place(x=25,y=25) #placing the button
main() # using function 'main'
mainloop()
从tkinter导入*
导入时间
root=Tk()
根部几何结构(“100x100+100+100”)#根部尺寸/位置
def callback():#此函数将在按下按钮时运行
root.update()#您应该使用线程。按钮被卡住取决于代码。如果您确实有睡眠,则应使用多线程或之后的。如果实际进程需要那么长的时间,您可以使用更新方法或多线程。嗯,您让它休眠,所以它正在休眠。它无法在睡眠时更新显示。多线程不是答案。tkinter是单线程的,一次只能做一件事。循环运行时,无法更新显示。在stackoverflow上有很多问题。你应该使用线程。按钮被卡住取决于代码。如果您确实有睡眠,则应使用多线程或之后的。如果实际进程需要那么长的时间,您可以使用更新方法或多线程。嗯,您让它休眠,所以它正在休眠。它无法在睡眠时更新显示。多线程不是答案。tkinter是单线程的,一次只能做一件事。循环运行时,无法更新显示。关于stackoverflow有很多问题。当我在回调函数中使用需要时间的循环时,会发生什么?如果你的循环需要时间来计算东西,那么你可以求助于线程。但是,如果“时间”是为游戏对象设置动画,情况似乎是这样的:每个游戏对象都应该“立即”在某个位置绘制,并安排一个.after
回调在其下一个位置渲染。为游戏对象使用类使这变得非常简单。当我在回调函数中使用需要时间的循环时会发生什么?如果你的循环需要时间来计算东西,那么你可以求助于线程。但是,如果“时间”是为游戏对象设置动画,情况似乎是这样的:每个游戏对象都应该“立即”在某个位置绘制,并安排一个.after
回调在其下一个位置渲染。为游戏对象使用类使之变得非常简单。感谢您的第一次贡献!不过,你的答案需要稍加润色。我建议尝试在文本部分解释代码示例正在做什么以及为什么。另外,我怀疑你的代码所做的是OP所追求的:我认为OP正在寻找一种在不阻塞tkinter的UI线程的情况下运行更长计算的方法。感谢你的第一次贡献!不过,你的答案需要稍加润色。我建议尝试在文本部分解释代码示例正在做什么以及为什么。另外,我怀疑您的代码所做的是OP所追求的:我认为OP正在寻找一种在不阻塞tkinter的UI线程的情况下运行更长计算的方法。