外部模块如何在 Pyqt5 ui 中显示消息?
How an external module can display a message in Pyqt5 ui?
我正在用 Python 3.7 PyQt5 开发 UI。
这个 UI 有几个执行某些方法的按钮。
我创建了一个名为 «mymodules.py» 的单独模块,用于存储一些方法。我的 UI 的按钮正在调用这些外部方法。
这些方法有时会失败,我在日志控制台中显示错误消息。
我想在我的 UI.
上显示这些错误消息
请问我该怎么做?由于我的模块无法访问我的 UI.
的元素
您可以使用下面的代码轻松重现此场景,复制并粘贴到 2 个单独的文件中(一个用于 UI,一个用于 'mymodules')
代码:
#main UI
import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, QPushButton, QToolBar, QAction, QCheckBox, QStatusBar
from PyQt5.QtCore import Qt, QSize
import mymodules2
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setWindowTitle("My Awesome App")
label = QLabel("THIS IS AWESOME!!!")
label.setAlignment(Qt.AlignCenter)
self.setCentralWidget(label)
toolbar = QToolBar("My main toolbar")
toolbar.setIconSize(QSize(16,16))
self.addToolBar(toolbar)
button_action = QAction(QIcon("bug.png"), "Your button", self)
button_action.setStatusTip("This is your button")
button_action.triggered.connect(self.onMyToolBarButtonClick)
button_action.setCheckable(True)
toolbar.addAction(button_action)
def onMyToolBarButtonClick(self):
"""
This method import mymodules.py and execute MyPersonalMethod()
"""
mymodules2.MyPersonalMethod("https://google.com")
# I need this method above to display in label UI "label" any error message produced by the method
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
mymodules2.py
import webbrowser
def MyPersonalMethod(url):
#DO 1 thing
try:
chrome_path = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
webbrowser.register('chrome', None, webbrowser.BackgroundBrowser(chrome_path))
webbrowser.get('chrome').open_new_tab(url)
except Exception as ex:
print(f"error : {ex}")
#Here I would like to return the message error in the label of ui in order to display it immediately
# Do a second thing whatever happened before
try:
print("We suppose to have open google.com!?")
except Exception as ex:
print(f"error : {ex}")
最简单的方法是将标签传递给函数:
button_action.triggered.connect(lambda:self.onMyToolBarButtonClick(label))
更改触发函数:
def onMyToolBarButtonClick(self,label):
mymodules2.MyPersonalMethod("https://google.com",label)
最后像这样在你的模块中使用它:
def MyPersonalMethod(url,label):
try:
print("Trying")
label.setText("Success")
except Exception as ex:
print(f"error : {ex}")
label.setText(f"error : {ex}")
我正在用 Python 3.7 PyQt5 开发 UI。
这个 UI 有几个执行某些方法的按钮。
我创建了一个名为 «mymodules.py» 的单独模块,用于存储一些方法。我的 UI 的按钮正在调用这些外部方法。
这些方法有时会失败,我在日志控制台中显示错误消息。 我想在我的 UI.
上显示这些错误消息请问我该怎么做?由于我的模块无法访问我的 UI.
的元素您可以使用下面的代码轻松重现此场景,复制并粘贴到 2 个单独的文件中(一个用于 UI,一个用于 'mymodules')
代码:
#main UI
import sys
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QLabel, QMainWindow, QPushButton, QToolBar, QAction, QCheckBox, QStatusBar
from PyQt5.QtCore import Qt, QSize
import mymodules2
class MainWindow(QMainWindow):
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setWindowTitle("My Awesome App")
label = QLabel("THIS IS AWESOME!!!")
label.setAlignment(Qt.AlignCenter)
self.setCentralWidget(label)
toolbar = QToolBar("My main toolbar")
toolbar.setIconSize(QSize(16,16))
self.addToolBar(toolbar)
button_action = QAction(QIcon("bug.png"), "Your button", self)
button_action.setStatusTip("This is your button")
button_action.triggered.connect(self.onMyToolBarButtonClick)
button_action.setCheckable(True)
toolbar.addAction(button_action)
def onMyToolBarButtonClick(self):
"""
This method import mymodules.py and execute MyPersonalMethod()
"""
mymodules2.MyPersonalMethod("https://google.com")
# I need this method above to display in label UI "label" any error message produced by the method
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec_()
mymodules2.py
import webbrowser
def MyPersonalMethod(url):
#DO 1 thing
try:
chrome_path = "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe"
webbrowser.register('chrome', None, webbrowser.BackgroundBrowser(chrome_path))
webbrowser.get('chrome').open_new_tab(url)
except Exception as ex:
print(f"error : {ex}")
#Here I would like to return the message error in the label of ui in order to display it immediately
# Do a second thing whatever happened before
try:
print("We suppose to have open google.com!?")
except Exception as ex:
print(f"error : {ex}")
最简单的方法是将标签传递给函数:
button_action.triggered.connect(lambda:self.onMyToolBarButtonClick(label))
更改触发函数:
def onMyToolBarButtonClick(self,label):
mymodules2.MyPersonalMethod("https://google.com",label)
最后像这样在你的模块中使用它:
def MyPersonalMethod(url,label):
try:
print("Trying")
label.setText("Success")
except Exception as ex:
print(f"error : {ex}")
label.setText(f"error : {ex}")