【问题标题】:Python: tkinter: creating a custom window promptPython:tkinter:创建自定义窗口提示
【发布时间】: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


【解决方案1】:

您将不得不以不同的方式构建它。

您可以创建一个具有self.result 的类,并且它必须使用wait_window(),因此它会等到您关闭窗口。然后你可以从self.result获得价值

你可以在你的choice_message中使用这个类:

import tkinter as tk

# --- classes ---
# you can put this class in separated file
# (it will need `import tkinter`)

import tkinter 

class MsgBox(tkinter.Toplevel):

    def __init__(self, title="MsgBox", message="Hello World", choice1="OK", choice2="Cancel"):
        tkinter.Toplevel.__init__(self)

        self.result = None

        self.choice1 = choice1
        self.choice2 = choice2

        self.title(title)

        self.label = tkinter.Label(self, text=message, bg='white')
        self.label.pack(ipadx=50, ipady=20)

        self.button = tkinter.Button(self, text=str(self.choice1), command=self.on_press_choice1)
        self.button.pack(side='left', ipadx=5, padx=10, pady=10)

        self.button = tkinter.Button(self, text=str(self.choice2), command=self.on_press_choice2)
        self.button.pack(side='right', ipadx=5, padx=10, pady=10)

        # don't return to main part till you close this MsgBox
        self.wait_window()

    def on_press_choice1(self):
        self.result = self.choice1
        self.destroy()

    def on_press_choice2(self):
        self.result = self.choice2
        self.destroy()

# --- functions ---

def choice_message(title, message, choice1, choice2):

    msg = MsgBox(title, message, choice1, choice2)
    # MsgBox is waiting till you close it
    return msg.result

def choose():
    if choice_message("Hello", "This is a message text", True, False) == True:
        print("You choose: True")
    else:
        print("You choose: False")

def about():
    msg = MsgBox()
    # MsgBox is waiting till you close window
    print("You choose:", msg.result)

def close():
    msg = MsgBox('Closing', 'Are you sure?', 'Yes', 'No')
    # MsgBox is waiting till you close window
    if msg.result == 'Yes':
        root.destroy()

# --- main ---

root = tk.Tk()

b = tk.Button(root, text="Choose", command=choose)
b.pack(fill='x', expand=True)

b = tk.Button(root, text="About", command=about)
b.pack(fill='x', expand=True)

b = tk.Button(root, text="Close", command=close)
b.pack(fill='x', expand=True)

root.mainloop()

主消息框:

选择:

关于:

关闭:

【讨论】:

  • 当提示打开时,您仍然可以与主窗口进行交互。用户可以打开 12 个“关于”窗口的副本
猜你喜欢
  • 2011-06-16
  • 1970-01-01
  • 1970-01-01
  • 2011-09-03
  • 1970-01-01
  • 2011-10-29
  • 1970-01-01
  • 2021-12-15
  • 1970-01-01
相关资源
最近更新 更多