简体   繁体   中英

Pressed button after leaving messagebox in tkinter

In a binding to the button DROP function I have:

def on_click_button_drop(element):

    # ...

    if not db_name:
        print("db name empty")
        messagebox.showinfo("", "db name empty")
        return
# ...
button_drop.bind("<Button-1>", on_click_button_drop)

But after pressing OK in messagebox.showinfo the button left pressed...

image 1 image 2


Full test code

from tkinter import *
import tkinter.messagebox as messagebox

root = Tk()
root.grid()


def on_click_button_drop(element):
    db_name = entry_db_name.get()
    if not db_name:
        # print("db name empty")
        messagebox.showinfo("", "db name empty")
        return
    pass

label_db_name = Label(root, text="db name")
entry_db_name = Entry(root, width=20)
button_drop = Button(root, text="DROP", width=10)

label_db_name.pack()
entry_db_name.pack()
button_drop.pack()

button_drop.bind("<Button-1>", on_click_button_drop)

root.mainloop()

What to do to depress the button

To achieve the desired effect use the <ButtonRelease-1> event, instead of <Button-1> event:

button_drop.bind("<ButtonRelease-1>", on_click_button_drop)

Additionally, your return and pass statements are redundant, this is enough:

def on_click_button_drop(element):
    db_name = entry_db_name.get()
    if not db_name:
        # print("db name empty")
        messagebox.showinfo("", "db name empty")

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