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)小部件。 –