Python PyQt5 中QtextBrowser打印程序log,只输出一次不知道原因
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python PyQt5 中QtextBrowser打印程序log,只输出一次不知道原因相关的知识,希望对你有一定的参考价值。
from PyQt5 import QtWidgets
from frame.test_ui import Ui_MainWindow
import threading
import importlib
import time
class MyWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def start_test(self):
print(1)
def show_log(self, mes):
def _slot1(textbrowser, mess):
textbrowser.append(mess)
textbrowser.moveCursor(textbrowser.textCursor().End)
time.sleep(0.2)
threading.Thread(target=_slot1, args=(self.log_win, mes)).start()
def start_run(self):
threading.Thread(target=self.start_test).start()
#self.start_test()
self.show_log('22')
如代码,我设定Qtestbrowser打印程序LOG,在其他class中调用show_log:
import MyWindow
class MyLog(object):
my_win = MyWindow()
def print_log(self):
my_win.show_log('wwww')
最后‘wwww’并没有打印,有人知道怎么回事吗?
是log不是logo,你不是程序员吧?
Python 3.5,单独窗口中的 pyqt5 进度条 gui
【中文标题】Python 3.5,单独窗口中的 pyqt5 进度条 gui【英文标题】:Python 3.5, pyqt5 progress bar gui in seperate window 【发布时间】:2016-11-04 23:23:09 【问题描述】:我是 PyQt5 的新手,对 Python 还是很陌生。我正在尝试在 Python 3.5 中使用 PyQt5 创建一个图形用户界面,在其中单击一个按钮以启动一个单独的窗口,其中进度条迭代到 100,然后在迭代结束时关闭窗口以生成一条消息“它工作”。
问题是进度条已创建但没有更新,并且在到达结束后它不显示它工作的消息。当我尝试调试时,它完全崩溃,没有任何警告说明原因。不知道怎么调试代码
我的进度条码如下图:
from PyQt5 import QtCore, QtWidgets
import sys
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(1075, 84)
self.progressBar = QtWidgets.QProgressBar(Form)
self.progressBar.setGeometry(QtCore.QRect(30, 30, 1000, 35))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.progressBar.sizePolicy().hasHeightForWidth())
self.progressBar.setSizePolicy(sizePolicy)
self.progressBar.setMinimumSize(QtCore.QSize(1000, 35))
self.progressBar.setMaximumSize(QtCore.QSize(1000, 35))
self.progressBar.setProperty("value", 0)
self.progressBar.setObjectName("progressBar")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def setValue(self, val):
self.progressBar.setProperty("value", val)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Progress bar"))
下面给出主程序
from PyQt5.QtWidgets import QApplication, QDialog, QWidget, QPushButton, QMessageBox
import ProgressBar
import sys
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'PyQt5 button - pythonspot.com'
self.left = 200
self.top = 200
self.width = 320
self.height = 200
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
button = QPushButton('PyQt5 button', self)
button.setToolTip('This is an example button')
button.move(100, 70)
button.clicked.connect(self.on_click)
self.show()
def on_click(self):
print('PyQt5 button click')
app1 = QApplication(sys.argv)
window = QDialog()
ui = ProgressBar.Ui_Form()
ui.setupUi(window)
window.show()
for i in range(0, 100):
ui.setValue(((i + 1) / 100) * 100)
app1.quit()
QMessageBox.information(self, "Message", "Data Loaded")
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
任何帮助将不胜感激。
【问题讨论】:
【参考方案1】:这是我的最终代码。我试图在使用 pyqt 设计器时包含一些好的做法,而不是直接编辑创建的文件,而是从另一个文件运行它并调用它。当我想按照建议进行更改时,这使事情变得更加容易。我还在代码中加入了 time.sleep(0.1) 来减慢它的速度,这样你就可以看到它正在工作。希望对您有所帮助。
ProgressBar_ui.py - 由 ProgressBar.ui 在 python 中转换生成的文件
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'Base.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(1075, 84)
self.progressBar = QtWidgets.QProgressBar(Form)
self.progressBar.setGeometry(QtCore.QRect(30, 30, 1000, 35))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.progressBar.sizePolicy().hasHeightForWidth())
self.progressBar.setSizePolicy(sizePolicy)
self.progressBar.setMinimumSize(QtCore.QSize(1000, 35))
self.progressBar.setMaximumSize(QtCore.QSize(1000, 35))
self.progressBar.setProperty("value", 0)
self.progressBar.setObjectName("progressBar")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "Progress bar"))
ProgressBar.py - 调用 ProgrssBar.ui
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from ProgressBar_ui import Ui_Form
class ProgressBar(QtWidgets.QDialog, Ui_Form):
def __init__(self, desc = None, parent=None):
super(ProgressBar, self).__init__(parent)
self.setupUi(self)
self.show()
if desc != None:
self.setDescription(desc)
def setValue(self, val): # Sets value
self.progressBar.setProperty("value", val)
def setDescription(self, desc): # Sets Pbar window title
self.setWindowTitle(desc)
def main():
app = QtWidgets.QApplication(sys.argv) # A new instance of QApplication
form = ProgressBar('pbar') # We set the form to be our MainWindow (design)
app.exec_() # and execute the app
if __name__ == '__main__': # if we're running file directly and not importing it
main() # run the main function
Main_program.py - 从这里运行
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox
from ProgressBar import ProgressBar
import sys, time
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'PyQt5 button - pythonspot.com'
self.left = 200
self.top = 200
self.width = 320
self.height = 200
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
button = QPushButton('PyQt5 button', self)
button.setToolTip('This is an example button')
button.move(100, 70)
button.clicked.connect(self.on_click)
self.show()
def on_click(self):
pb = ProgressBar()
for i in range(0, 100):
time.sleep(0.05)
pb.setValue(((i + 1) / 100) * 100)
QApplication.processEvents()
pb.close()
QMessageBox.information(self, "Message", "Data Loaded")
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
【讨论】:
【参考方案2】:最终找到了解决方案。 3个问题:
1) 只应在主应用程序中调用QApplication
一次,而不应再次调用
2) Add QApplication.processEvents()
到 forloop,因此它会不断更新进度条
3) 在 forloop 之后添加 window.close()
使其关闭
【讨论】:
以上是关于Python PyQt5 中QtextBrowser打印程序log,只输出一次不知道原因的主要内容,如果未能解决你的问题,请参考以下文章
python pyqt5 QTableWidget 设置表头
搭建pyqt5开发环境(python3+pycharm2019+pyqt5)