在另一个*.ui-fi的框架/小部件中加载整个*ui文件

2024-06-16 08:30:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在为一个家庭项目开发一个小的用户界面。我用QT设计器创建了一个*.ui文件。这是我的主窗口,有一些导航按钮、标签等等。现在,当我单击导航按钮时,我正努力在主窗口的框架或小部件中加载另一个*.ui(例如包含内容)。我使用的是pyqt4,必须在raspberry pi上用python实现它。在

我也使用了搜索,但我没有找到解决问题的有效方法。也许这真的很容易,但对我来说很难。在

*重要提示:我不想重组按钮和标签!我想在主窗口的小部件或框架中加载整个*.ui文件!在

下面是我的代码示例: 以下是我的主.py在

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.uic import *

def buttonKlick1close():
start.close()

start = loadUi("start.ui")
start.connect(start.pushButton, SIGNAL("clicked()") , buttonKlick1close)

# there are other "signal-connections" for my navigation buttons but I 
# integreated all exactly the same like above. 
# actually when I click to another navigation button it will open an "def" 
# with "window2.showFullscreen()" and so on.... 

start.showFullScreen()
app.exit(app.exec_())

现在,当我点击导航按钮时,它总是打开7-8个窗口,所以我也需要在每个窗口上都有导航按钮。 我的目标是创建一个带有导航按钮和框架/小部件的窗口,在那里我可以升级/加载其他*ui文件。或者没有框架/小部件还有其他好的解决方案吗?在

enter image description here


Tags: 文件fromimport框架appui部件def
1条回答
网友
1楼 · 发布于 2024-06-16 08:30:33

首先,我的解决方案的基础是推广一个widget,为此我将按照以下方式构建项目:

├── main.py
├── pages
│   ├── energypage.py
│   ├── fanpage.py
│   ├── homepage.py
│   └── statuspage.py
└── ui
    ├── energy.ui
    ├── fan.ui
    ├── home.ui
    ├── main.ui
    └── status.ui

页面的.ui将基于Widget模板,但主窗口将使用MainWindow模板(MainWindow允许有工具栏、状态栏、DockWidgets、menuBar等,所以我选择它作为main)。在

由于.ui本身无法升级,因此将创建调用设计并遵循类似结构的类,但您可以添加更多功能,例如主页.py公司名称:

^{pr2}$

在主.ui按钮在左侧,QStackedWidget在右侧:

enter image description here

enter image description here

通过右键单击QStackedWidget打开的菜单的当前页面之后,选择“插入页面”>;来添加每个页面

然后它将被提升为使用页面文件夹中的小部件:

enter image description here

然后在主菜单中将按钮与相应的索引相关联:

主.py

import os
from PyQt4 import QtGui, uic
from functools import partial

current_dir = os.path.dirname(os.path.abspath(__file__))
Form, Base = uic.loadUiType(os.path.join(current_dir, "ui/main.ui"))

class MainWidget(Base, Form):
    def __init__(self, parent=None):
        super(self.__class__, self).__init__(parent)
        self.setupUi(self)
        buttons = (self.homebutton, self.statusbutton, self.fanbutton, self.energybutton)
        for i, button in enumerate(buttons):
            button.clicked.connect(partial(self.stackedWidget.setCurrentIndex, i))

if __name__ == '__main__':
    import sys
    app = QtGui.QApplication(sys.argv)
    app.setStyle("fusion")
    w = MainWidget()
    w.show()
    sys.exit(app.exec_())

完整的例子是here。在

相关问题 更多 >