在对按钮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
后按下了按钮...
完整的测试代码
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()
如何按下按钮
答案 0 :(得分:3)
要达到预期效果,请使用<ButtonRelease-1>
事件,而不是<Button-1>
事件:
button_drop.bind("<ButtonRelease-1>", on_click_button_drop)
此外,您的return
和pass
语句是多余的,这就足够了:
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")