Recently I started learning python, I liked the PyQt5 library, I started learning. In general, the problem is the following: when opening a program, a window is displayed with the choice of the program language, for example, we select Ukrainian or Russian and the text changes to the one that was selected. I tried to create a function and enter the parameters from the main window, but I can't. And please tell me how to put the button Choose the language in the center of the window?

CODE:

from PyQt5.QtWidgets import (QApplication ,QWidget, QPushButton) from PyQt5 import QtCore from PyQt5 import QtWidgets import sys class MainWindow(QWidget): def __init__(self, parent=None): super().__init__(parent) self.secondWin = None self.initUI() def initUI(self): self.setWindowTitle('Product calculation') self.resize(1000, 640) qr = self.frameGeometry() cp = QtWidgets.QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) self.move(qr.topLeft()) self.but = QPushButton(self) self.but.setText('Choose the language') self.but.resize(120, 30) self.but.clicked.connect(self.openCL) self.but.clicked.connect(self.hideBut) self.but.clicked.connect(self.showbut) self.but.move(400, 280) self.but.show() self.but1 = QPushButton(self) self.but1.setText('Create calculation') self.but1.resize(140, 32) self.but1.move(100, 100) self.but1.hide() self.but2 = QPushButton(self) self.but2.setText('Search a calculation') self.but2.resize(140, 32) self.but2.move(100, 150) self.but2.hide() self.but3 = QPushButton(self) self.but3.setText('Settings') self.but3.resize(140, 32) self.but3.move(100, 200) self.but3.hide() self.but4 = QPushButton(self) self.but4.setText('About program') self.but4.resize(140, 32) self.but4.move(100, 250) self.but4.hide() def hideBut(self): self.but.hide() def showbut(self): self.but1.show() self.but2.show() self.but3.show() self.but4.show() def openCL(self): if not self.secondWin: self.secondWin = ChooseLang(self) self.secondWin.show() class ChooseLang(QWidget): def __init__(self, parent=None): super().__init__(parent, QtCore.Qt.Window) self.initUI() def initUI(self): self.setWindowTitle('CL') self.resize(200, 100) self.butE = QPushButton(self) self.butE.setText('English') self.butE.move(20, 30) self.butE.clicked.connect(self.closeSW) self.butIta = QPushButton(self) self.butIta.setText('Italian') self.butIta.move(20, 60) self.butIta.clicked.connect(self.closeSW) self.butRu = QPushButton(self) self.butRu.setText('Russian') self.butRu.move(100, 30) self.butRu.clicked.connect(self.closeSW) self.butUa = QPushButton(self) self.butUa.setText('Ukrainian') self.butUa.move(100, 60) self.butUa.clicked.connect(self.closeSW) def closeSW(self): self.close() if __name__== '__main__': app = QApplication(sys.argv) window = MainWindow() window.show() sys.exit(app.exec_()) 

    1 answer 1

    PyQt5 is a good choice.

    Arrangement of widgets in the window is better done using layouts. Read the documents https://doc.qt.io/qt-5/qlayout.html#details

    To dynamically change languages ​​(translations) on PyQt5 you can do this:

    • We write a program in which we create a method that can be called retranslateUi (), and set texts in it using translate () instead of tr () (for more information, read the documents). http://pyqt.sourceforge.net/Docs/PyQt5/i18n.html Calling this method when changing the language for it should use the changeEvent () event

    • Then format .ts (for each language). For example, for Italian it looks like this:

    eng-it.ts

     <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE TS> <TS version="2.1" language="it"> <context> <name>MainWindow</name> <message> <location filename="main.py" line="165"/> <source>Product calculation</source> <translation>Calcolo del prodotto</translation> </message> <message> <location filename="main.py" line="122"/> <source>Create calculation</source> <translation>Crea calcolo</translation> </message> <message> <location filename="main.py" line="123"/> <source>Search a calculation</source> <translation>Cerca un calcolo</translation> </message> <message> <location filename="main.py" line="124"/> <source>Settings</source> <translation>Impostazioni</translation> </message> <message> <location filename="main.py" line="125"/> <source>About program</source> <translation>Informazioni sul programma</translation> </message> <message> <location filename="main.py" line="120"/> <source>Hello, World</source> <translation>Ciao, Mondo</translation> </message> </context> </TS> 

    similarly for other languages ​​...

    • Then format .qm:

    lrelease eng-it.ts eng-ru.ts eng-uk.ts

    • Now you can run the application:

     import sys from PyQt5.QtWidgets import (QApplication ,QWidget, QPushButton, QComboBox, QGridLayout, QLabel) from PyQt5.QtCore import QSize, Qt, pyqtSlot, QTranslator, QEvent class MainWindow(QWidget): def __init__(self, chooseLang, parent=None): super().__init__(parent) self.chooseLang = chooseLang self.initUI() def initUI(self): self.resize(550, 370) self.combo = QComboBox() self.combo.currentIndexChanged.connect(self.unit_choice) self.label = QLabel(alignment=Qt.AlignCenter) self.but1 = QPushButton(self, minimumSize=QSize(140, 50)) self.but2 = QPushButton(self, minimumSize=QSize(140, 50)) self.but3 = QPushButton(self, minimumSize=QSize(140, 50)) self.but4 = QPushButton(self, minimumSize=QSize(140, 50)) self.trans = QTranslator(self) self.grid = QGridLayout(self) self.grid.addWidget(self.combo, 0, 1, alignment=Qt.AlignRight) self.grid.setRowStretch(1, 1) self.grid.addWidget(self.label, 2, 0, 1, 2, alignment=Qt.AlignCenter) self.grid.addWidget(self.but1, 3, 0, alignment=Qt.AlignLeft) self.grid.addWidget(self.but2, 4, 0, alignment=Qt.AlignLeft) self.grid.addWidget(self.but3, 5, 0, alignment=Qt.AlignLeft) self.grid.addWidget(self.but4, 6, 0, alignment=Qt.AlignLeft) self.grid.setRowStretch(7, 1) options = ([('English', ''), ('Italian', 'eng-it' ), ('Russian', 'eng-ru'), ('Ukrainian', 'eng-uk'),]) for i, (text, lang) in enumerate(options): self.combo.addItem(text) self.combo.setItemData(i, lang) self.retranslateUi() @pyqtSlot(int) def unit_choice(self, index): data = self.combo.itemData(index) if data: self.trans.load(data) QApplication.instance().installTranslator(self.trans) else: QApplication.instance().removeTranslator(self.trans) def changeEvent(self, event): if event.type() == QEvent.LanguageChange: self.retranslateUi() super(MainWindow, self).changeEvent(event) def retranslateUi(self): self.setWindowTitle(QApplication.translate('MainWindow', 'Product calculation')) self.but1.setText(QApplication.translate('MainWindow', 'Create calculation')) self.but2.setText(QApplication.translate('MainWindow', 'Search a calculation')) self.but3.setText(QApplication.translate('MainWindow', 'Settings')) self.but4.setText(QApplication.translate('MainWindow', 'About program')) self.label.setText(QApplication.translate('MainWindow', 'Hello, World')) ChooseLang = ["English", "Italian", "Russian", "Ukrainian"] if __name__== '__main__': app = QApplication(sys.argv) window = MainWindow(ChooseLang) window.show() sys.exit(app.exec_()) 

    enter image description here