PyQt:从另一个函数访问类之外的函数和自身

问题描述 投票:0回答:1

这是我的问题:我已经使用Qt Design创建了一个GUI,并将* .ui转换为* .py,并将代码移到了我的脚本中。到目前为止,我已经掌握了这一点,而忽略了其他对我尝试执行的操作并不重要的功能:

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.setWindowModality(QtCore.Qt.NonModal)
        MainWindow.resize(951, 487)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(20, 20, 261, 71))
        font = QtGui.QFont()
        font.setPointSize(22)
        self.label.setFont(font)
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 951, 26))
        self.menubar.setObjectName("menubar")
        self.menuFile = QtWidgets.QMenu(self.menubar)
        self.menuFile.setObjectName("menuFile")
        self.menuAbout = QtWidgets.QMenu(self.menubar)
        self.menuAbout.setObjectName("menuAbout")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.actionExit = QtWidgets.QAction(MainWindow)
        self.actionExit.setObjectName("actionExit")
        self.actionAbout = QtWidgets.QAction(MainWindow)
        self.actionAbout.setObjectName("actionAbout")
        self.actionGitHub_Page = QtWidgets.QAction(MainWindow)
        self.actionGitHub_Page.setObjectName("actionGitHub_Page")
        self.menuFile.addAction(self.actionExit)
        self.menuAbout.addAction(self.actionAbout)
        self.menuAbout.addAction(self.actionGitHub_Page)
        self.menubar.addAction(self.menuFile.menuAction())
        self.menubar.addAction(self.menuAbout.menuAction())

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

        self.actionExit.triggered.connect(self.exit)
        self.actionGitHub_Page.triggered.connect(self.GitHub_link)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "GUI"))
        self.label.setText(_translate("MainWindow", "TextLabel"))
        self.menuFile.setTitle(_translate("MainWindow", "File"))
        self.menuAbout.setTitle(_translate("MainWindow", "Help"))
        self.actionExit.setText(_translate("MainWindow", "Exit"))
        self.actionAbout.setText(
            _translate("MainWindow", "About"))
        self.actionGitHub_Page.setText(_translate("MainWindow", "GitHub Page"))

    def exit(self):
        sys.exit()

    def GitHub_link(self):
        os.system(
            "start \"\" https://github.com")

    def text(self, text):
        self.label.setText(text)


class GUIThread(Thread):
    '''
    Provides a dedicated thread for the GUI
    '''

    def __init__(self, name):
        Thread.__init__(self)
        self.name = name

    def run(self):
        global Stopped
        try:
            app = QtWidgets.QApplication([])
            MainWindow = QtWidgets.QMainWindow()
            ui = Ui_MainWindow()
            ui.setupUi(MainWindow)
            MainWindow.show()

            # sys.exit(app.exec_())
            if(app.exec_() == 0):
                Stopped = True

        except:
            print("\n\nThe GUI thread raised an exception. Terminating...\n\n")
            Stopped = True

def main():
    global Stopped

    try:
        # Create a GUI thread to manage the visual stuff
        Gui_thread = GUIThread("GUI")
        Gui_thread.daemon = True
        Gui_thread.start()


        while(True):
            if Stopped == False:
                time.sleep(0.5)
            else:
                print("Closing...")
                sys.exit()

    # If any non-exit exception is raised then the specified thread will be closed
    except Exception as e:
        print(e)
        print("\n\nThe main thread raised an exception. Terminating...\n\n")
        sys.exit()


if __name__ == '__main__':
    # Call main function and pass args given by user
    main()

现在,我需要一种从代码中任何地方更新标签文本的方法,但是我找不到方法来调用该函数

def text(self, text):
        self.label.setText(text)

在课堂之外。例如,如果要从def main()调用它,则需要指定“ self”和“ text”参数,但self在主函数中未定义。

我的主要目标是能够从脚本的几乎任何地方调用def text(self, text)函数并更新标签文本。

我试图从我创建的GUithread中调用它,并执行一会儿循环,但这会阻塞Qt线程并使窗口无法使用,最后终止代码。有没有人可以帮我这个忙?

谢谢:)

python pyqt self-reference
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.