代码之家  ›  专栏  ›  技术社区  ›  Elcook

关于Qt国际化的问题

  •  0
  • Elcook  · 技术社区  · 7 年前

    我不知道如何使用Qt为python应用程序创建翻译文件。

    现在我只想翻译代码中的几个词。

    从…起 Qt Linguist page 我知道我需要使用一个。pro项目文件,该文件将由 pylupdate4

    我试着跑步:

    $ qmake -project myfile.py
    $ pylupdate4 myfile.pro -ts file.ts
    

    但结果是。无法读取pro文件 ( XML error: Parse error at line 1, column 1 [...] )

    从这个 Tutorial

    $ pylupdate4 myfile.py -ts file.ts
    

    有没有人能告诉我哪里出了问题,我在浏览器中打开的15个标签帮不上忙。

    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(QtCore.QString('yes')))
            comboBox=QtGui.QComboBox(self) #create drop down menu
            comboBox.addItem('Portugues')
            comboBox.addItem('English')
            self.hbox.addWidget(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')
    
    
            self.show()
    
    
        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()
    
    def main():
    
        app = QtGui.QApplication(sys.argv)
        translator = QtCore.QTranslator()
        translator.load("~/basefiles/translations/qt_pt.qm")
        app.installTranslator(translator)
        ex = MainWindow()
        sys.exit(app.exec_())
    
    
    if __name__ == '__main__':
        main()  
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Elcook    7 年前

    当你使用self时。tr您必须传递字符串,而不是QString变量,在您的情况下,它会发生变化:

    self.myButtons.setText(self.tr(QtCore.QString('yes')))
    

    self.myButtons.setText(self.tr("yes"))