Tkinter更新标签时出现错误:“AttributeError: ‘Frame’对象没有属性’label1’”

huangapple go评论70阅读模式
英文:

Tkinter updating label fails with "AttributeError: 'Frame' object has no attribute 'label1' "

问题

#!/usr/bin/python3
import tkinter as tk
import tkinter.ttk as ttk
import serial
Arduino = serial.Serial('/dev/ttyUSB3', '115200', timeout=1)
print('连接正常')

value1 = 0
value2 = 0
value3 = 0
value4 = 0

class App:

    def readdata(self):
        Arduino.write(bytes('n', 'utf-8'))
        for x in range(0, 6):
            globals()['raw%s' % x] = Arduino.readline().decode().strip()
        self.value1 = int(raw1)
        self.value2 = int(raw2)
        self.value3 = int(raw3)
        self.value4 = int(raw4)
        self.changelabels()

    def changelabels(self):
        label1.config(text=self.value1.get())

    def __init__(self, master=None):
        # build ui
        self.appwindow = tk.Tk() if master is None else tk.Toplevel(master)
        self.appwindow.configure(height=240, width=320)
        self.appwindow.minsize(320, 240)
        self.mainframe = ttk.Frame(self.appwindow)
        self.mainframe.configure(height=240, width=320)
        label1 = ttk.Label(self.mainframe)
        self.value1 = tk.IntVar()
        label1.configure(textvariable=self.value1)
        label1.grid(column=1, row=0)
        label2 = ttk.Label(self.mainframe)
        self.value2 = tk.IntVar()
        label2.configure(textvariable=self.value2)
        label2.grid(column=1, row=1)
        label3 = ttk.Label(self.mainframe)
        self.value3 = tk.IntVar()
        label3.configure(textvariable=self.value3)
        label3.grid(column=1, row=2)
        label4 = ttk.Label(self.mainframe)
        self.value4 = tk.IntVar()
        label4.configure(textvariable=self.value4)
        label4.grid(column=1, row=3)
        self.text1 = ttk.Label(self.mainframe)
        self.text1.configure(text='Value1: ')
        self.text1.grid(column=0, row=0)
        self.text2 = ttk.Label(self.mainframe)
        self.text2.configure(text='Value2: ')
        self.text2.grid(column=0, row=1)
        self.text3 = ttk.Label(self.mainframe)
        self.text3.configure(text='Value3: ')
        self.text3.grid(column=0, row=2)
        self.text4 = ttk.Label(self.mainframe)
        self.text4.configure(text='Value4:  ')
        self.text4.grid(column=0, row=3)
        progressbar1 = ttk.Progressbar(self.mainframe)
        progressbar1.configure(orient="horizontal")
        progressbar1.grid(column=2, row=0)
        progressbar2 = ttk.Progressbar(self.mainframe)
        progressbar2.configure(orient="horizontal")
        progressbar2.grid(column=2, row=1)
        progressbar3 = ttk.Progressbar(self.mainframe)
        progressbar3.configure(orient="horizontal")
        progressbar3.grid(column=2, row=2)
        progressbar4 = ttk.Progressbar(self.mainframe)
        progressbar4.configure(orient="horizontal")
        progressbar4.grid(column=2, row=3)
        self.refresh = ttk.Button(self.mainframe)
        self.refresh.configure(text='刷新')
        self.refresh.configure(command=self.readdata)
        self.refresh.grid(column=1, row=4)
        self.mainframe.pack(side="top")

        # 主窗口
        self.mainwindow = self.appwindow

    def run(self):
        self.mainwindow.mainloop()

if __name__ == "__main__":
    app = App()
    app.run()

请注意,我已经将代码中的中文注释翻译为英文,但未翻译代码本身。

英文:

I'm trying to put together my first tkinter application, showing values recieved from Arduino serial port, I successfully managed to make console application, but I'm novice in GUI.

raw0 is string with 'NR'
raw 1 - 4 are int compatible values
raw5 is string 'EOR'

here is my code:

