2017-09-05 84 views
1

我創建一個組合框,讓用戶選擇的文本語言的簡單窗口的組合框整個應用程序動態更改應用程序的翻譯與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,我將不勝感激。

回答

0

好吧,我發現了一個辦法做到這一點,我不知道這是否是一個非常「Python化」解決方案,但它的工作原理

我的main()函數現在看起來像這樣:

if __name__ == '__main__': 
    lang =1 #default language 
    i=0 
    while i>=0: 
     i=i+1 
     if lang == 1: 
      print "i'm english" 
      if i>1: 
       app2=None # to erase the previous aplication when we loop between two language choices 
      app = QtGui.QApplication(sys.argv) 
      translator = QtCore.QTranslator() 
      translator.load("~/translations/qt_pt.qm") 
      app.removeTranslator(translator) 
      ex = MainWindow(lang) 
      ex.show() 
      lang =app.exec_() 

     elif lang==0: 
      print "i'm portuguese" 
      app=None 
      app2 = QtGui.QApplication(sys.argv) 
      translator = QtCore.QTranslator() 
      translator.load("~/translations/qt_pt.qm") 
      app2.installTranslator(translator) 
      ex = MainWindow(lang) 
      ex.show() 
      lang=app2.exec_() 
      print "im here" 

到現在的語言我aplication需要加載,在我定義我的「combochange」功能,像這樣的組合框以下用戶輸入:

def combochange(self,e): 
    #print self.comboBox.currentText() 
    if self.comboBox.currentText()=="Portugues": 
     MainWindow.lang=0 
     QtGui.qApp.exit(MainWindow.lang) 

    else: 
     MainWindow.lang=1 
     QtGui.qApp.exit(MainWindow.lang) 

lang」是我外界傳遞,它會觸發一個應用程序關閉並以不同的語言重新打開。

它看起來不太優雅,但它的工作原理。

完整的代碼是:

#!/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,lang2): 

     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(lang2) 

    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() 
     sys.exit() 

    def initUI(self,lang2): 

     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(QtCore.QString(self.tr('yes'))) 
     self.myButtons.setText(self.tr("yes")) 
     self.comboBox=QtGui.QComboBox(self) #create drop down menu 
     if lang2 == 1: 
      self.comboBox.addItem('English') 
      self.comboBox.addItem('Portugues') 
     else: 
      self.comboBox.addItem('Portugues') 
      self.comboBox.addItem('English')    
     self.comboBox.currentIndexChanged.connect(self.combochange) 
     self.hbox.addWidget(self.comboBox,1,QtCore.Qt.AlignRight) #add drop down menu to box layout 
     self.hbox.addStretch(3)  # set separation between buttons 
     self.myButtons.clicked.connect(self.buttonClicked) # what should the button do 
     self.hbox.addWidget(self.myButtons,1,QtCore.Qt.AlignRight) #add button to box layout 

     self.setWindowTitle('Test2') 

    def combochange(self,e): 
     #print self.comboBox.currentText() 
     if self.comboBox.currentText()=="Portugues": 
      MainWindow.lang=0 
      QtGui.qApp.exit(MainWindow.lang) 

     else: 
      MainWindow.lang=1 
      QtGui.qApp.exit(MainWindow.lang) 

    def buttonClicked(self): 

     msbox= QtGui.QMessageBox() 
     choice=msbox.warning(self,'ok',"This button doesn't do anything!!!") 

     if choice == QtGui.QMessageBox.No: 
      print('nanan') 
     else: 
      print('Bye') 
      self.settings.setValue("size",self.size()); 
      self.settings.setValue("pos",self.pos()); 
      sys.exit() 

if __name__ == '__main__': 
    lang =1 
    i=0 
    while i>=0: 
     i=i+1 
     print i 
     if lang == 1: 
      print "i'm english" 
      if i>1: 
       app2=None 
      app = QtGui.QApplication(sys.argv) 
      translator = QtCore.QTranslator() 
      translator.load("/Users/paulmota/Documents/BlueBerryProject/basefiles/translations/qt_pt.qm") 
      app.removeTranslator(translator) 
      ex = MainWindow(lang) 
      ex.show() 
      lang =app.exec_() 

     elif lang==0: 
      print "i'm portuguese" 
      app=None 
      app2 = QtGui.QApplication(sys.argv) 
      translator = QtCore.QTranslator() 
      translator.load("/Users/paulmota/Documents/BlueBerryProject/basefiles/translations/qt_pt.qm") 
      app2.installTranslator(translator) 
      ex = MainWindow(lang) 
      ex.show() 
      lang=app2.exec_() 
      print "im here" 
0

這裏是一個更撥款的方式來做到這一點:那就是它

#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_())