简体   繁体   English

PyQt4添加标签loadUi

[英]PyQt4 add label loadUi

import sys
from PyQt4 import QtCore, QtGui, uic


class MainWindow(QtGui.QMainWindow):
     def __init__(self):
         QtGui.QMainWindow.__init__(self)

         self.Dynamic_log = uic.loadUi("Dynamic_log.ui")
         self.Dynamic_log.show()

         self.Main_Window = uic.loadUi("Main_Window.ui")
         self.Main_Window.hide()


         self.Dynamic_log.Continue_Button.clicked.connect(self.Continue2)

     def Continue2(self):

         ***self.Main_Window.addWidget(self.progress_label)***

         self.Main_Window.show()
         self.Dynamic_log.hide()



app = QtGui.QApplication(sys.argv)
window = MainWindow()
sys.exit(app.exec_())

The triple asterisk is where I am stuck. 三重星号是我遇到的问题。 This line of code explains what I would like to achieve but returns an error instead. 这行代码说明了我要实现的目标,但返回一个错误。 I would like to load .ui files using this method and then still be able to add more tools, progressbars, labels buttons etc ... into Main_Window afterwards. 我想使用此方法加载.ui文件,然后仍然可以在之后向Main_Window添加更多工具,进度条,标签按钮等。

My reason for wanting the .ui files to load this way that it is easier to layout and make changes 我希望以这种方式加载.ui文件的原因是,它更易于布局和更改

My reason for needing to add custom tools afterwards is because i intend to run threads, the quantity being at a users discretion and duplicate tools must be made to match the number of threads. 我之所以需要在之后添加自定义工具,是因为我打算运行线程,数量由用户决定,并且必须制作重复的工具以匹配线程数。

After hunting for the answer without any luck, I've solved this myself. 在没有任何运气的情况下寻找答案之后,我自己解决了这个问题。

After creating a QVBoxLayout within the pyqt4 Designer file (Main_Window), i was then able to refer to it (refer to its name which in this case is ProgressThreads) the same way you would without using the uic.loadUi import method. 在pyqt4 Designer文件(Main_Window)中创建了QVBoxLayout之后,我便能够以与不使用uic.loadUi导入方法相同的方式来引用它(在本例中为它的名称,它是ProgressThreads)。

the working code reads as follows, 工作代码如下:

class MyWindow(QWidget): 
    def __init__(self, *args): 
        QWidget.__init__(self, *args)

        self.Dynamic_log = uic.loadUi("Dynamic_log.ui")
        self.Dynamic_log.show()

        self.Main_Window = uic.loadUi("Main_Window.ui")
        self.Main_Window.hide()


        layout = self.Main_Window.ProgressThreads
        self.progress_label = QLabel(" ")
        layout.addWidget(self.progress_label)
        self.progress_label.setText('0%')




if __name__ == "__main__": 
    app = QtGui.QApplication(sys.argv)
    window = MyWindow() 
    sys.exit(app.exec_())

It was also nessesary to change from a QMainWindow to a QWidget 从QMainWindow更改为QWidget也是必要的

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

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