使用类和函数tkinter连接多个条目

时间:2018-02-15 17:19:35

标签: python tkinter

我使用Python(tkinter)创建了一个程序。当您单击按钮"添加任务"新行已创建。用户需要在"尺寸[m2]"中输入值。和"总成本"条目,当他/她点击按钮"总和值",在成本/平方米下的输入框中应插入值"总成本" /"大小[m2]& #34;成本/平方米下所有值的总和(总和应该在"答案E")

我这样做是为了它可以在成本/平方米下对输入框中输入的值求和,但我不能使它执行"总成本" /"大小[m2] "并在cost / m2下插入该答案,然后对其求和。我总是得到这个错误:

CostSize=float(CostEntry.get())/float(SizeEntry.get())
NameError: name 'CostEntry' is not defined

我想要这个:

enter image description here

我该怎么做才能识别CostEntry? 这是代码:

from tkinter import *
from tkinter import ttk

myApp = Tk()
myApp.title("APP")                         
myApp.geometry("1000x650")

frame1=LabelFrame(myApp,text=" Activities ")
frame1.grid(row=0,column=0,padx=5)

i=Label(frame1, text=" i ")
i.grid(row=0,column=1)

ProjectName=Label(frame1, text=" Project name ")
ProjectName.grid(row=0,column=2)

SizeLabel=Label(frame1, text="Size [m2]")
SizeLabel.grid(row=0,column=3)

TotalCostLabel=Label(frame1, text="Total Cost")
TotalCostLabel.grid(row=0,column=4)

CostSizeLabel=Label(frame1, text="Cost/m2")
CostSizeLabel.grid(row=0,column=5)

newrow=1
class AddNewTask(object):
    rowlist=[]

    def update_row_values(self):

        for i,entry in enumerate(self.rowlist):
            entry.delete(0, 'end')
            entry.insert(0,i+1)

    def addTask(self):

        def delete():
            try:
                sum = int(answerE.get())
                entry_value = int(prodEntry.get())
                new_sum = sum - entry_value
                answerE.delete(0, 'end')
                answerE.insert(0, sum)
            except ValueError:
                pass

            bdelete.destroy()
            iEntry.destroy()
            ProjectEntry.destroy()
            SizeEntry.destroy()   
            CostEntry.destroy()
            CostSizeEntry.destroy()

            CostSizeEntry.destroy()
            self.rowlist.remove(iEntry)
            self.update_row_values()
            self.entrylist.remove(CostSizeEntry)


        global newrow
        newrow=newrow+1

        bdelete=Button(frame1,text="-",command=delete)
        bdelete.grid(row=newrow,column=0,sticky="E",padx=4)

        iEntry=Entry(frame1,width=3)
        self.rowlist.append(iEntry)     
        iEntry.grid(row=newrow,column=1,padx=1)
        n = len(self.rowlist)
        iEntry.insert(0,n)

        ProjectEntry=Entry(frame1,width=75)
        ProjectEntry.grid(row=newrow,column=2,padx=1)

        SizeEntry=Entry(frame1,width=10)
        SizeEntry.grid(row=newrow,column=3,padx=1)

        CostEntry=Entry(frame1,width=10)
        CostEntry.grid(row=newrow,column=4,padx=1)

        CostSizeEntry=Entry(frame1,width=10)
        CostSizeEntry.grid(row=newrow,column=5,padx=1)

        self.entrylist.append(CostSizeEntry)


    def __init__(self):

        buttonadd=Button(frame1,text="Add Task",command=self.addTask)
        buttonadd.grid(row=0,column=0,padx=3,pady=5)
        self.entrylist = []

    def sumValues():

        try:                                
            CostSize=float(CostEntry.get())/float(SizeEntry.get())
            CostSizeEntry.insert(0,CostSize)
        except ValueError:
            pass

        sum = 0        
        for entry in AddNewTask.entrylist:
            try:
                sum += float(entry.get())
            except ValueError:
                pass

            answerE.delete(0, 'end')
            answerE.insert(0, sum)

    sumButton = Button(frame1, text="Sum Values", command=sumValues)
    sumButton.grid(row=0, column=7)


AddNewTask=AddNewTask()


frame2=LabelFrame(myApp,text=" Answer")
frame2.grid(row=0,column=1,padx=5,sticky="N")

answerL=Label(frame2, text="Answer:  ")
answerL.grid(row=0,column=0)

