python - 如何从 Tkinter 的弹出窗口中获取值?

标签 python python-3.x tkinter

我正在尝试创建一个包含一个 Entry 和一个 Button 的弹出窗口。我需要获取用户在该条目中输入的所有内容并将其返回到我的主窗口。我正在使用Python 3.6.0。我有以下代码(受到 this question 中的代码的启发):

from tkinter import *


class Application(Frame):    
    def __init__(self, master=None):
        super().__init__(master)

        self.server_address = None

        self.master = master
        self.master.title('Application')
        self.master.resizable(width=False, height=False)
        self.master.minsize(width=800, height=600)
        self.master.maxsize(width=800, height=600)

        self.grid()
        self.init_menubar()

    def init_menubar(self):
        menubar = Menu(self.master)
        self.master.config(menu=menubar)

        filemenu = Menu(menubar, tearoff=False)
        filemenu.add_command(label="Set remote server", command=self.call_server_config)
        filemenu.add_command(label="Set refresh delay", command=None)
        filemenu.add_separator()
        filemenu.add_command(label="Close", command=self.master.destroy)
        menubar.add_cascade(label='File', menu=filemenu)

        helpmenu = Menu(menubar, tearoff=False)
        helpmenu.add_command(label="Help", command=None)
        menubar.add_cascade(label='Help', menu=helpmenu)

    def call_server_config(self):
        popup = ServerConfig(self.master)
        self.master.wait_window(popup.top)
        print('DEBUG:', popup.value)
        self.create_widgets()


class ServerConfig(Frame):
    def __init__(self, master=None):
        super().__init__(master)

        self.top = Toplevel(master)
        self.label = Label(self.top, text="Server address")
        self.label.pack()
        self.entry = Entry(self.top)
        self.entry.pack()
        self.button = Button(self.top, text='Ok', command=self.top.destroy())

        self.value = self.entry.get()


if __name__ == '__main__':
    root = Tk()
    app = Application(master=root)
    app.mainloop()

这给了我错误:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\<...>\Python\Python36\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "D:/Documents/<...>/tk-client.py", line 45, in call_server_config
    popup = ServerConfig(self)
  File "D:/Documents/<...>/tk-client.py", line 60, in __init__
    self.button = Button(self.top, text='Ok', command=self.top.destroy())
  File "C:\Users\<...>\Python\Python36\lib\tkinter\__init__.py", line 2363, in __init__
    Widget.__init__(self, master, 'button', cnf, kw)
  File "C:\Users\<...>\Python\Python36\lib\tkinter\__init__.py", line 2293, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: bad window path name ".!application.!toplevel"

最佳答案

这段代码导致了异常:

self.button = Button(self.top, text='Ok', command=self.top.destroy())

这是因为 self.top.destroy() 在您创建按钮时首先被评估。因此,您无法将按钮放置在 self.top 上,因为它已被销毁。您需要将其更改为:

self.button = Button(self.top, text='Ok', command=self.top.destroy)

command=self.top.destroy 仅在按下按钮时执行 self.top.destroy

关于python - 如何从 Tkinter 的弹出窗口中获取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46045277/

相关文章:

python - 按一列分组并将两列应用到列表 Pandas 中

python - django:触发下载后重定向到另一个页面

python - 如何让 VSCode 自动重新加载外部 *.py 模块?

python - CrawlSpider 无法解析 Scrapy 中的多页

python-3.x - 使用 python 访问 Azure webhook 数据

python - 根据 Pandas 中另一列中的值范围聚合一列的内容

Python TkInter - 为不确定数量的图像分配标签

python - 使用 for 循环创建一定数量的输入框,但只能从最后一个输入框获取最后一个值

python-3.x - pandas 列出相同的索引

Python tkinter 无法在 crontab 上工作