创建 Tkinter 类并等待返回值 [英] Creating a Tkinter class and waiting for a return value

查看:27
本文介绍了创建 Tkinter 类并等待返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 tkinter 课程:

I have a tkinter class:

class DBCreatorWin():
    def closeWindow(self):
        tkMessageBox.showinfo("Ilmiont SQLite Database Manager", "This window cannot be closed.\nEnter a database name and press Continue.")

    def returnName(self):
        dbName = self.entry.get()
        self.window.destroy()
        return dbName

    def __init__(self):
        self.window = Toplevel()
        self.window.transient(tkRoot)
        self.window.grab_set()
        self.window.resizable(width=False, height=False)
        self.window.title("Ilmiont SQLite Database Manager")
        self.window.protocol("WM_DELETE_WINDOW", self.closeWindow)

        self.label = Label(self.window, text="Enter the name of the database to be created: ")
        self.entry = Entry(self.window, width=30)
        self.button = Button(self.window, text="Continue", command=self.returnName)
        self.label.grid(row=0, column=0)
        self.entry.grid(row=0, column=1)
        self.button.grid(row=1, column=0, columnspan=2)

我想在我的主代码中创建这个类的一个实例并等待返回值.用户在输入字段中键入名称并按下继续按钮.此时,该值应该返回到类最初实例化的位置.我该怎么做?我似乎无法让它以正常方式工作,而且我是 tkinter 的新手.

I want to create an instance of this class within my main code and wait for the return value. The user types a name into the entry field and presses the Continue button. At that point, the value should be returned to where the class was originally instantiated. How do I go about this? I can't seem to make it work in a normal way and am new to tkinter.

提前致谢,伊利诺特

推荐答案

有几种方法可以做到这一点.基本思想是使用 tkinter 方法在返回之前等待特定事件.Tkinter 提供了两种方法来做到这一点:wait_windowwait_variable.最常见的方法是打开一个窗口,然后等待它被销毁.可以在 effbot 站点上的标题为 Dialog Windows 的页面上找到一些很好的示例.

There are a couple of ways to do this. The basic idea is to use a tkinter method to wait for a specific event before returning. Tkinter provides two methods to do just that: wait_window and wait_variable. The most common method is to open a window and then wait for it to be destroyed. Some good examples can be found on the effbot site, on a page titled Dialog Windows.

这是一个简单的说明.它不是生产就绪的,但说明了总体思路.至少你需要在对话框上添加一个抓取,这样你就不能在对话框打开时与主窗口交互,因为你说你希望对话框是模态的.

Here's a simple illustration. It's not production-ready, but illustrates the general idea. At the very least you'll want to add a grab on the dialog so that you can't interact with the main window while the dialog is open, since you said you want the dialog to be modal.

import Tkinter as tk
class MyDialog(object):
    def __init__(self, parent):
        self.toplevel = tk.Toplevel(parent)
        self.var = tk.StringVar()
        label = tk.Label(self.toplevel, text="Pick something:")
        om = tk.OptionMenu(self.toplevel, self.var, "one", "two","three")
        button = tk.Button(self.toplevel, text="OK", command=self.toplevel.destroy)
        label.pack(side="top", fill="x")
        om.pack(side="top", fill="x")
        button.pack()

    def show(self):
        self.toplevel.deiconify()
        self.toplevel.wait_window()
        value = self.var.get()
        return value


class Example(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        self.button = tk.Button(self, text="Click me!", command=self.on_click)
        self.label = tk.Label(self, width=80)
        self.label.pack(side="top", fill="x")
        self.button.pack(pady=20)

    def on_click(self):
        result = MyDialog(self).show()
        self.label.configure(text="your result: %s" % result)

if __name__ == "__main__":
    root = tk.Tk()
    Example(root).pack(fill="both", expand=True)
    root.mainloop()

这篇关于创建 Tkinter 类并等待返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