【发布时间】:2017-05-14 18:04:18
【问题描述】:
我一直在非常努力地试图找出这个问题。在使用 tkinter 和 python 时,我可以使用什么方法来使用按钮返回函数的值?这是我当前的代码
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...")
【问题讨论】:
-
请修正代码的缩进。我认为所有这些东西都是在
choice_message函数中定义的,但目前很难说。 -
有几种方法可以做你想做的事,但使用standard dialog 会更简单。顺便说一句,如果您的代码已经使用
Tk()创建了一个根窗口,或者多次调用choice_message,您将会遇到问题,因为Tkinter 程序中应该只有一个根窗口。相反,choice_message应该创建一个TopLevel小部件。
标签: python function tkinter return window