我如何使用pyqt4更改qpushbutton的标签名称

时间:2018-12-13 08:20:13

标签: python pyqt4

这是我的示例代码,我尝试使用python将我的整个文件翻译成阿拉伯语。我在pyqt4中尝试了字典,当我单击阿拉伯语时,我想更改所有按钮名称和标签名称我的file.please任何人都可以帮助我,如何在pyqt4中使用此程序来解决该程序

下面是我的示例代码:

# -*- coding: utf-8 -*-
import sys
from pyface.qt import QtGui, QtCore

data = {'Arabic': {'Share': 'Aktie','Rename': 'Umbenennen','Back':'Zurück'}}
class Example(QtGui.QMainWindow):
    def __init__(self, parent=None):
        super(Example, self).__init__(parent)
        self.sharebtn = QtGui.QPushButton("Share")
        self.renamebtn = QtGui.QPushButton("Rename")
        self.backbtn  = QtGui.QPushButton('Back')

        self.labelflag = QtGui.QLabel()
        pixmap = QtGui.QPixmap("./img/english.png")
        pixmap1 = pixmap.scaled(60, 60, QtCore.Qt.KeepAspectRatio, QtCore.Qt.FastTransformation)

        self.labelflag.setPixmap(pixmap1)
        self.labelflag.mousePressEvent = self.languageWidget
        self.lay1 = QtGui.QVBoxLayout()
        self.scrollArea = QtGui.QScrollArea()
        self.scrollArea.setBackgroundRole(QtGui.QPalette.Light)
        self.scrollArea.setWidgetResizable(True)
        self.mainLayout =QtGui. QGridLayout()
        self.btnSee  =QtGui. QPushButton('See_file')
        self.btnSee.clicked.connect(self.files)
        self.lay1 = QtGui.QVBoxLayout()
        self.lay1.addWidget(self.btnSee)
        self.lay1.addWidget(self.backbtn)
        self.lay1.addWidget(self.sharebtn)
        self.lay1.addWidget(self.labelflag)

        self.mainLayout.addWidget(self.scrollArea,0,1)
        self.mainLayout.addLayout(self.lay1,0,0)
        self.setCentralWidget(QtGui.QWidget(self))
        self.centralWidget().setLayout(self.mainLayout)
        self.setGeometry(100,100,800,500)
    def files(self):
        self.w = QtGui.QWidget()
        self.vbox = QtGui.QVBoxLayout(self.w)
        self.label = QtGui.QLabel()
        self.label.setText("helloworld")
        self.btnq = QtGui.QPushButton("click")
        self.vbox.addWidget(self.label)
        self.vbox.addWidget(self.btnq)
        self.scrollArea.setWidget(self.w)
        self.show()

    def languageWidget(self, event):

        self.lanwidget1 = QtGui.QDialog()
        self.langvbox = QtGui.QVBoxLayout(self.lanwidget1)

        self.eng_hbox = QtGui.QHBoxLayout()
        self.englabel = QtGui.QLabel()
        self.englabel.setText("English")
        self.englabel1 = QtGui.QLabel()

        self.engpixmap = QtGui.QPixmap("./img/english.png")
        self.englabel1.mousePressEvent = self.engicon
        self.eng_pixmap = self.engpixmap.scaled(60, 60, QtCore.Qt.KeepAspectRatio, QtCore.Qt.FastTransformation)
        self.englabel1.setPixmap(self.eng_pixmap)
        self.eng_hbox.addWidget(self.englabel1)
        self.eng_hbox.addWidget(self.englabel)


        self.arabic_hbox = QtGui.QHBoxLayout()

        self.ara_label = QtGui.QLabel()
        self.ara_label.setText("Arabic")
        self.arbiclabel = QtGui.QLabel()
        self.arabicpixmap = QtGui.QPixmap("./img/arabic.jpeg")
        self.arbiclabel.mousePressEvent = self.arabic_icon
        self.ara_pixmap = self.arabicpixmap.scaled(60, 60, QtCore.Qt.KeepAspectRatio, QtCore.Qt.FastTransformation)
        self.arbiclabel.setPixmap(self.ara_pixmap)
        self.arabic_hbox.addWidget(self.arbiclabel)
        self.arabic_hbox.addWidget(self.ara_label)


        self.german_hbox = QtGui.QHBoxLayout()
        self.ger_label = QtGui.QLabel()
        self.ger_label.setText("German")

        self.germanlabel = QtGui.QLabel()
        self.germanpixmap = QtGui.QPixmap("./img/german.png")
        self.german_pixmap = self.germanpixmap.scaled(60, 60, QtCore.Qt.KeepAspectRatio, QtCore.Qt.FastTransformation)
        self.germanlabel.setPixmap(self.german_pixmap)
        self.german_hbox.addWidget(self.germanlabel)
        self.germanlabel.mousePressEvent = self.german_icon
        self.german_hbox.addWidget(self.ger_label)

        self.langvbox.addLayout(self.eng_hbox)
        self.langvbox.addLayout(self.arabic_hbox)
        self.langvbox.addLayout(self.german_hbox)
        self.lanwidget1.setWindowFlags(QtCore.Qt.FramelessWindowHint)

        self.lanwidget1.exec_()
    def engicon(self,event):
        print "englishh"
        global langIndx
        self.lanwidget1.close()
        self.labelflag.setPixmap(self.eng_pixmap)
    def arabic_icon(self,event):
        print "arabiccc"
        global langIndx
        self.lanwidget1.close()
        self.labelflag.setPixmap(self.ara_pixmap)
        self.change("Arabic")

    def german_icon(self,event):
        print "germannn"
        global langIndx
        self.lanwidget1.close()
        self.labelflag.setPixmap(self.german_pixmap)
    def change(self,i):
        print i, "iiiiiiiiiiiiiilanguage"

        if i == "Arabic":
            if i in data.keys():
                share_con_value1 = data.get(i, {}).get('Share')
                self.sharebtn = QtGui.QPushButton("share_con_value1")

                ex = Example()
                ex.show()
               #here i want to get the label names of the button how can i change and replace the that particular word 

def main():
    print sys.argv
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    ex.show()
    sys.exit(app.exec_())

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:1)

您的代码甚至都没有尝试读取文件。

摆脱掉所有QT东西,现在可以进入控制台,以后再担心GUI。

然后进行翻译。这不是一件容易的事。您无法进行1-1翻译,因为语言的使用方式有所不同。最好是使用googles API之类的翻译API。

步骤如下:

  1. 摆脱Qt并返回控制台应用程序
  2. 打开并读取文件内容
  3. 获取内容并使用从文件中读取的文字调用googles api
  4. 从Google取回文字 将其打印到控制台屏幕或将其写入文件
  5. 拉回Qt以创建您的GUI