【问题标题】:How can I combine in one file multiple .py and.ui files?如何在一个文件中合并多个 .py 和 .ui 文件?
【发布时间】:2019-08-06 11:04:07
【问题描述】:

我有双语申请。在 QtDesigner 中,我制作了两个界面:gui_eng.ui 和 gui_rus.ui。对于它们中的每一个,都存在 main_gui_eng.py 和 main_gui_rus.py 文件以及这些 .ui 文件的配置代码。更重要的是,存在main.py 文件(根据上次关闭,从main_gui_eng.py 或main_gui_rus.py 开始)。 main.py 将被转换为 .exe 文件。 在每个文件中,我使用以下方式连接 .ui:

main_window = uic.loadUiType('gui_eng.ui')[0]
class WIZWindow(QMainWindow, main_window):   

问题:我怎样才能以这种方式连接它们?如果我在 main.py 中写:

import main_gui_eng

它没有打开需要的 .ui 文件...

【问题讨论】:

  • 您确实意识到这就是翻译功能的全部意义——您不需要完全不同的 UI,您只需要两个翻译功能(每种语言一个),然后依赖于选择的语言访问适当的翻译功能。

标签: python python-3.x pyqt5 exe qt-designer


【解决方案1】:

1 pyuic5 gui_eng.ui -o gui_eng.py

2pyuic5 gui_rus.ui -o gui_rus.py

3python main_eng_rus.py

4pyinstaller -c -F main_eng_rus.py

5main_eng_rus.exe


ma​​in_gui_eng.py

from PyQt5.QtWidgets import QMainWindow, QApplication

# pyuic5 gui_eng.ui -o gui_eng.py
from gui_eng import Ui_MainWindow


class Window(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.parent = parent

        self.setupUi(self)

    def closeEvent(self, event):
        self.hide()
        self.parent.show()


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    w = Window()
    w.setWindowTitle("main_gui_eng.py")
    w.show()
    sys.exit(app.exec_())        

ma​​in_gui_rus.py

from PyQt5.QtWidgets import QMainWindow, QApplication

# pyuic5 gui_rus.ui -o gui_rus.py
from gui_rus import Ui_MainWindow


class Window(QMainWindow, Ui_MainWindow):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.parent = parent 

        self.setupUi(self)

    def closeEvent(self, event):
        self.hide()
        self.parent.show()


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    w = Window()
    w.setWindowTitle("main_gui_rus.py")
    w.show()
    sys.exit(app.exec_())        

ma​​in_eng_rus.py

from PyQt5.QtWidgets import (QMainWindow, QApplication, QPushButton, 
                             QWidget, QGridLayout)

from main_gui_eng import Window as gui_eng
from main_gui_rus import Window as gui_rus


class MainWindow(QMainWindow):
    def __init__(self, parent=None):
        super(MainWindow, self).__init__(parent)

        centralWidget = QWidget()
        self.setCentralWidget(centralWidget)

        self.button_eng = QPushButton('Open main_gui_eng.py')
        self.button_eng.clicked.connect(self.open_gui_eng)
        self.button_rus = QPushButton('Open main_gui_rus.py')
        self.button_rus.clicked.connect(self.open_gui_rus)

        self.grid = QGridLayout(centralWidget)
        self.grid.addWidget(self.button_eng, 0, 0)         
        self.grid.addWidget(self.button_rus, 0, 1)

    def open_gui_eng(self):
        self.mainGuiEng = gui_eng(self)
        self.mainGuiEng.show()
        self.hide()

    def open_gui_rus(self):
        self.mainGuiRus = gui_rus(self)
        self.mainGuiRus.show()
        self.hide()


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)
    w = MainWindow()
    w.setWindowTitle("main_eng_rus.py")
    w.show()
    sys.exit(app.exec_())        

【讨论】:

    猜你喜欢
    • 2021-10-16
    • 1970-01-01
    • 2016-12-09
    • 1970-01-01
    • 2020-07-20
    • 1970-01-01
    • 2020-02-23
    • 2019-12-16
    • 2020-07-10
    相关资源
    最近更新 更多