• <small id='FKVL5'></small><noframes id='FKVL5'>

    <legend id='FKVL5'><style id='FKVL5'><dir id='FKVL5'><q id='FKVL5'></q></dir></style></legend>
    1. <tfoot id='FKVL5'></tfoot>
      • <bdo id='FKVL5'></bdo><ul id='FKVL5'></ul>
      <i id='FKVL5'><tr id='FKVL5'><dt id='FKVL5'><q id='FKVL5'><span id='FKVL5'><b id='FKVL5'><form id='FKVL5'><ins id='FKVL5'></ins><ul id='FKVL5'></ul><sub id='FKVL5'></sub></form><legend id='FKVL5'></legend><bdo id='FKVL5'><pre id='FKVL5'><center id='FKVL5'></center></pre></bdo></b><th id='FKVL5'></th></span></q></dt></tr></i><div id='FKVL5'><tfoot id='FKVL5'></tfoot><dl id='FKVL5'><fieldset id='FKVL5'></fieldset></dl></div>

        从不同文件中的其他类更改标签

        时间:2023-08-04

            1. <i id='nU7tW'><tr id='nU7tW'><dt id='nU7tW'><q id='nU7tW'><span id='nU7tW'><b id='nU7tW'><form id='nU7tW'><ins id='nU7tW'></ins><ul id='nU7tW'></ul><sub id='nU7tW'></sub></form><legend id='nU7tW'></legend><bdo id='nU7tW'><pre id='nU7tW'><center id='nU7tW'></center></pre></bdo></b><th id='nU7tW'></th></span></q></dt></tr></i><div id='nU7tW'><tfoot id='nU7tW'></tfoot><dl id='nU7tW'><fieldset id='nU7tW'></fieldset></dl></div>
                <tbody id='nU7tW'></tbody>

                <bdo id='nU7tW'></bdo><ul id='nU7tW'></ul>
                <legend id='nU7tW'><style id='nU7tW'><dir id='nU7tW'><q id='nU7tW'></q></dir></style></legend>
              • <tfoot id='nU7tW'></tfoot>

                  <small id='nU7tW'></small><noframes id='nU7tW'>

                  本文介绍了从不同文件中的其他类更改标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在创建一个应用程序,其中有一个带有标签的主窗口,然后是另一个文件中的停靠小部件.我想从停靠小部件上的按钮更改主窗口标签.我尝试导入主窗口文件,但无法访问标签.而且我还尝试在主窗口中调用一个函数来更改标签,但标签不会改变.代码如下:

                  I am creating an application where I have a main window whit a label and then a docked widget that is in another file. I want to change the main windows label from a button at the docked widget. I try to import the main window file but then I can not access to the label. And I also tried to call a function in the main windows that changes the label but then the label does not change. Here is the code:

                  main_window.py:

                  main_window.py:

                  import results_window
                  
                  class MainWindow(QMainWindow):
                  
                      def __init__(self):
                          super(MainWindow, self).__init__()
                  
                          self.define_main_windows()
                  
                          self.create_dock_widgets()
                  
                      def define_main_windows(self):
                          # Define de Main window properties
                          self.setMinimumSize(QSize(300, 100))    
                          self.setWindowTitle("Python SkyLibris") 
                          self.setWindowIcon(QtGui.QIcon("skylibris_icon.png"))
                          self.setStyleSheet("QMainWindow {background: 'white';}")
                  
                          self.top = 50
                          self.left = 0
                          self.width = 1300
                          self.height = 400
                          self.setGeometry(self.left, self.top, self.width, self.height)
                  
                          self.result = QLabel("result:")
                          self.setCentralWidget(self.result)
                  
                  
                      def create_dock_widgets(self):
                          # Create dock widgets
                          self.results_window = results_window.results_window()
                          self.resultsWindowDock = QDockWidget("Results Viewer", self)
                          self.resultsWindowDock.setWidget(self.results_window )
                          self.resultsWindowDock.setFloating(False)
                          self.resultsWindowDock.setVisible(True)
                          self.addDockWidget(Qt.LeftDockWidgetArea, self.resultsWindowDock)
                  
                  if __name__ == "__main__":
                      app = QtWidgets.QApplication(sys.argv)
                      app.setStyle('Fusion')
                      mainWin = MainWindow()
                      mainWin.show()
                      sys.exit(app.exec_())
                  

                  results_window.py:

                  results_window.py:

                  import main_window
                  
                  class results_window(QWidget):
                  
                      def __init__(self):
                          super(results_window, self).__init__()
                          print("init")
                  
                          self.label = QLabel()
                          self.value = QLineEdit()
                  
                          self.bt = QPushButton("Click")
                          self.bt.clicked.connect(self.clickMethod)
                  
                  
                          self.main_layout = QVBoxLayout()
                          self.main_layout.addWidget(self.label)
                          self.main_layout.addWidget(self.value)
                          self.main_layout.addWidget(self.bt)
                  
                          self.setLayout(self.main_layout)
                  
                      def clickMethod(self):
                          print(self.value.text())
                          text = self.value.text()
                          main_window.result.setText(text)
                  

                  推荐答案

                  你使用了错误的工具,例如你的代码有一个循环导入导致你的应用程序关闭,因为它相当于一个 while True.

                  You are using the wrong tools, for example your code has a circular import that causes your application to close since it is equivalent to a while True.

                  在 Qt 中,信号和槽用于异步共享数据,并有助于实现类之间不存在耦合的事实.在您的情况下,Results_Window 必须具有将该信息传输到 MainWindow 的信号,该信号必须在 clickMethod 内发出.

                  In Qt, signals and slots are used to share data asynchronously, as well as contributing to the fact that there is no coupling between classes. In your case, Results_Window must have a signal that transmits that information to the MainWindow, this signal must be emit within clickMethod.

                  results_window.py

                  from PyQt5 import QtCore, QtWidgets
                  
                  class Results_Window(QtWidgets.QWidget):
                      resultChanged = QtCore.pyqtSignal(str)
                  
                      def __init__(self):
                          super(Results_Window, self).__init__()
                          print("init")
                  
                          self.label = QtWidgets.QLabel()
                          self.value = QtWidgets.QLineEdit()
                  
                          self.bt = QtWidgets.QPushButton("Click")
                          self.bt.clicked.connect(self.clickMethod)
                  
                          main_layout = QtWidgets.QVBoxLayout(self)
                          main_layout.addWidget(self.label)
                          main_layout.addWidget(self.value)
                          main_layout.addWidget(self.bt)
                  
                      @QtCore.pyqtSlot()
                      def clickMethod(self):
                          text = self.value.text()
                          self.resultChanged.emit(text)
                  
                  if __name__ == "__main__":
                      import sys
                      app = QtWidgets.QApplication(sys.argv)
                      app.setStyle('Fusion')
                      w = Results_Window()
                      w.show()
                      sys.exit(app.exec_())
                  

                  ma​​in_window.py

                  from PyQt5 import QtCore, QtGui, QtWidgets
                  import results_window
                  
                  class MainWindow(QtWidgets.QMainWindow):
                      def __init__(self):
                          super(MainWindow, self).__init__()
                          self.define_main_windows()
                          self.create_dock_widgets()
                  
                      def define_main_windows(self):
                          self.setMinimumSize(QtCore.QSize(300, 100))    
                          self.setWindowTitle("Python SkyLibris") 
                          self.setWindowIcon(QtGui.QIcon("skylibris_icon.png"))
                          self.setStyleSheet("QMainWindow {background: 'white';}")
                          top, left, width, height = 50, 0, 1300, 400
                          self.setGeometry(left, top, width, height)
                          self.result = QtWidgets.QLabel("result:")
                          self.setCentralWidget(self.result)
                  
                      def create_dock_widgets(self):
                          self.results_window = results_window.Results_Window()
                          self.results_window.resultChanged.connect(self.result.setText)
                          self.resultsWindowDock = QtWidgets.QDockWidget("Results Viewer", self)
                          self.resultsWindowDock.setWidget(self.results_window )
                          self.resultsWindowDock.setFloating(False)
                          self.resultsWindowDock.setVisible(True)
                          self.addDockWidget(QtCore.Qt.LeftDockWidgetArea, self.resultsWindowDock)
                  
                  if __name__ == "__main__":
                      import sys
                      app = QtWidgets.QApplication(sys.argv)
                      app.setStyle('Fusion')
                      mainWin = MainWindow()
                      mainWin.show()
                      sys.exit(app.exec_())
                  

                  这篇关于从不同文件中的其他类更改标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:装饰器添加了一个意想不到的参数 下一篇:如何在不影响 Pyqt5 中的小部件的情况下将背景图

                  相关文章

                  最新文章

                • <small id='txv6U'></small><noframes id='txv6U'>

                      <tfoot id='txv6U'></tfoot>

                      <legend id='txv6U'><style id='txv6U'><dir id='txv6U'><q id='txv6U'></q></dir></style></legend>

                        <bdo id='txv6U'></bdo><ul id='txv6U'></ul>
                      <i id='txv6U'><tr id='txv6U'><dt id='txv6U'><q id='txv6U'><span id='txv6U'><b id='txv6U'><form id='txv6U'><ins id='txv6U'></ins><ul id='txv6U'></ul><sub id='txv6U'></sub></form><legend id='txv6U'></legend><bdo id='txv6U'><pre id='txv6U'><center id='txv6U'></center></pre></bdo></b><th id='txv6U'></th></span></q></dt></tr></i><div id='txv6U'><tfoot id='txv6U'></tfoot><dl id='txv6U'><fieldset id='txv6U'></fieldset></dl></div>