我創建一個組合框,讓用戶選擇的文本語言的簡單窗口的組合框整個應用程序動態更改應用程序的翻譯與PyQt的
我已經創建了必要的.qm文件顯示,並且當我開始應用程序時,文本正在更新。但是,我怎樣才能將它鏈接到組合框上的選項,並從mainWindow內部改變語言?
我的代碼:
#!/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
我想我不得不從主窗口的參數某種方式傳遞給主( )函數並重新載入所有內容。
這甚至可能嗎?
的Python 2.7,QT 5.9.1,PyQt4的4.12.1在OSX 10.11.6
編輯:
我發現this後QT維基頁面上的我想要做什麼,不幸的是我根本不精通C語言。如果有人可以幫我翻譯這個python,我將不勝感激。