#!/usr/bin/python3
import tkinter as tk
import tkinter.ttk as ttk
import serial
Arduino = serial.Serial('/dev/ttyUSB3', '115200', timeout=1)
print('Connection is working')
value1 = 0
value2 = 0
value3 = 0
value4 = 0
class App:
def readdata(self):
Arduino.write(bytes('n', 'utf-8'))
for x in range(0, 6):
globals()['raw%s' % x] = Arduino.readline().decode().strip()
self.value1 = int(raw1)
self.value2 = int(raw2)
self.value3 = int(raw3)
self.value4 = int(raw4)
App.changelabels(self.mainframe)
def changelabels(self):
label1.config(text=self.value1.get())
def __init__(self, master=None):
# build ui
self.appwindow = tk.Tk() if master is None else tk.Toplevel(master)
self.appwindow.configure(height=240, width=320)
self.appwindow.minsize(320, 240)
self.mainframe = ttk.Frame(self.appwindow)
self.mainframe.configure(height=240, width=320)
label1 = ttk.Label(self.mainframe)
self.value1 = tk.IntVar()
label1.configure(textvariable=self.value1)
label1.grid(column=1, row=0)
label2 = ttk.Label(self.mainframe)
self.value2 = tk.IntVar()
label2.configure(textvariable=self.value2)
label2.grid(column=1, row=1)
label3 = ttk.Label(self.mainframe)
self.value3 = tk.IntVar()
label3.configure(textvariable=self.value3)
label3.grid(column=1, row=2)
label4 = ttk.Label(self.mainframe)
self.value4 = tk.IntVar()
label4.configure(textvariable=self.value4)
label4.grid(column=1, row=3)
self.text1 = ttk.Label(self.mainframe)
self.text1.configure(text='Value1: ')
self.text1.grid(column=0, row=0)
self.text2 = ttk.Label(self.mainframe)
self.text2.configure(text='Value2: ')
self.text2.grid(column=0, row=1)
self.text3 = ttk.Label(self.mainframe)
self.text3.configure(text='Value3: ')
self.text3.grid(column=0, row=2)
self.text4 = ttk.Label(self.mainframe)
self.text4.configure(text='Value4:  ')
self.text4.grid(column=0, row=3)
progressbar1 = ttk.Progressbar(self.mainframe)
progressbar1.configure(orient="horizontal")
progressbar1.grid(column=2, row=0)
progressbar2 = ttk.Progressbar(self.mainframe)
progressbar2.configure(orient="horizontal")
progressbar2.grid(column=2, row=1)
progressbar3 = ttk.Progressbar(self.mainframe)
progressbar3.configure(orient="horizontal")
progressbar3.grid(column=2, row=2)
progressbar4 = ttk.Progressbar(self.mainframe)
progressbar4.configure(orient="horizontal")
progressbar4.grid(column=2, row=3)
self.refresh = ttk.Button(self.mainframe)
self.refresh.configure(text='Refresh')
self.refresh.configure(command=self.readdata)
self.refresh.grid(column=1, row=4)
self.mainframe.pack(side="top")
# Main widget
self.mainwindow = self.appwindow
def run(self):
self.mainwindow.mainloop()
if __name__ == "__main__":
app = App()
app.run()

Thank you very much and have a nice day!

I'm trying to update labels (label1-4) with variables stored in value1-4, but I'm unable to do so, mostly because I don't know how to approach labels correctly.
I've tried various ways how to approach this, but I've failed every time. Is anybody willing to kick me into right direction?

答案1

得分: 0

label1 = ttk.Label(self.mainframe)修改为self.label1 = ttk.Label(self.mainframe),并且在所有其他地方将对label1的引用都以self为前缀使用。这样,所有属于您的App类的方法(并且可以访问self)将能够访问self.label1

英文:

Change label1 = ttk.Label(self.mainframe) to self.label1 = ttk.Label(self.mainframe) and similarly prefix all references to label1 with self everywhere else it's used. That way, all methods that are members of your App class (and have access to self) will have access to self.label1

huangapple
  • 本文由 发表于 2023年4月11日 01:11:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75979136.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定