如何获取for x in in range循环中的列表项以显示在tkinter框架上

时间:2016-01-21 12:43:09

标签: python list for-loop tkinter function

这是原始代码的一部分。我没有收到任何错误消息,但是当我打开Calendar框架时似乎没有任何显示。我有提交(self)函数在调用时在python中打印出Control.counter的值,并且当Control.counter值大于0时没有显示任何内容。

class Setup_Info(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent, background="white")

        scrollbar = tk.Scrollbar(self)
        scrollbar.pack(side='right', fill='y')
        title2 = tk.Label(self, text="Setup Information", font=LARGE_FONT, background="white")
        title2.place(relx=0.25, y=0.00, relheight=0.2, relwidth=0.5)

        self.number = tk.Entry(self)
        self.number.place(relx=0.2, y=140, relheight=0.05)

        question1 = tk.Label(self, text="Name of medication:", font=FONT, background="white")
        question1.place(relx=0.2, y=200, relheight=0.05)
        self.entry1 = tk.Entry(self)
        self.entry1.place(relx=0.2, y=240, relheight=0.05, relwidth=0.2)

        question2 = tk.Label(self, text="When it was prescribed:", font=FONT, background="white")
        question2.place(relx=0.2, y=300, relheight=0.05)
        self.entry2 = tk.Entry(self)
        self.entry2.place(relx=0.2, y=340, relheight=0.05, relwidth=0.2)

        question3 = tk.Label(self, text="When you stop the medication:", font=FONT, background="white")
        question3.place(relx=0.2, y=400, relheight=0.05)
        self.entry3 = tk.Entry(self)
        self.entry3.place(relx=0.2, y=440, relheight=0.05, relwidth=0.2)

        question4 = tk.Label(self, text="How many times in a day do you take it:", font=FONT, background="white")
        question4.place(relx=0.2, y=500, relheight=0.05)
        self.entry4 = ttk.Combobox(self)
        self.entry4.config(values = ('1','2','3'))
        self.entry4.place(relx=0.45, y=540, relheight=0.05, relwidth=0.2)

        enter = tk.Button(self, text="Enter", font=LARGE_FONT, background="white",
                          command=self.submit)
        enter.place(relx=0.45, rely=0.2, relheight=0.05, relwidth=0.2)

        enter2 = tk.Button(self, text="Enter", font=LARGE_FONT, background="white",
                          command=lambda: controller.show_frame(Calendar))
        enter2.pack()

        Setup_Info.list1=[]
        Setup_Info.list2=[]
        Setup_Info.list3=[]
        Setup_Info.list4=[]

    def submit(self):
        print(self.number.get())
        Control.counter += int(self.number.get())
        print(Control.counter)
        entry1=self.entry1.get()
        entry2=self.entry2.get()
        entry3=self.entry3.get()
        entry4=self.entry4.get()
        Setup_Info.list1.append(entry1)
        Setup_Info.list2.append(entry2)
        Setup_Info.list3.append(entry3)
        Setup_Info.list4.append(entry4)
        print(Setup_Info.list1)
        print(Setup_Info.list2)
        print(Setup_Info.list3)
        print(Setup_Info.list4)



class Calendar(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent, background="white")

        if Control.counter > 0:
            display_info()

    def display_info():
        for x in range(Control.counter):
            self.medicine1 = tk.Label(self, text=(Setup_Info.list1[x]), font=LARGE_FONT, bg="white")
            self.medicine1.pack()

            self.medicine2 = tk.Label(self, text=(Setup_Info.list2[x]), font=LARGE_FONT, bg="white")
            self.medicine1.pack()

2 个答案:

答案 0 :(得分:0)

问题是你的Calendar类在程序启动时被实例化。在那个时间点,Control.counter为零(我假设),因为用户还没有机会输入任何内容。因此,框架是空的。

当您去展示框架时,您不会重新创建框架,或重新初始化内容,因此窗口将为空。

您需要修改代码,以便在需要时按需创建类,或者在显示框架之前立即直接调用display_info方法。

答案 1 :(得分:0)

对于一个看起来你每次初始化一个Setup_info对象时都在Setup_info中创建类变量的情况,每次创建一个列表时很可能会将列表重置为空但我无法根据代码确定您提供了,尝试从 init ()

中删除它
{hazelcast-directory}/bin/hazelcast.xml

并将他们的定义放在班级本身:

    Setup_Info.list1=[]
    Setup_Info.list2=[]
    Setup_Info.list3=[]
    Setup_Info.list4=[]

你的问题显然与控制权有关,因此如果没有相应的代码,那么我无法为你做些什么...