1.開啟designer ,新增 pushbutton, label, comboBox 三項
2.儲存為untitled.ui
pyuic5 -x untitled.ui -o untitled.py
3.將py檔案的產生 ts 檔案,為translation source
pylupdate5 untitled.py -ts eng-chs.ts (把英文轉為中文)
pylupdate5 untitled.py -ts eng-fr.ts (把英文轉為法文)
4.要把 ts轉換為 qm檔案格式,為python 可讀取的
4.1 利用 開啟 linguist 軟體,如下圖
4.2開啟ts 檔案,分別為 eng-cht.ts, eng-fr.ts 兩個
4.3要分別設定相關翻譯內容
建議只修改 按鈕與文字標籤
修改方法如下,
pushbutton 翻譯中文為 按鈕
pushbutton 翻譯法文為 sdfsdfds
4.4 輸入好之後打勾,變成藍色
4.5 選擇 -> 檔案 -> 全部匯出
則出現
eng-chs.qm
eng-fr.qm 兩個檔案
4.6
新寫一個python 檔案,呼叫untitled.py,為 test.py
*有四個地方要注意
1.from untitled import Ui_Form
2.self.trans = QTranslator(self)
3.self.comboBox.currentTextChanged.connect(self.change_func) # 用comboBox 切換內容
4.
def change_func(self):
if self.comboBox.currentText() == ‘中文’:
self.trans.load(‘eng-chs’)
_app = QApplication.instance()
_app.installTranslator(self.trans)
self.retranslateUi(self)
elif self.comboBox.currentText() == ‘french’:
self.trans.load(‘eng-fr’)
_app = QApplication.instance()
_app.installTranslator(self.trans)
self.retranslateUi(self)
else: # 英文
_app = QApplication.instance()
_app.removeTranslator(self.trans)
self.retranslateUi(self)
5.詳細內容為:
import sys
from untitled import Ui_Form
from PyQt5.QtCore import QTranslator
from PyQt5.QtWidgets import QApplication, QWidget
class Demo(QWidget, Ui_Form):
def init(self):
super(Demo, self).init()
self.setupUi(self)
self.trans = QTranslator(self) # 1
self.comboBox.currentTextChanged.connect(self.change_func) # 2
def change_func(self): # 3
if self.comboBox.currentText() == '中文':
self.trans.load('eng-chs')
_app = QApplication.instance()
_app.installTranslator(self.trans)
self.retranslateUi(self)
elif self.comboBox.currentText() == 'français':
self.trans.load('eng-fr')
_app = QApplication.instance()
_app.installTranslator(self.trans)
self.retranslateUi(self)
else:
_app = QApplication.instance()
_app.removeTranslator(self.trans)
self.retranslateUi(self)
if name == ‘main’:
app = QApplication(sys.argv)
demo = Demo()
demo.show()
sys.exit(app.exec_())