为什么我不能在函数内创建类对象?

时间:2019-05-28 16:46:27

标签: python class tkinter

我的主要代码包含几个功能,并且我对其进行了测试,并且效果很好。然后,我为tkinter窗口创建一个类,并分别对其进行测试,它也可以正常工作。当将两者结合在一起时,通过在函数内部创建类对象,我会收到以下错误消息:

"TclError: can't invoke "frame" command: application has been destroyed"

import tkinter

class InputFromForm():
    def __init__(self, root, values):
        self.field = []
        self.fieldStrings = values
        self.temp = self.makeform(self.fieldStrings)

    def makeform(self, fields):
        entries = []
        for i in fields:
            row = tkinter.Frame(root)
            lab = tkinter.Label(row, width=15, text=i, anchor='w')
            ent = tkinter.Entry(row)
            ent.insert(tkinter.END, fields[i])
            ent.bind("<FocusIn>", lambda event, e=ent: e.selection_range(0, tkinter.END))
            row.pack(side=tkinter.TOP, fill=tkinter.X, padx=5, pady=10)
            lab.pack(side=tkinter.LEFT)
            ent.pack(side=tkinter.RIGHT, expand=tkinter.YES, fill=tkinter.X)
            entries.append((i, ent))

        return entries


    def fetch(self, entries):
        for entry in entries:
            string = entry[0]
            text  = entry[1].get()
            self.field.append(text)
        return self.field

    def close(self, root):
        root.destroy()


def extractData(dOfValues):
    root = tkinter.Tk()
    root.title('Test')
    userInput = InputFromForm(root, dOfValues)
    root.bind('<Return>', (lambda event, e=userInput.temp: userInput.fetch(e)))
    b1 = tkinter.Button(root, text='OK',command=(lambda e=userInput.temp: [userInput.fetch(e), userInput.close(root)]))
    b1.config(width=25)
    b1.pack(anchor=tkinter.CENTER, padx=5, pady=20)


    root.mainloop()



if __name__ == '__main__':


    dOfValues = {'Customer No.': '',
            'PO #':'020202',
            'Order Date': '2/3/2012',
            'Zip to code':'92182'}

    #root = tkinter.Tk()
    #root.title('Test')

    #userInput = InputFromForm(root, dOfValues)

    #root.bind('<Return>', (lambda event, e=userInput.temp: userInput.fetch(e)))
    #b1 = tkinter.Button(root, text='OK',command=(lambda e=userInput.temp: [userInput.fetch(e), userInput.close(root)]))
    #b1.config(width=25)
    #b1.pack(anchor=tkinter.CENTER, padx=5, pady=20)


    #root.mainloop()
    extractData(dOfValues)  # Adding this, creates the error. If I put the code 
                            # from this function into main, it works. 

有人可以解释发生了什么事吗?

2 个答案:

答案 0 :(得分:1)

@paul“打印和退货完全无关”

确定吗?试试这个代码。我想您可以更改它来检索

您想要的,如果我了解您想要的,则您字段中的值。

import tkinter as tk
from tkinter import messagebox

class InputFromForm(tk.Tk):
    def __init__(self, root, values):
        self.field = []
        self.entries = []
        self.fieldStrings = values
        self.temp = self.makeform(self.fieldStrings)

    def makeform(self, fields):

        entries = []

        for i in fields:
            row = tk.Frame()
            lab = tk.Label(row, width=15, text=i, anchor='w')
            ent = tk.Entry(row)
            ent.insert(tk.END, fields[i])
            ent.bind("<FocusIn>", lambda event, e=ent: e.selection_range(0, tk.END))
            row.pack(side=tk.TOP, fill=tk.X, padx=5, pady=10)
            lab.pack(side=tk.LEFT)
            ent.pack(side=tk.RIGHT, expand=tk.YES, fill=tk.X)
            entries.append((i, ent))

        return entries 


    def fetch(self, entries):
        for entry in entries:
            string = entry[0]
            text  = entry[1].get()
            self.field.append(text)
        return self.field

    def close(self, root):

        msg = "Do you want to quit?\nI've this data for you\n {0}".format(self.field)

        if messagebox.askokcancel("Simple App", msg):
            root.destroy()



