我正在从事类似于TeamViewer的学习项目,但是非常简单。我正在使用插槽在两台PC之间建立连接。在每个PS中,我打开两个线程:一个线程对用于从一台PC接收屏幕图像并使用tkinter在另一台计算机上显示,另一线程对用于从一台PC向另一台PC发送键盘和鼠标命令以执行命令。 但是,我遇到一个问题,我的tkinter不能从线程打开。
这是我在一台PC上的代码:
from socket import *
import _thread as thread
import tkinter, pickle
class TeamVieverServer:
port = 50004
host = ''
def __init__(self):
conn = self.connector()
self.dispacher(conn)
def connector(self, port=port, host=host):
sock = socket(AF_INET,SOCK_STREAM)
sock.bind((host, port))
sock.listen(5)
conn, addr = sock.accept()
data = conn.recv(1024)
print(b'Received data from the client is [%s]' % data)
conn.send(b'Prion')
return conn
def dispacher(self, conn):
thread.start_new_thread(self.recv_thread,(conn,))
thread.start_new_thread(self.send_thread,(conn,))
def send_thread(self, conn):
pass
def recv_thread(self, conn):
screen_size = conn.recv(1024)
width, height = pickle.loads(screen_size)
root = tkinter.Tk()
tkinter.Frame(root, width=width, height=height).pack()
root.mainloop()
if __name__ == '__main__':
TeamVieverServer()
在这里,我从其他PS接收一些信息以打开相应的tkinter窗口。
这是我用于其他PC的代码:
from socket import *
import _thread as thread
import tkinter, pickle
import pyautogui
class TeamVieverClient:
port = 50004
host = '***************'
def __init__(self):
conn = self.connector()
self.dispacher(conn)
def connector(self, port=port, host=host):
sock = socket(AF_INET,SOCK_STREAM)
sock.connect((host, port))
sock.send(b'Connection is estalished')
data = sock.recv(1024)
print('Server:[%s]' % data)
return sock
def dispacher(self, conn):
thread.start_new_thread(self.recv_thread,(conn,))
thread.start_new_thread(self.send_thread,(conn,))
def send_thread(self, conn):
screen_size = pyautogui.size()
conn.send(pickle.dumps(screen_size))
def recv_thread(self, conn):
pass
if __name__ == '__main__':
TeamVieverClient()
我在这里发送信息。 问题是正在建立连接,但tkinter窗口没有打开。 有人可以帮忙吗?预先感谢。