0
我一直在努力尝试解决这个问题。当使用tkinter和python时,我可以使用什么方法来使用按钮返回函数的值?这里是我现在的代码Python:tkinter:创建自定义窗口提示
def choice_message (message_title, message, choice_1, choice_2):
new_window = Tk()
new_window.minsize (400, 150)
new_window.maxsize (400, 150)
new_window.title (str (message_title))
Label (new_window, text = message, font = "Arial", wrap = 380, anchor = NW).place (x = 10, y = 0, width = 400, height = 100)
def yes():
new_window.destroy()
# Added code
def no():
new_window.destroy()
# Added code (same as yes but returns false (??))
Button (new_window, text = str (choice_1), font = "Arial", anchor = CENTER, command = yes).place (x = 90, y = 110, width = 100, height = 30)
Button (new_window, text = str (choice_2), font = "Arial", anchor = CENTER, command = no).place (x = 210, y = 110, width = 100, height = 30)
# ....
if (choice_message ("Hello", "This is a message text", "True", "False") == True):
print ("The Window Works...")
请修正你的代码的缩进。我_think_所有东西都是在'choice_message'函数中定义的,但目前很难说。 –
有几种方法可以做你想做的事,但使用[标准对话框]会更简单(http://effbot.org/tkinterbook/tkinter-standard-dialogs.htm)。顺便说一句,如果你的代码已经用'Tk()'创建了一个根窗口,或者多次调用'choice_message',你将会遇到问题,因为在Tkinter程序中应该只有一个根窗口。相反,'choice_message'应该创建一个['TopLevel'](http://effbot.org/tkinterbook/toplevel.htm)小部件。 –