gpt4 book ai didi

python - 刷新 GUI 窗口

转载 作者:太空宇宙 更新时间:2023-11-03 20:36:17 25 4
gpt4 key购买 nike

我有一个客户端 (C++) 到服务器 (Python) 项目。

客户端向服务器发送一条Message,每个Message包含三个属性。

Gui 应在屏幕上显示所有属性。当客户端发送第一条消息时,一切正常。但是,当他发送下一条消息时,我需要关闭窗口,当我这样做时,它会打开一个新窗口并显示以下消息,如果我不这样做,他就不会显示收到的新消息。

我怀疑 app.exec_() 阻止程序获取像 in this case 这样的新消息,但我不确定。我的问题是:

  1. 真的是这样吗?如果是这样,我应该把线程放在哪里?

  2. 我以前从未使用过QThread,我看到我们正在对Run()进行Override在 Run() 方法中,我们放入了所有逻辑,我应该将哪些逻辑放入我的特定问题中。您可以找到完整的项目 here

相关代码:

def recive(self):

global income
global table2

msg = Payload(0, 0, 0)

while (True):

print('waiting for a connection..')
conn, addr = self.My_socket.accept()
print("connection has been established | " + repr(addr))
logger.info("connection has been established | " + repr(addr))

while conn:
myThread = MyThread()

buff = conn.recv(sizeof(msg))

print("recv %d bytes" % sizeof(msg))
payload_in = Payload.from_buffer_copy(buff)

print(f"Received id={payload_in.id}, counter={payload_in.counter}, opcode={payload_in.opcode}")

payload_out = payload_in

self.opcode = payload_in.opcode

if self.opcode == 1:
self.export()
elif self.opcode == 2:
payload_out.counter += 1
else:
logger.info("Unexpected opcode %d" % self.opcode)

nsent = conn.send(payload_out)
print("send %d bytes" % nsent)
print("send id=%d, counter=%d, opcode=%d" % (payload_out.id,
payload_out.counter,
payload_out.opcode))
self.setNewTable(payload_in, payload_out)

print("Closing connection to client")
print("----------------------------")

sys.exit()

def setNewTable(self,payload_in, payload_out):
self.insertValues(table1, payload_in)
self.insertValues(table2, payload_out)
ex.show()
app.exec_()

def insertValues(self,table, payload):
table.setItem(0, 1, QTableWidgetItem(str(payload.id)))
table.setItem(1, 1, QTableWidgetItem(str(payload.counter)))
table.setItem(2, 1, QTableWidgetItem(str(payload.opcode)))

最佳答案

我知道一些关于 QThread ui 的事情,认为你需要使用 python 信号和插槽来更新 ui。引用以下代码

from PyQt4 import QtCore, QtGui
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import datetime
import sys
import struct
import threading
import os
import datetime
import time
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s

try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class MySignal(QObject):
signelforWritemsg=Signal(str)
class EchoThread(QThread):
#def __init__(self,Inf_found1,inf_cleaned1,Auto_clean,timer,movie,Scan_btn,cancel_btn,status_text):
def __init__(self,parent = None):
QThread.__init__(self,parent)
self.exiting = False
self.signal = MySignal()

def run(self):
while True:
scantimenow= datetime.datetime.now()
scantime=scantimenow.strftime('%Y-%m-%d %H:%M:%S')
print(scantime)
self.signal.signelforWritemsg.emit(scantime)
time.sleep(5)
class Ui_Clientserver(object):
def setupUi(self, Clientserver):
Clientserver.setObjectName(_fromUtf8("Clientserver"))
Clientserver.resize(400, 300)
self.label = QtGui.QLabel(Clientserver)
self.label.setGeometry(QtCore.QRect(150, 70, 200, 200))
self.label.setObjectName(_fromUtf8("label"))
self.retranslateUi(Clientserver)

self.thread = EchoThread()
self.thread.start()
self.thread.signal.signelforWritemsg.connect(self.signelforWritemsg)

def signelforWritemsg(self,data):
self.label.setText(_translate("Clientserver",str(data), None))
def retranslateUi(self, Clientserver):
Clientserver.setWindowTitle(_translate("Clientserver", "Clientserver", None))

self.label.setText(_translate("Clientserver", "inProgess", None))
if __name__ == "__main__":
import sys
QtCore.QCoreApplication.setAttribute(QtCore.Qt.AA_X11InitThreads)
app = QtGui.QApplication(sys.argv)
Clientserver = QtGui.QWidget()
ui = Ui_Clientserver()
ui.setupUi(Clientserver)
Clientserver.show()
sys.exit(app.exec_())

关于python - 刷新 GUI 窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57145313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com