使用wait_window()后tkinter按钮动画卡住

时间:2017-11-25 07:25:04

标签: 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 = 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()

每次按下按钮eventaddperson被触发,退出该功能时主窗口的按钮动画卡在按下状态,我正在寻找一种方法来刷新gui或者如果我正在做什么如何解决问题呢?

2 个答案:

答案 0 :(得分:1)

如果我使用command=代替bind(),那么问题就会消失

顺便说一句:如果您使用command=,那么def evntshow()必须没有event

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

答案 1 :(得分:0)

在使用bind()时我遇到了一些滞后的按钮动画,切换到命令=让它看起来好多了!

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

我正在使用python 3.6和windows 10

相关问题