通过自定义对话框获取变量#

48 阅读2分钟

在使用 Tkinter 库创建自定义对话框时,我们需要获取对话框中输入的数据。通常,我们可以在对话框的类中定义一个变量来存储输入的数据,然后在其他类中访问这个变量。但是,当我们尝试从另一个类的实例中访问对话框中的变量时,可能会遇到错误。

具体来说,在下面的代码中,我们定义了一个 socialDialog 类来创建对话框,并在 ok 方法中将对话框中的输入数据存储在 self.social 变量中。然后,在 App 类的 getSocial 方法中,我们创建了一个 socialDialog 类的实例 d,并尝试打印 d.social 变量。但是,我们会得到一个错误,说 d 对象没有 social 属性。

from Tkinter import *

class App:
    def __init__(self, master):
        b = Button(text="Click for social dialog", command=self.getSocial)
        b.grid(row=0, column=0)

    def getSocial(self):
        d = socialDialog(root)
        print(d.social)  # Error: 'socialDialog' object has no attribute 'social'

class socialDialog:
    def __init__(self, master):
        self.top = Toplevel()
        Label(self.top, text='Social Security #: ').grid(row=0, column=0)
        self.entry = Entry(self.top)
        self.entry.grid(row=0, column=1)
        self.entry.focus_set()
        self.top.bind('<Key>', self.formatData)
        self.top.bind('<Return>', self.ok)

    def formatData(self, master):
        currentData = self.entry.get()
        if len(currentData) == 3:
            self.entry.insert(3, '-')
        elif len(currentData) == 6:
            self.entry.insert(6, '-')
        elif len(currentData) > 11:
            self.entry.delete(-1, END)

    def ok(self, master):
        self.social = self.entry.get()
        self.top.destroy()

root = Tk()
app = App(root)
root.mainloop()

2、解决方案

为了解决这个问题,我们需要在 App 类中创建一个变量来存储 socialDialog 类实例。然后,我们可以在 socialDialog 类中将输入的数据存储在 App 类变量中。这样,我们就可以在 App 类中访问对话框中的输入数据。

修改后的代码如下:

from Tkinter import *

class App:
    def __init__(self, master):
        self.social = None  # Create a variable to store the social security number

        b = Button(text="Click for social dialog", command=self.getSocial)
        b.grid(row=0, column=0)

    def getSocial(self):
        d = socialDialog(root, self)  # Pass the App instance to the socialDialog constructor
        d.top.mainloop()  # Wait for the dialog to be closed

class socialDialog:
    def __init__(self, master, app):
        self.top = Toplevel()
        self.app = app  # Store the App instance

        Label(self.top, text='Social Security #: ').grid(row=0, column=0)
        self.entry = Entry(self.top)
        self.entry.grid(row=0, column=1)
        self.entry.focus_set()
        self.top.bind('<Key>', self.formatData)
        self.top.bind('<Return>', self.ok)

    def formatData(self, master):
        currentData = self.entry.get()
        if len(currentData) == 3:
            self.entry.insert(3, '-')
        elif len(currentData) == 6:
            self.entry.insert(6, '-')
        elif len(currentData) > 11:
            self.entry.delete(-1, END)

    def ok(self, master):
        self.app.social = self.entry.get()  # Store the social security number in the App instance
        self.top.destroy()

root = Tk()
app = App(root)
root.mainloop()

现在,当我们点击“Click for social dialog”按钮时,对话框会出现。我们可以输入社会安全号码并点击“OK”按钮来关闭对话框。输入的数据将被存储在 app.social 变量中,并且我们可以通过 app.social 来访问它。