在tkinter离开messagebox后按下按钮

时间:2014-07-13 08:56:18

标签: python tkinter

在对按钮DROP的绑定中,我有:

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)

但是在OK按下messagebox.showinfo后按下了按钮...

image 1 image 2


完整的测试代码

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

如何按下按钮

1 个答案:

答案 0 :(得分:3)

要达到预期效果,请使用<ButtonRelease-1>事件,而不是<Button-1>事件:

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

此外,您的returnpass语句是多余的,这就足够了:

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