retranslateUi 在 PyQT 中做什么

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

当运行 pyuic5 命令并从 QT 设计器 UI 文件生成 python 文件时,有一个

retranslateUi
函数。

def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))

此功能是否支持多语言?如果我对多语言不感兴趣,我可以安全地删除此功能吗?

我在循环中添加 GUI 小部件,因此在我的代码中实现此功能会很麻烦。

python qt pyqt
2个回答
1
投票

此函数是为了实现多语言支持逻辑(参见12)。

您可以毫无问题地将其删除。


0
投票

我的代码:

import sys

from window1 import Ui_MainWindow


# from PyQt5.QtWidgets import *

from PyQt5 import QtWidgets, QtCore


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


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    

    window = MainWindow()
    window.show()

    sys.exit(app.exec_())  

window1.py
:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'window1.ui'
#
# Created by: PyQt5 UI code generator 5.15.6
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayoutWidget = QtWidgets.QWidget(self.centralwidget)
        self.gridLayoutWidget.setGeometry(QtCore.QRect(10, 90, 771, 441))
        self.gridLayoutWidget.setObjectName("gridLayoutWidget")
        self.gridLayout = QtWidgets.QGridLayout(self.gridLayoutWidget)
        self.gridLayout.setContentsMargins(0, 0, 0, 0)
        self.gridLayout.setObjectName("gridLayout")
        self.pushButton_4 = QtWidgets.QPushButton(self.gridLayoutWidget)
        self.pushButton_4.setObjectName("pushButton_4")
        self.gridLayout.addWidget(self.pushButton_4, 1, 1, 1, 1)
        self.pushButton_2 = QtWidgets.QPushButton(self.gridLayoutWidget)
        self.pushButton_2.setObjectName("pushButton_2")
        self.gridLayout.addWidget(self.pushButton_2, 0, 1, 1, 1)
        self.pushButton_1 = QtWidgets.QPushButton(self.gridLayoutWidget)
        self.pushButton_1.setObjectName("pushButton_1")
        self.gridLayout.addWidget(self.pushButton_1, 0, 0, 1, 1)
        self.pushButton_3 = QtWidgets.QPushButton(self.gridLayoutWidget)
        self.pushButton_3.setObjectName("pushButton_3")
        self.gridLayout.addWidget(self.pushButton_3, 1, 0, 1, 1)
        self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox.setGeometry(QtCore.QRect(10, 10, 271, 51))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.groupBox.setFont(font)
        self.groupBox.setTitle("")
        self.groupBox.setCheckable(False)
        self.groupBox.setChecked(False)
        self.groupBox.setObjectName("groupBox")
        self.pushButton_eng = QtWidgets.QPushButton(self.groupBox)
        self.pushButton_eng.setGeometry(QtCore.QRect(26, 10, 111, 33))
        self.pushButton_eng.setCheckable(True)
        self.pushButton_eng.setChecked(False)
        self.pushButton_eng.setAutoExclusive(True)
        self.pushButton_eng.setObjectName("pushButton_eng")
        self.pushButton_ita = QtWidgets.QPushButton(self.groupBox)
        self.pushButton_ita.setGeometry(QtCore.QRect(150, 10, 101, 33))
        self.pushButton_ita.setCheckable(True)
        self.pushButton_ita.setChecked(True)
        self.pushButton_ita.setAutoExclusive(True)
        self.pushButton_ita.setObjectName("pushButton_ita")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 29))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

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

    # def retranslateUi(self, MainWindow):
    #     _translate = QtCore.QCoreApplication.translate
    #     MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
    #     self.pushButton_4.setText(_translate("MainWindow", "mucca"))
    #     self.pushButton_2.setText(_translate("MainWindow", "gatto"))
    #     self.pushButton_1.setText(_translate("MainWindow", "cane"))
    #     self.pushButton_3.setText(_translate("MainWindow", "topo"))
    #     self.pushButton_eng.setText(_translate("MainWindow", "ENG"))
    #     self.pushButton_ita.setText(_translate("MainWindow", "ITA"))

只要删除它就可以工作:

  `self.retranslateUi(MainWindow)` line in `def setupUi(self, MainWindow):` too , but you wont get any text in your widgets:

© www.soinside.com 2019 - 2024. All rights reserved.