answerE=Entry(frame2,width=10)
answerE.grid(row=0, column=1)

myApp.mainloop()

2 个答案:

答案 0 :(得分:1)

CostEntry变量是在类方法中创建的。但是,它是使用本地范围创建的。这意味着当方法结束时,对CostEntry的引用也是如此。并且下次调用方法(销毁对象)时,它不存在。

解决方案:使用self属性创建变量实例。

self.CostEntry=Entry(frame1,width=10)
self.CostEntry.grid(row=newrow,column=4,padx=1)

当你去删除它时:

self.CostEntry.destroy()

答案 1 :(得分:0)

您应该将self属性用于在本地范围之外使用的变量。试试这个:

from tkinter import *
from tkinter import ttk

myApp = Tk()
myApp.title("APP")                         
myApp.geometry("1000x650")

frame1=LabelFrame(myApp,text=" Activities ")
frame1.grid(row=0,column=0,padx=5)

i=Label(frame1, text=" i ")
i.grid(row=0,column=1)

ProjectName=Label(frame1, text=" Project name ")
ProjectName.grid(row=0,column=2)

SizeLabel=Label(frame1, text="Size [m2]")
SizeLabel.grid(row=0,column=3)

TotalCostLabel=Label(frame1, text="Total Cost")
TotalCostLabel.grid(row=0,column=4)

CostSizeLabel=Label(frame1, text="Cost/m2")
CostSizeLabel.grid(row=0,column=5)

newrow=1
class AddNewTask(object):
    rowlist=[]

    def update_row_values(self):

        for i,entry in enumerate(self.rowlist):
            entry.delete(0, 'end')
            entry.insert(0,i+1)

    def addTask(self):

        def delete():
            try:
                sum = int(answerE.get())
                entry_value = int(prodEntry.get())
                new_sum = sum - entry_value
                answerE.delete(0, 'end')
                answerE.insert(0, sum)
            except ValueError:
                pass

            bdelete.destroy()
            iEntry.destroy()
            ProjectEntry.destroy()
            self.SizeEntry.destroy()   
            self.CostEntry.destroy()
            self.CostSizeEntry.destroy()

            self.rowlist.remove(iEntry)
            self.update_row_values()
            self.entrylist.remove(CostSizeEntry)


        global newrow
        newrow=newrow+1

        bdelete=Button(frame1,text="-",command=delete)
        bdelete.grid(row=newrow,column=0,sticky="E",padx=4)

        iEntry=Entry(frame1,width=3)
        self.rowlist.append(iEntry)     
        iEntry.grid(row=newrow,column=1,padx=1)
        n = len(self.rowlist)
        iEntry.insert(0,n)

        ProjectEntry=Entry(frame1,width=75)
        ProjectEntry.grid(row=newrow,column=2,padx=1)

        self.SizeEntry=Entry(frame1,width=10)
        self.SizeEntry.grid(row=newrow,column=3,padx=1)

        self.CostEntry=Entry(frame1,width=10)
        self.CostEntry.grid(row=newrow,column=4,padx=1)

        self.CostSizeEntry=Entry(frame1,width=10)
        self.CostSizeEntry.grid(row=newrow,column=5,padx=1)

        self.entrylist.append(self.CostSizeEntry)


    def __init__(self):

        buttonadd=Button(frame1,text="Add Task",command=self.addTask)
        buttonadd.grid(row=0,column=0,padx=3,pady=5)
        self.entrylist = []

        self.sumButton = Button(frame1, text="Sum Values", command=self.sumValues)
        self.sumButton.grid(row=0, column=7)


    def sumValues(self):

        try:                                
            CostSize=float(self.CostEntry.get())/float(self.SizeEntry.get())
            self.CostSizeEntry.insert(0,CostSize)
        except ValueError:
            pass

        sum = 0        
        for entry in AddNewTask.entrylist:
            try:
                sum += float(entry.get())
            except ValueError:
                pass

            answerE.delete(0, 'end')
            answerE.insert(0, sum)



AddNewTask=AddNewTask()


frame2=LabelFrame(myApp,text=" Answer")
frame2.grid(row=0,column=1,padx=5,sticky="N")

answerL=Label(frame2, text="Answer:  ")
answerL.grid(row=0,column=0)

answerE=Entry(frame2,width=10)
answerE.grid(row=0, column=1)

myApp.mainloop()