gpt4 book ai didi

python - 使用 pyqt 的组合框动态更改应用程序的翻译

转载 作者:太空宇宙 更新时间:2023-11-03 14:48:41 25 4
gpt4 key购买 nike

我正在创建一个带有组合框的简单窗口,让用户选择要在整个应用程序中显示的文本语言

我已经创建了必要的 .qm 文件,并且当我启动应用程序时文本正在更新。但是我如何将其链接到组合框上的选项,并从主窗口中动态更改语言?

我的代码:

#!/usr/bin/python
# -*- coding: utf-8 -*-

'''
GUI

'''

import sys
import os.path as osp
import os
from PyQt4 import QtGui, QtCore

class MainWindow(QtGui.QWidget):

def __init__(self):
super(MainWindow,self).__init__()

# Set MainWindow geometry, use settings of last session. If it's first session,
# use defaulted settings
self.settings = QtCore.QSettings('Paul',QtCore.QSettings.NativeFormat)
self.resize(self.settings.value("size", QtCore.QSize(500, 300)).toSize())
self.move(self.settings.value("pos", QtCore.QPoint(5, 5)).toPoint());

self.initUI()


def closeEvent(self, e):
#Save MainWindow geometry session when closing the window
self.settings.setValue("size",self.size())
self.settings.setValue("pos",self.pos())
e.accept()

def initUI(self):

self.hbox = QtGui.QVBoxLayout(self) # Create Vertival box layout to put the buttons
self.myButtons = QtGui.QPushButton('button',self) #create push button
self.myButtons.setStyleSheet("""QPushButton { background-color: red; font:bold 20px}""")
self.myButtons.setToolTip('Push this button')
self.myButtons.setText(self.tr('yes'))
comboBox=QtGui.QComboBox(self) #create drop down menu
comboBox.addItem('English')
comboBox.addItem('Portugues')
self.hbox.addWidget(comboBox,1,QtCore.Qt.AlignRight) #add drop down menu to box layout
self.hbox.addStretch(3) # set separation between buttons
self.hbox.addWidget(self.myButtons,1,QtCore.Qt.AlignRight) #add button to box layout


self.setWindowTitle('Test2')


self.show()



def main():

app = QtGui.QApplication(sys.argv)
translator = QtCore.QTranslator()
translator.load("~/translations/qt_pt.qm")
app.installTranslator(translator)
app.setWindowIcon(QtGui.QIcon(path))
ex = MainWindow()
sys.exit(app.exec_())


if __name__ == '__main__':
main()

基本上我想让组合框做这样的事情:

self.comboBox.currentIndexChanged.connect(self.combochange)

def combochange(self):
if self.comboBox.currentText()=='Portugues':
translator = QtCore.QTranslator()
translator.load('~/translations/qt_pt.qm')
app.installTranslator(translator) #Obviously this doesn't work

我假设我必须以某种方式将参数从 mainWindow 传递到 main() 函数并重新加载所有内容。

这可能吗?

OSX 10.11.6 上的 Python 2.7、QT 5.9.1、PyQt4 4.12.1

编辑:

我找到了this在 QT wiki 页面上发布了我想要的内容,不幸的是我根本不精通 C。如果有人能帮我将其翻译成Python,我将不胜感激。

最佳答案

这是一种更合适的方法:

#create a new translator and load the desired translation file
translator = QtCore.QTranslator()
translator.load("monAppli_fr.qm")

#install translator to the app
#app is the variable created in (if __name__ == "__main__":) section, make it gloabl
app.installTranslator(translator)
#call retranslateUi on ui, which is defined in the same section as app
#MainWindow : created in the same section
ui.retranslateUi(MainWindow)

就是这样,我的 if __name__ == '__main__': 部分看起来像:

if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

关于python - 使用 pyqt 的组合框动态更改应用程序的翻译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46058686/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com