gpt4 book ai didi

python - 从线程(concurrent.futures)更改小部件的可见性,在正常模式下不起作用,在 Debug模式下它可以工作,为什么?

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

我想改变我的图形用户界面中矩形的可见性。因此我在线程中使用了无限的 while 但它不起作用(仅在 Debug模式下)。有人回答吗?

你能帮我完成我的想法,或者给我一些建议,让我以其他方式完成这项工作吗?

最后,我想检查以太网连接并用绿色或红色矩形显示它。为了进行测试,我向服务器发送指定的消息,服务器发回答案。

我的代码:

import sys
from PyQt5 import QtWidgets, QtGui
from concurrent import futures
from time import sleep


class MainApplication(QtWidgets.QWidget):
def __init__(self, parent=None):
super(MainApplication, self).__init__(parent)

# configure the main window
self.setWindowTitle('Msg Tool')
self.resize(230, 330)

# Check if ethernet is ok
self.close_connection_test = [0]

self.e = futures.ThreadPoolExecutor(max_workers=1)
self.e.submit(self.connection_test)

# RedRect
self.red_rect = RedRect(self)
self.red_rect.move(180, 12.5)
self.red_rect.setVisible(True)

# GreenRect
self.green_rect = GreenRect(self)
self.green_rect.move(180, 12.5)
self.green_rect.setVisible(False)

# Function called at the end of the application
def closeEvent(self, event):
self.close_connection_test = [1] # To stop the endless loop in the task
self.e.shutdown() # Shutdown the executer

def connection_test(self):
i = 0
while True:
i += 1
if self.close_connection_test == [1]:
break
if (i % 2) == 0:
self.red_rect.setVisible(False)
else:
self.red_rect.setVisible(True)
# self.red_rect.setVisible(False)
print('Test')
sleep(1)


class RedRect(QtWidgets.QWidget):
def __init__(self, parent=None):
super(RedRect, self).__init__(parent)
self.pen = QtGui.QPen(QtGui.QColor(0, 0, 0))
self.pen.setWidth(1)
self.brush = QtGui.QBrush(QtGui.QColor(255, 48, 48))

def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.setPen(self.pen)
painter.setBrush(self.brush)
painter.drawRect(0, 0, 15, 15)


class GreenRect(QtWidgets.QWidget):
def __init__(self, parent=None):
super(GreenRect, self).__init__(parent)
self.pen = QtGui.QPen(QtGui.QColor(0, 0, 0))
self.pen.setWidth(1)
self.brush = QtGui.QBrush(QtGui.QColor(124, 252, 0))

def paintEvent(self, event):
painter = QtGui.QPainter(self)
painter.setPen(self.pen)
painter.setBrush(self.brush)
painter.drawRect(0, 0, 15, 15)


Application = QtWidgets.QApplication(sys.argv)
Application.setStyle('Fusion')
inst_MainApplication = MainApplication()
inst_MainApplication.show()

sys.exit(Application.exec_())

最佳答案

首先,您必须在创建 self.red_rect 后使用提交,因为如果您不会看到指示该属性不存在的错误。

class MainApplication(QtWidgets.QWidget):
def __init__(self, parent=None):
super(MainApplication, self).__init__(parent)

# configure the main window
self.setWindowTitle('Msg Tool')
self.resize(230, 330)

# RedRect
self.red_rect = RedRect(self)
self.red_rect.move(180, 12.5)
self.red_rect.setVisible(True)

# GreenRect
self.green_rect = GreenRect(self)
self.green_rect.move(180, 12.5)
self.green_rect.setVisible(False)

# Check if ethernet is ok
self.close_connection_test = [0]
self.e = futures.ThreadPoolExecutor(max_workers=1)
self.e.submit(self.connection_test)

另一方面,问题是您不应该直接从另一个更新 GUI,而应该通过 pyqtSignal、QMetaObject::invokedMethod()、QEvent 或使用 QTimer 的最简单方法来更新 GUI: :singleShot()functools.partial():

1。 QTimer

# ...
from functools import partial
from PyQt5 import QtCore, QtGui, QtWidgets
# ...
class MainApplication(QtWidgets.QWidget):
# ...
def connection_test(self):
i = 0
while True:
i += 1
if self.close_connection_test == [1]:
break
isVisible = i % 2 != 0
wrapper = partial(self.red_rect.setVisible, isVisible)
QtCore.QTimer.singleShot(0, wrapper)
print('Test')
sleep(1)

2。 pyqt信号


class MainApplication(QtWidgets.QWidget):
visibilityChanged = QtCore.pyqtSignal(bool)

def __init__(self, parent=None):
# ...


# RedRect
self.red_rect = RedRect(self)
self.red_rect.move(180, 12.5)
self.red_rect.setVisible(True)
self.visibilityChanged.connect(self.red_rect.setVisible)

# ...

def connection_test(self):
i = 0
while True:
i += 1
if self.close_connection_test == [1]:
break
isVisible = i % 2 != 0
self.visibilityChanged.emit(isVisible)
print('Test')
sleep(1)
# ...

3。 QMetaObject::invokedMethod

# ...
def connection_test(self):
i = 0
while True:
i += 1
if self.close_connection_test == [1]:
break
isVisible = i % 2 != 0
QtCore.QMetaObject.invokeMethod(
self.red_rect,
"setVisible",
QtCore.Qt.QueuedConnection,
QtCore.Q_ARG(bool, isVisible),
)
print("Test")
sleep(1)
# ...

4。 QEvent

class CustomEvent(QtCore.QEvent):
_type = QtCore.QEvent.User

def __init__(self, visibility):
super(CustomEvent, self).__init__(CustomEvent._type)
self._visibility = visibility

@property
def visibility(self):
return self._visibility

class MainApplication(QtWidgets.QWidget):
# ...

def connection_test(self):
i = 0
while True:
i += 1
if self.close_connection_test == [1]:
break
isVisible = i % 2 != 0
event = CustomEvent(isVisible)
QtCore.QCoreApplication.postEvent(self, event)
print("Test")
sleep(1)

def event(self, e):
if e.type() == CustomEvent._type:
self.red_rect.setVisible(e.visibility)
return True
return super(MainApplication, self).event(e)

关于python - 从线程(concurrent.futures)更改小部件的可见性,在正常模式下不起作用,在 Debug模式下它可以工作,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55670536/

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