更新tkinter标签以在我的python GUI上一次显示一行文本文件

时间:2017-12-11 08:28:03

标签: python python-3.x user-interface tkinter

我正在使用tkinter创建我的第一个GUI。我已经创建了几个类,但需要帮助。我正在尝试读取文件的一行,使用标签在GUI上显示它,读取下一行并更新标签..依此类推,直到我到达文件的末尾。 (实际上并没有读取传感器值,它只是创建了一个虚拟函数。相反,我正在读取.txt文件中的数据)

我不知道该怎么做。任何反馈都会有帮助。

class HealthWindow(Frame):
def __init__(self, parent, controller):
    Frame.__init__(self, parent)
    self.configure(background='gray')  # change bg color
    label = Label(self, text="Health Status", bg="gray", font=LARGE_FONT)
    label.pack(pady=10, padx=10)

    button1 = ttk.Button(self, text="Back to Main",
                         command=lambda: controller.show_frame(FirstWindow)) 
 # ttk buttons are better looking
    button1.pack()
    displayline = Label(self, text="", font=LARGE_FONT)
    displayline.pack()

    def readSensor():
        with open("data.txt") as f:
            #for line in f:
                #time.sleep(2)
            temp = f.readline()
            displayline.configure(text=str(temp))

    def update():
        readSensor()
        self.after(1000, update)

    buttonClick = ttk.Button(self, text="View Status", command= lambda: 
readSensor())  # ttk buttons are better looking
    buttonClick.pack()

1 个答案:

答案 0 :(得分:1)

你必须只打开一次文件 - 即。在__init__中 - 然后逐行阅读。

class HealthWindow(Frame):

    def __init__(self, parent, controller):
        super().__init__(parent)

        self.configure(background='gray')  # change bg color

        label = Label(self, text="Health Status", bg="gray", font=LARGE_FONT)
        label.pack(pady=10, padx=10)

        button1 = ttk.Button(self, text="Back to Main",
                             command=lambda: controller.show_frame(FirstWindow)) 
        button1.pack()

        # with self. to have access in other methods
        self.displayline = Label(self, text="", font=LARGE_FONT)
        self.displayline.pack()

        buttonClick = ttk.Button(self, text="View Status", command=self.update)
        buttonClick.pack()

        # open only once
        self.f = open("data.txt")

    # method,not internal function
    def update(self):
        try:
            temp = self.f.readline()
            self.displayline["text"] = temp
            self.after(1000, self.update)
        except Exception as ex:
            print('Error:', ex)
            print('Probably end of file')
            self.f.close()

BTW:当您再次点击update时,仍然需要检查文件是否未打开。

编辑:现在按钮运行start_update,检查文件是否已打开。

我使用self.f = None来控制它。

after仍然运行update,而不是start_update

class HealthWindow(Frame):

    def __init__(self, parent, controller):
        super().__init__(parent)

        self.configure(background='gray')  # change bg color

        label = Label(self, text="Health Status", bg="gray", font=LARGE_FONT)
        label.pack(pady=10, padx=10)

        button1 = ttk.Button(self, text="Back to Main",
                             command=lambda: controller.show_frame(FirstWindow)) 
        button1.pack()

        self.displayline = Label(self, text="", font=LARGE_FONT)
        self.displayline.pack()

        buttonClick = ttk.Button(self, text="View Status", command=self.start_update)
        buttonClick.pack()

        self.f = None # to see if it is open when you click button next time

    def start_update(self):
        if self.f is None:
            # open only once
            self.f = open("data.txt")
            self.update()        

    def update(self):
        temp = self.f.readline()
        self.displayline["text"] = temp

        if temp: # there was text in file
            self.after(1000, self.update)
        else:
            print('Probably end of file')
            self.f.close()
            self.f = None # so you can open again