Python tkinter按钮动画在使用wait_window()后卡住
这是一个对话框窗体类: **更新显示问题的完整可行源代码Python tkinter按钮动画在使用wait_window()后卡住,python,user-interface,events,tkinter,dialog,Python,User Interface,Events,Tkinter,Dialog,这是一个对话框窗体类: **更新显示问题的完整可行源代码 from tkinter import * class SGForm: created_form = False def __init__(self, root, title=""): self.form = Toplevel(root) self.form.wm_title(title) self.input = dict() self.var = Stri
from tkinter import *
class SGForm:
created_form = False
def __init__(self, root, title=""):
self.form = Toplevel(root)
self.form.wm_title(title)
self.input = dict()
self.var = StringVar()
SGForm.created_form = True
def getform(self):
return self.form
def addinput(self, name, text ,var = None):
p = Frame(self.form)
p.pack(side="top", fill="both", expand=True, padx=10, pady=10)
l = Label(p, text=text)
l.pack(side="left", fill="both", expand=True, padx=10, pady=10)
self.input[name] = Entry(p, textvariable=var)
self.input[name].pack(side="left", fill="both", expand=True, padx=10, pady=10)
def addbutton(self, text, signal, func):
p = Frame(self.form)
p.pack(side="top", fill="both", expand=True, padx=10, pady=10)
b = Button(p, text=text)
b.pack(side="left", fill="both", expand=True, padx=10, pady=10)
b.bind(signal, func)
def showandreturn(self):
value = dict()
value['firstname'] = self.var.get()
SGForm.created_form = False
return value
def closeform(self, event):
self.form.destroy()
def customform(self):
self.addinput('entfirstname', 'frist name', self.var)
self.addbutton('close','<Button-1>', self.closeform)
#example calling dialog class
root = Tk()
def evntshow(event):
form = SGForm(root)
form.customform()
root.wait_window(form.getform())
test = form.showandreturn()
print(test)
button = Button(root, text='show')
button.pack()
button.bind('<Button-1>', evntshow)
root.mainloop()
从tkinter导入*
类别表格:
created\u form=False
def uuu init uuuu(self,root,title=”“):
self.form=顶级(根)
self.form.wm_title(title)
self.input=dict()
self.var=StringVar()
SGForm.created_form=True
def getform(self):
返回自我表格
def addinput(self、name、text、var=None):
p=帧(self.form)
p、 封装(side=“top”、fill=“both”、expand=True、padx=10、pady=10)
l=标签(p,文本=文本)
l、 封装(side=“left”、fill=“both”、expand=True、padx=10、pady=10)
self.input[name]=Entry(p,textvariable=var)
self.input[name].pack(side=“left”,fill=“both”,expand=True,padx=10,pady=10)
def添加按钮(自身、文本、信号、功能):
p=帧(self.form)
p、 封装(side=“top”、fill=“both”、expand=True、padx=10、pady=10)
b=按钮(p,文本=文本)
b、 封装(side=“left”、fill=“both”、expand=True、padx=10、pady=10)
b、 绑定(信号,函数)
def显示和返回(自身):
value=dict()
值['firstname']=self.var.get()
SGForm.created_form=False
返回值
def关闭窗体(自身、事件):
self.form.destroy()
def自定义表单(自我):
self.addinput('entfirstname','first name',self.var)
self.addbutton('close','',self.closeform)
#调用对话框类的示例
root=Tk()
def evntshow(事件):
form=SGForm(根)
form.customform()
root.wait\u窗口(form.getform())
test=form.showandreturn()
打印(测试)
按钮=按钮(根,text='show')
button.pack()
按钮绑定(“”,evntshow)
root.mainloop()
每次按下按钮
eventaddperson
get trigger,当退出功能时,主窗口的按钮动画会停留在按下状态,我正在寻找刷新gui的方法,或者如果我做错了什么,如何修复它?如果我使用命令=
而不是bind()
然后问题就消失了
顺便说一句:如果使用命令=
,则def evntshow()
必须没有事件
def evntshow(): # <--- without event
form = SGForm(root)
form.customform()
root.wait_window(form.getform())
test = form.showandreturn()
print(test)
# use `command=` instead of `bind('<Button-1>',...)
button = Button(root, text='show', command=evntshow)
button.pack()
def evntshow():#在使用bind()时,我也体验到了类似于laggy按钮的动画,切换到command=使它看起来好多了
from tkinter import *
import time
def func1():
print('waiting for 1 second...')
time.sleep(1)
def func2(event):
print('waiting for 1 second...')
time.sleep(1)
root = Tk()
# button animation runs smoothly
Button1 = Button(root, text="button with command=", command=func1)
Button1.pack()
Button2 = Button(root, text="button with bind()") # button animation does not occur
Button2.bind('<Button-1>', func2)
Button2.pack()
root.mainloop()
从tkinter导入*
导入时间
def func1():
打印('等待1秒…')
时间。睡眠(1)
def func2(事件):
打印('等待1秒…')
时间。睡眠(1)
root=Tk()
#按钮动画运行平稳
Button1=按钮(根,text=“Button with command=”,command=func1)
按钮1.pack()
Button2=按钮(root,text=“Button with bind()”)按钮动画不发生
按钮2.绑定(“”,功能2)
按钮2.pack()
root.mainloop()
我正在使用python 3.6和windows 10在wait\u window()
之前是否尝试过update()
主窗口wait_window()
仅对窗体运行mainloop,因此mainloop无法更改主窗口中的按钮update()
应该强制mainloop更改主窗口中的按钮。我只是尝试了一下,但没有成功。您必须创建,这样我们才能运行它-也许您在创建示例时会发现问题。顺便说一句:Button
有command=
来分配函数-按钮(…,command=evntshow)
-您不必使用bind(“”,evntshow)
请提供一个工作示例代码作为说明。@Sphinx Ty!请运行此代码段以了解我的意思