[英]How to see a file download progress without the GUI freezing (python 3.4, pyQt5) using QThread
[英]How to redirect Python script output to PyQT5 GUI Console, without any buttons clicks?
嘿,我正在使用相同的舊代碼來獲取所有代碼輸出 PyQT5 QTexBrowser GUI 控制台,但我還在所有新行中獲取下一行字符(“\ n”)。
代碼 URL - pipe output 的 sys.stdout 到 ZAA78521B1A359C17 中的文本瀏覽器
下面是我從上面使用的代碼 URL -
class Port(object):
def __init__(self, view):
self.view = view
def write(self, *args):
# self.view.append(str([*args]))
# self.view.append(str(*args).rstrip("\n").lstrip("\n"))
self.view.append(*args)
有什么方法可以從參數中刪除"\n"
,請建議。
當我使用下面的行時,我能夠看到每個新參數中都有"\n"
。
self.view.append(str([*args]))
帶有可見"\n"
的 GUI 控制台 Output(* 忽略行中的數字作為第一個字符 - 這是用戶選擇 output 的一部分。)
然后我嘗試了所有可能的方法來刪除像strip(),lstrip()等,仍然沒有成功。
下面是代碼的一部分,我如何將運行時 output 重定向到QtextBrowser
self.execution_result = QtWidgets.QWidget()
self.execution_result.setObjectName("execution_result")
self.execution_rslt_saveresult_btn = QtWidgets.QPushButton(self.execution_result)
self.execution_rslt_saveresult_btn.setGeometry(QtCore.QRect(1220, 780, 131, 25))
self.execution_rslt_saveresult_btn.setObjectName("execution_rslt_saveresult_btn")
# ------------------------------------------------------------------------------------------------------#
# Console Output
# ------------------------------------------------------------------------------------------------------#
self.execution_rslt_textbrowser = QtWidgets.QTextBrowser(self.execution_result)
self.execution_rslt_textbrowser.setGeometry(QtCore.QRect(2, 1, 1371, 771))
self.execution_rslt_textbrowser.setObjectName("execution_rslt_textbrowser")
self.execution_rslt_textbrowser.setStyleSheet(
"""QTextBrowser {background-color: #000000;
color: #00FF00;
font: 11pt Courier new;}""")
self.execution_rslt_textbrowser.document().setPlainText(
"-" * 40 + '\n' + " Log starts from below line " + '\n' + "-" * 40)
# ------------------------------------------------------------------------------------------------------#
# Below line is sending run time output to GUI console
# ------------------------------------------------------------------------------------------------------#
sys.stdout = Port(self.execution_rslt_textbrowser)
請任何人建議我應該怎么做才能從輸出行中刪除"\n"
。 還有如何刪除錯誤AttributeError: 'port' object has no attribute 'flush'
下面是帶有下一行字符問題的工作示例代碼。 - 供參考/調試
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
class Port(object):
def __init__(self, view):
self.view = view
def flush(self):
pass
def write(self, *args):
# self.view.append(str([*args])) # This line will highlight next line character in console "\n"
self.view.append(*args)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
# MainWindow.setEnabled(True)
MainWindow.resize(1000, 500)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
MainWindow.setSizePolicy(sizePolicy)
MainWindow.setMinimumSize(QtCore.QSize(1000, 500))
MainWindow.setMaximumSize(QtCore.QSize(1000, 500))
MainWindow.setBaseSize(QtCore.QSize(1000, 500))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setMinimumSize(QtCore.QSize(0, 883))
self.centralwidget.setObjectName("centralwidget")
self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
self.tabWidget.setGeometry(QtCore.QRect(280, 0, 671, 341))
self.tabWidget.setTabPosition(QtWidgets.QTabWidget.North)
self.tabWidget.setTabShape(QtWidgets.QTabWidget.Rounded)
self.tabWidget.setDocumentMode(False)
self.tabWidget.setTabsClosable(False)
self.tabWidget.setObjectName("tabWidget")
self.execution_result = QtWidgets.QWidget()
self.execution_result.setObjectName("execution_result")
self.execution_rslt_textbrowser = QtWidgets.QTextBrowser(self.execution_result)
self.execution_rslt_textbrowser.setGeometry(QtCore.QRect(1, 1, 681, 321))
self.execution_rslt_textbrowser.setObjectName("execution_rslt_textbrowser")
self.execution_rslt_textbrowser.document().setPlainText(
"-" * 40 + '\n' + " Log starts from below line " + '\n' + "-" * 40)
# ----------------------------------------------------------------------------#
# Console Output Redirection
# ----------------------------------------------------------------------------#
sys.stdout = Port(self.execution_rslt_textbrowser)
# ----------------------------------------------------------------------------#
self.tabWidget.addTab(self.execution_result, "")
self.mainwindow_list = QtWidgets.QListWidget(self.centralwidget)
self.mainwindow_list.setGeometry(QtCore.QRect(20, 30, 241, 301))
self.mainwindow_list.setObjectName("mainwindow_list")
self.mainwindow_list.itemClicked['QListWidgetItem*'].connect(self.qlistoptions)
item = QtWidgets.QListWidgetItem()
font = QtGui.QFont()
font.setPointSize(9)
item.setFont(font)
self.mainwindow_list.addItem(item)
item = QtWidgets.QListWidgetItem()
font = QtGui.QFont()
font.setPointSize(9)
item.setFont(font)
self.mainwindow_list.addItem(item)
item = QtWidgets.QListWidgetItem()
font = QtGui.QFont()
font.setPointSize(9)
item.setFont(font)
self.mainwindow_list.addItem(item)
item = QtWidgets.QListWidgetItem()
font = QtGui.QFont()
font.setPointSize(9)
item.setFont(font)
self.mainwindow_list.addItem(item)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
self.tabWidget.setCurrentIndex(0)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.tabWidget.setTabText(self.tabWidget.indexOf(self.execution_result), _translate("MainWindow", "Execution Console"))
__sortingEnabled = self.mainwindow_list.isSortingEnabled()
self.mainwindow_list.setSortingEnabled(False)
item = self.mainwindow_list.item(0)
item.setText(_translate("MainWindow", "Item1"))
item = self.mainwindow_list.item(1)
item.setText(_translate("MainWindow", "Item2"))
item = self.mainwindow_list.item(2)
item.setText(_translate("MainWindow", "Item3"))
item = self.mainwindow_list.item(3)
item.setText(_translate("MainWindow", "Item4"))
self.mainwindow_list.setSortingEnabled(__sortingEnabled)
def qlistoptions(self):
print("Inside qlistoptions method : Button Clicked")
# window.hide() ## This to close main window and open new window.
print(self.mainwindow_list.currentRow()) # Will Return Index number of list item
print(self.mainwindow_list.currentItem().text()) # Will Return current selected text from list item
# print("Signal-", self.mainwindow_ok.isChecked())
# Item1
if self.mainwindow_list.currentRow() == 0:
print("Item1 is Clicked!!")
# Item2
elif self.mainwindow_list.currentRow() == 1:
print("Item2 is Clicked!!")
# Item3
elif self.mainwindow_list.currentRow() == 2:
print("Item3 is Clicked!!")
# Item4
elif self.mainwindow_list.currentRow() == 3:
print("Item4 is Clicked!!")
else:
print("Please select any item first!!")
def printText(self, button):
print("Value from printText Method -")
print(button.text())
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
append方法增加了一個額外的endline,解決方法是插入文本而不添加它:
class Port(object):
def __init__(self, view):
self.view = view
def flush(self):
pass
def write(self, text):
cursor = self.view.textCursor()
cursor.movePosition(QtGui.QTextCursor.End)
cursor.insertText(text)
self.view.setTextCursor(cursor)
self.view.ensureCursorVisible()
我用命令寫: sys.stdout.write("Append a line to console!")
,一切都很好,沒有多余的。
要刪除刷新屬性錯誤,只需將這個覆蓋 function 添加到您的端口 class:
def flush(self):
pass
經測試!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.