如何在另一个类的Tkinter列表框中添加数据?

时间:2014-01-04 06:48:42

标签: python user-interface listbox tkinter

我有一个带有(按钮,两个输入框和一个列表框)的GUI类,我想在Listbox中插入另一个类的一些数据。这是我的GUI类...

class GUI(object):      
def __init__(self, parent):
    frame = Frame(parent.title("CAN Interface"))
    frame.pack()
    self.lbfirst = Label(frame, text="CAN ID:")
    self.lbfirst.grid(row=1,column=0)
    self.first = Entry(frame)
    self.first.grid(row=2,column=0)
    self.lbsecond = Label(frame, text="CAN Data:")
    self.lbsecond.grid(row=1,column=2)
    self.second = Entry(frame)
    self.second.grid(row=2,column=2)
    self.number=Button(frame, text="Send", fg="red", command=self.call_comm)
    self.number.grid(row=5,column=1)
    self.result = Listbox(frame)
    self.result.grid(row=9,column=0, columnspan=5, sticky=N+S+E+W)

def call_comm_rxc(self, data1):
    print("in call_comm_rxc")
    self.data1 = data1
    self.result.insert (END, 'CAN ID : '+str(self.data1[0])+'|Length : '+str(self.data1[1])+'|Data : '+str(self.data1[2]))

&安培;这是我想要访问GUI类的 self.result 属性的另一个类

class Comm(Thread, Format, GUI):
def __init__(self):
    Thread.__init__(self) # Call the Thread class's init function
def run(self):
    while True:
        # here need to add the code which needs to run all the time
        print("in true")            
        self.CAN_Receive()

def CAN_Receive(self):
    print("in rxc")
    #self.result1 = result1
    self.data = Format.UnPack(self,b'\x12\x00\x00\x00\x03\x00\x00\x00\x01\x02\x03\x00\x00\x00\x00\x00')
    GUI.call_comm_rxc(self, self.data)

我面临的错误是“AttributeError:'Comm'对象没有属性'result'

Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Python33\lib\threading.py", line 901, in _bootstrap_inner
self.run()
File "E:\Studies\Ctech\MicroLab Files\example code.py", line 124, in run
self.CAN_Receive()
File "E:\Studies\Ctech\MicroLab Files\example code.py", line 141, in CAN_Receive
GUI.call_comm_rxc(self, self.data)
File "E:\Studies\Ctech\MicroLab Files\example code.py", line 110, in call_comm_rxc
self.result.insert (END, 'CAN ID : '+str(self.data1[0])+'|Length : '+str(self.data1[1])+'|Data :   '+str(self.data1[2]))
AttributeError: 'Comm' object has no attribute 'result'

请帮助我如何从子类(Comm)

插入Listbox

1 个答案:

答案 0 :(得分:1)

GUI中缺少类Comm的初始化。

GUI.__init__(self, parent)添加到Comm.__init__