简体   繁体   中英

tkinter button animation stuck after using wait_window()

This is a dialog form class :

** update full workable source code showing the problem

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

Each time the button get pressed eventaddperson get triggered, when exiting the function the button animation of the main window get stuck on press status, I am looking for a way to refresh the gui or what if I am doing something wrong how to fix it?

If I use command= instead of bind() then problem disappers

BTW: if you use command= then def evntshow() has to be without 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()

I was experiencing kind of laggy button animations when using bind() as well, switching to command= made it a look a lot better!

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

I am working with python 3.6 and windows 10

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM