【发布时间】:2019-05-10 15:31:54
【问题描述】:
我正面临这个问题。我对 py 中的线程和 GUI 都很陌生,这就是为什么我无法摆脱它。 基本上我有这个类:
class receiving(threading.Thread): #thread class
#init and other methods
def run(self):
data = self.sock.recv(1024) #sock is the socket on which the 'run' method as to listen on
UserIF.main.addNewMessage(data) #with this line i want to pass the 'data' variable to the 'addNewMessage' method
侦听套接字并返回一个字符串,我必须将此字符串写入此类中的 tkinter 'Text' 对象:
class UserIF():
def main(self):
#some code
messages = tk.Text(master=window, height=10, width=30)
messages.grid(column=5, row=4)
def addNewMessage(string):
messages.insert(string)
我正在尝试一种我知道在 python 中不存在的“转到”。
【问题讨论】:
-
您没有将
self作为def addNewMessage(string):中的参数传递,但我不相信您需要嵌套函数。tkinter对我来说不是强项。 -
@roganjosh 这不是 tkinter 真正的问题,这是我如何将线程类的 run 方法中的“数据”变量传递给 UserIF 类的 main 方法来打印它让我们说.
标签: python class user-interface methods tkinter