python - Python程序子类化一个PyQt5窗口不能在函数中设置window.title
问题描述
我有一个示例 Python 程序,它对 PyQt5 窗口进行子类化。我正在自学,还有一些新的 PyQt5 和 python 类。
该程序执行我想做的事情我遇到了一个我不知道如何修复的错误。要按原样启动此程序功能,我目前正在学习如何运行Threads
. 我导入并分类了 PyQt5 窗口。在__init__
子类的部分我可以设置窗口标题,它工作正常。
如果我将语句移动到一个函数中"def initUI(self):"
,我将无法设置窗口标题,请注意,我已经尝试了该语句的各种版本,但没有任何效果。这是 def 的第一行,我已将其注释掉。
我的问题是:
此属性是否可在定义中设置?
如果是 io 语句的正确格式是什么。
from PyQt5.QtCore import pyqtSlot, pyqtSignal
from PyQt5 import QtCore, QtGui, QtWidgets
from Threads import Ui_MainWindow
import sys, time
from time import sleep
class MainWindow_EXEC():
def __init__(self): # This section has to be laid out this way
app = QtWidgets.QApplication(sys.argv)
win = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow()
self.ui.setupUi(win)
self.initUI() # Inits that need to happen before start up
win.setWindowTitle("This is the Title!") # Works fine
win.resize(800,600)
win.show()
sys.exit(app.exec_())
def initUI(self): # Button assignments
#self.ui.setWindowTitle("This is the Title!") # AttributeError: 'Ui_MainWindow' object has no attribute 'setWindowTitle'
self.ui.btn_Start.clicked.connect(self.start_progressbar)
self.ui.btn_Stop.clicked.connect(self.stop_progressbar)
self.ui.btn_Reset.clicked.connect(self.reset_progressbar)
self.progress_value = 0
self.stop_progress = False
def progressbar_counter(self, start_value=0):
# have to use member: self.run_thread NOT local var: run_thread
self.run_thread = RunThread(parent=None, counter_start=start_value)
self.run_thread.start()
self.run_thread.counter_value.connect(self.get_thread_value)
def get_thread_value(self, counter): #This updates the progress bar
print(counter)
if not self.stop_progress:
self.ui.progressBar.setValue(counter)
def start_progressbar(self): # This is the button that starts the progress bar
self.stop_progress = False # This is a switch
self.progress_value = self.ui.progressBar.value() # Updating the progress bar
self.progressbar_counter(self.progress_value)
def stop_progressbar(self): # This is a button to stop the progress bar
self.stop_progress = True
self.run_thread.stop()
def reset_progressbar(self): # This is a button to reset the progress bar
self.stop_progressbar()
self.progress_value = 0
self.stop_progress = False
self.ui.progressBar.reset()
class RunThread(QtCore.QThread):
counter_value = QtCore.pyqtSignal(int) # define new Signal
def __init__(self, parent=None, counter_start=0):
super(RunThread, self).__init__(parent)
self.counter = counter_start
self.isRunning = True
def run(self):
while self.counter < 100 and self.isRunning == True:
sleep(0.1)
self.counter += 1
print(self.counter)
self.counter_value.emit(self.counter) # emit new Signal with value
def stop(self):
self.isRunning = False
print('stopping thread...')
self.terminate()
if __name__ == "__main__":
MainWindow_EXEC()
解决方案
必须区分以下类别的目标:
- QMainWindow 是一个具有 setWindowTitle 方法的小部件。
- Ui_MainWindow 不是一个小部件,而是一个用于填充小部件的类,因此它没有 setWindowTitle 方法。
解决方案是让 win 成为一个类成员,然后使用该对象修改标题:
class MainWindow_EXEC():
def __init__(self): # This section has to be laid out this way
app = QtWidgets.QApplication(sys.argv)
self.win = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow()
self.ui.setupUi(self.win)
self.initUI()
self.win.setWindowTitle("This is the Title!")
self.win.resize(800,600)
self.win.show()
sys.exit(app.exec_())
def initUI(self): # Button assignments
self.win.setWindowTitle("This is the Title!")
self.ui.btn_Start.clicked.connect(self.start_progressbar)
# ...
推荐阅读
- java - Beam WordCount Docker 映像无法在 Flink Runner 上启动,但 AvroCoderregistrar 不是 SubType 异常
- .net-core - 'reportgenerator' 不是内部或外部命令、可运行程序或批处理文件
- python - 如何在熊猫中未选择的情况下获取行?
- python - 做一个负数正python
- r - 如何将单个列表转换为 R 中用于大数据的组合数据框?
- dask - 从自定义多处理解决方案迁移到 dask
- vue.js - 创建 vue.js 全局函数
- html - 如图所示,如何在矩形上制作梯形阴影?
- android - 为空或任何验证时如何更改 TextInputLayout 边框颜色
- amazon-web-services - 在 Sagemaker Jupyter Notebook 上使用 ffprobe:/bin/sh: 1: ffprobe: not found