def extractData(dOfValues):
    root = tk.Tk()
    root.title('Test')
    userInput = InputFromForm(root, dOfValues)
    root.bind('<Return>', (lambda event, e=userInput.temp: userInput.fetch(e)))
    b1 = tk.Button(root, text='OK',command=(lambda e=userInput.temp: [userInput.fetch(e), userInput.close(root)]))
    b1.config(width=25)
    b1.pack(anchor=tk.CENTER, padx=5, pady=20)


    root.mainloop()

if __name__ == '__main__':


    dOfValues = {'Customer No.': '',
            'PO #':'020202',
            'Order Date': '2/3/2012',
            'Zip to code':'92182'}

    #root = tk.Tk()
    #root.title('Test')

    #userInput = InputFromForm(root, dOfValues)

    #root.bind('<Return>', (lambda event, e=userInput.temp: userInput.fetch(e)))
    #b1 = tk.Button(root, text='OK',command=(lambda e=userInput.temp: [userInput.fetch(e), userInput.close(root)]))
    #b1.config(width=25)
    #b1.pack(anchor=tk.CENTER, padx=5, pady=20)


    #root.mainloop()
    extractData(dOfValues)  # Adding this, creates the error. If I put the code 
                            # from this function into main, it works. 

答案 1 :(得分:0)

我不确定是什么问题,但是我做了一些更改

在您的脚本上。首先,我将tkinter导入为tk,然后继承了

来自Tk的课程,请参见下文。

import tkinter as tk

class InputFromForm(tk.Tk):
    def __init__(self, root, values):
        self.field = []
        self.fieldStrings = values
        self.temp = self.makeform(self.fieldStrings)

    def makeform(self, fields):
        entries = []
        for i in fields:
            row = tk.Frame()
            lab = tk.Label(row, width=15, text=i, anchor='w')
            ent = tk.Entry(row)
            ent.insert(tk.END, fields[i])
            ent.bind("<FocusIn>", lambda event, e=ent: e.selection_range(0, tk.END))
            row.pack(side=tk.TOP, fill=tk.X, padx=5, pady=10)
            lab.pack(side=tk.LEFT)
            ent.pack(side=tk.RIGHT, expand=tk.YES, fill=tk.X)
            entries.append((i, ent))

        return entries


    def fetch(self, entries):
        for entry in entries:
            string = entry[0]
            text  = entry[1].get()
            self.field.append(text)
        return self.field

    def close(self, root):
        root.destroy()


def extractData(dOfValues):
    root = tk.Tk()
    root.title('Test')
    userInput = InputFromForm(root, dOfValues)
    root.bind('<Return>', (lambda event, e=userInput.temp: userInput.fetch(e)))
    b1 = tk.Button(root, text='OK',command=(lambda e=userInput.temp: [userInput.fetch(e), userInput.close(root)]))
    b1.config(width=25)
    b1.pack(anchor=tk.CENTER, padx=5, pady=20)


    root.mainloop()

if __name__ == '__main__':


    dOfValues = {'Customer No.': '',
            'PO #':'020202',
            'Order Date': '2/3/2012',
            'Zip to code':'92182'}

    #root = tk.Tk()
    #root.title('Test')

    #userInput = InputFromForm(root, dOfValues)

    #root.bind('<Return>', (lambda event, e=userInput.temp: userInput.fetch(e)))
    #b1 = tk.Button(root, text='OK',command=(lambda e=userInput.temp: [userInput.fetch(e), userInput.close(root)]))
    #b1.config(width=25)
    #b1.pack(anchor=tk.CENTER, padx=5, pady=20)


    #root.mainloop()
    extractData(dOfValues)  # Adding this, creates the error. If I put the code 
                            # from this function into main, it works. 

我明白了

enter image description here