简体   繁体   English

外部模块如何在 Pyqt5 ui 中显示消息?

[英]How an external module can display a message in Pyqt5 ui?

I am developing a UI with Python 3.7 PyQt5.我正在使用 Python 3.7 PyQt5 开发用户界面。

This UI has several buttons that execute some methods.这个 UI 有几个按钮来执行一些方法。

I create a separate module called « mymodules.py » which stores some methods.我创建了一个名为“mymodules.py”的单独模块,其中存储了一些方法。 The buttons of my UI are calling these external methods.我的 UI 的按钮正在调用这些外部方法。

These methods sometimes failed and I am displaying the error message in the log console.这些方法有时会失败,我在日志控制台中显示错误消息。 I would like to display these error messages on my UI.我想在我的 UI 上显示这些错误消息。

How can I do that please?请问我该怎么做? As my modules can't access the elements of my UI.由于我的模块无法访问我的 UI 的元素。

You can easily reproduce this scenario with this code below to copy-paste in 2 separate files (one for the UI, one for the 'mymodules')您可以使用下面的代码轻松重现此场景,以复制粘贴到 2 个单独的文件中(一个用于 UI,一个用于“mymodules”)

Code:代码:

#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我的模块2.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}")

Simplest method would be to pass label to function:最简单的方法是将 label 传递给 function:

button_action.triggered.connect(lambda:self.onMyToolBarButtonClick(label))

Change the triggered function:更改触发的function:

def onMyToolBarButtonClick(self,label):
    mymodules2.MyPersonalMethod("https://google.com",label)

Finally use it in your module like this:最后在你的模块中使用它,如下所示:

def MyPersonalMethod(url,label):
    try:
        print("Trying")
        label.setText("Success")
    except Exception as ex:
        print(f"error : {ex}")
        label.setText(f"error : {ex}")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM