0
希望有人可以用套接字和GUI来解决这个问题。在Tkinter的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()
我在GUI东西里打包了标签。使用.config()尝试更新“measuredistance”函数中的值。它的工作原理,非常感谢!有时候一个简单的东西可以解决“大”的问题(对我来说很重要,我是一个初学者)。希望这个答案也能帮助别人。 – 2014-10-17 09:33:51