希望有人可以通过套接字和GUI帮助解决这个问题。
我有一个客户端从套接字连接读取信息,然后我想在GUI的文本框中显示此信息,我使用线程来控制它。在GUI中有两个按钮,用于连接和读取,以及其他用于停止连接的按钮,我使用线程来完成它。但我无法管理如何将信息(“recived”)从套接字传递到GUI中的标签或文本框。我尝试使用全局变量,文本框,标签等。但我仍然无法达到它。以下是代码示例:
import socket
import Tkinter
from Tkinter import Frame, Tk, BOTH, Text, Menu, END
import threading
import tkFont
top = Tkinter.Tk()
top.config(bg="gray")
top.geometry("600x600")
top.title("GUI")
s = socket.socket()
rueda = dict()
class Looping(object):
def __init__(self):
helv100 = tkFont.Font(family='Helvetica',size=100, weight='bold')
self.B_start = Tkinter.Button(top, text ="Start",font=helv100, command = self.button_start)
self.B_start.pack(fill=BOTH, expand=1)
self.B_stop = Tkinter.Button(top, text ="Stop",font=helv100, command = self.button_stop)
self.B_stop.pack(fill=BOTH, expand=1)
self.isRunning = True
def button_start(self):
l.isRunning = True
t = threading.Thread(target = l.measuredistance)
t.start()
def measuredistance(self):
global recived
s = socket.socket()
s.connect(('172.17.18.21', 6000))
while self.isRunning == True:
recived = s.recv(60)#number of bytes recived
print recived
else:
print "Not connected to the wheel";
def button_stop(self):
l.isRunning = False
s.close()
print "Socket connection breaked"
l=Looping()
top.mainloop()
答案 0 :(得分:0)
您没有更新GUI上的内容,只需print
。
尝试将标签打包到主窗口,如下所示:
...
myLabel = Label(top, text=recived)
myLabel.pack()
...
然后使用config功能更新此标签的内容。
...
myLabel.config(text=recived)
...