我的条目小部件替换列表中的项目而不是添加为新项目?

时间:2018-05-26 22:53:12

标签: list tkinter append tkinter-entry

它不是将job_list条目附加到列表,而是在您尝试输入新值之前替换该项目。我认为您可以主要关注store_job方法,作业号输入和输入作业按钮。我不确定是否需要重置方法或其他东西,以便Entry小部件可以获取更多数据。任何帮助将非常感激。

from tkinter import *
class EntryGUI:
    def __init__(self, parent):
        self.cb = IntVar()
        self.job_number_label= Label(parent, text = "Job number:")
        self.job_number_label.grid(row = 1, column = 0)
        self.job_number = Entry(parent)
        self.job_number.focus()
        self.job_number.grid(row=1, column = 1)
        self.customer_name_label = Label(parent, text = "Customer name:")
        self.customer_name_label.grid(row = 2, column = 0)
        self.customer_name = Entry(parent)
        self.customer_name.grid(row=2, column = 1)
        self.distance_label= Label(parent, text = "Distance Travelled (km):")
        self.distance_label.grid(row = 3, column = 0)
        self.distance = Entry(parent)
        self.distance.grid(row=3, column = 1)
        self.min_spent_label= Label(parent, text = "Minutes spent on Virus protection:")
        self.min_spent_label.grid(row = 4, column = 0)
        self.min_spent = Entry(parent)
        self.min_spent.grid(row=4, column = 1)
        wof_tune= Checkbutton(parent, variable = self.cb, text = "check if WOF and tune is required",
                          onvalue = 100, offvalue = 0)
        wof_tune.grid(row = 5, column = 0)
        self.enter = Button(parent, text = "Enter Job", command = lambda:[self.store_job(),self.calculate()])
        self.enter.grid(row = 6, column = 0)
        self.show_all = Button(parent, text = "Show All")
        self.show_all.grid(row = 6, column = 1)
    def store_job(self):
        self.job_list = []
        self.customer_list = []
        self.job_list.append(self.job_number.get())
        self.customer_list.append(self.customer_name.get())
        for i in self.job_list:
            print (i)


    def calculate(self):
        self.cost_list = []

        self.distance_calc = int(self.distance.get())
        self.min_calc = int(self.min_spent.get())
        self.cost = 0

        #calculates the travel cost
        #if the distance is less than 5km it costs 10
        if self.distance_calc <= 5:
            self.cost = 10
        else: 
            self.distance_calc = self.distance_calc - 5 #-5 as you calclate the extra distance
            self.distance_calc = self.distance_calc / 2 #divide by two as you add 50c per km
            self.cost = self.distance_calc + 10 #initial 10 plus half the extra distance
            #print(self.cost)

        self.cost = self.cost + (self.min_calc * 0.8)
        self.cost = self.cost + int(self.cb.get())
        self.cost_list.append(self.cost)
        print(self.cost_list)
        self.enter_next()

    def enter_next(self):

        self.job_number.delete(0,END)
        self.customer_name.delete(0, END)
        self.distance.delete(0, END)
        self.min_spent.delete(0, END)
        self.enter.configure(state = NORMAL)


if __name__=="__main__":
    root = Tk()
    show_label = EntryGUI(root)
    root.mainloop()

1 个答案:

答案 0 :(得分:1)

您每次输入数据时都会重置用于存储输入数据的列表;您需要在__init__中声明存储属性,然后使用它们来累积数据:

class EntryGUI:

    def __init__(self, parent):
        self.cb = IntVar()
        self.job_number_label = Label(parent, text="Job number:")
        self.job_number_label.grid(row=1, column=0)
        self.job_number = Entry(parent)
        self.job_number.focus()
        self.job_number.grid(row=1, column=1)
        self.customer_name_label = Label(parent, text="Customer name:")
        self.customer_name_label.grid(row=2, column=0)
        self.customer_name = Entry(parent)
        self.customer_name.grid(row=2, column=1)
        self.distance_label = Label(parent, text="Distance Travelled (km):")
        self.distance_label.grid(row=3, column=0)
        self.distance = Entry(parent)
        self.distance.grid(row=3, column=1)
        self.min_spent_label = Label(parent, text="Minutes spent on Virus protection:")
        self.min_spent_label.grid(row=4, column=0)
        self.min_spent = Entry(parent)
        self.min_spent.grid(row=4, column=1)
        wof_tune= Checkbutton(parent, variable=self.cb, text="check if WOF and tune is required",
                              onvalue =100, offvalue=0)
        wof_tune.grid(row=5, column=0)
        self.enter = Button(parent, text="Enter Job", command=self.acquire_entries)
        self.enter.grid(row=6, column=0)
        self.show_all = Button(parent, text="Show All")
        self.show_all.grid(row=6, column=1)

        self.job_list = []
        self.customer_list = []
        self.cost_list = []

    def acquire_entries(self):
        self.store_job()
        self.calculate()

    def store_job(self):

        self.job_list.append(self.job_number.get())
        self.customer_list.append(self.customer_name.get())

    def calculate(self):

        self.distance_calc = int(self.distance.get())
        self.min_calc = int(self.min_spent.get())
        self.cost = 0
        # ... unchanged below