将以下代码保存为application.py可让我运行一个启动过程,显示一个带QDialog
的简单QLabel
:
python application.py
当进程正在运行并显示对话框时,我想更改默认标题"在彩虹的某个地方"别的东西。如何实现呢?
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
app = QApplication([])
class Dialog(QDialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
self.setLayout(QVBoxLayout())
self.label = QLabel('somewhere over the rainbow')
self.layout().addWidget(self.label)
self.show()
dialog = Dialog()
app.exec_()
以下是将QLabel
的文字设置为新字符串值的示例" 01011001001001110100100110":
答案 0 :(得分:0)
以下是围绕SocketServer
的{{1}}和TPCServer
类设计的有效解决方案。
此实施使用BaseRequestHandler
类。
QThread
application.py
import SocketServer
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
IP, PORT = '127.0.0.1', 62236
app = QApplication([])
class Emitter(QObject):
signal = pyqtSignal(str)
class Handler(SocketServer.BaseRequestHandler):
emitter = Emitter()
def handle(self):
self.emitter.signal.emit(self.request.recv(1024))
class Thread(QThread):
def __init__(self, target, parent=None):
QThread.__init__(self, parent)
self.target = target
def run(self):
while True:
self.target()
class Dialog(QDialog):
def __init__(self, parent=None):
super(Dialog, self).__init__(parent)
Handler.emitter.signal.connect(self.onEmit)
self.server = SocketServer.TCPServer((IP, PORT), Handler)
thread = Thread(target=self.server.handle_request)
thread.start()
self.setLayout(QVBoxLayout())
self.label = QLabel('somewhere over the rainbow')
self.layout().addWidget(self.label)
self.show()
def closeEvent(self, event):
self.server.server_close()
event.accept()
def onEmit(self, arg):
self.label.setText(str(arg))
dialog = Dialog()
app.exec_()
client.py
使用import socket
IP, PORT = '127.0.0.1', 62236
def send(data):
SOCKET = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
SOCKET.connect((IP, PORT))
SOCKET.send(data)
SOCKET.close()
for i in range(2, 1000):
send('data %07d'%i)
send(raw_input("input:\t"))
使用threading.Thread
方法代替SocketServer.TCPServer.serve_forever
:
SocketServer.TCPServer.handle_request