Не работает class в PyQt5
У меня возникла проблема. В приведённом ниже коде не работает класс OK_Window().
Пожалуйста объясните почему не работает, как это решить и что мне выучить.
Мой код:
import cryption as cry
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QLabel, QVBoxLayout, QWidget
from PyQt5 import QtCore, QtGui, QtWidgets
print('1')
class Ui_MainWindow(QMainWindow):
def crypte(self):
self.edit_1 = self.textEdit.text ()
self.edit_2 = self.textEdit_2.text ()
self.edit_3 = self.textEdit_3.text ()
print('4.1')
print(self.edit_1, self.edit_2, self.edit_3, '1')
self.qwerty = cry.crypt(self.edit_1, self.edit_2, self.edit_3)
global qwerty
qwerty = self.qwerty
print(qwerty)
print('4.2')
w2.show()
print('4.3')
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(500, 250)
MainWindow.setMinimumSize(QtCore.QSize(500, 250))
MainWindow.setMaximumSize(QtCore.QSize(500, 250))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser.setGeometry(QtCore.QRect(0, 0, 501, 51))
self.textBrowser.setObjectName("textBrowser")
self.textBrowser_3 = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser_3.setGeometry(QtCore.QRect(0, 50, 161, 51))
self.textBrowser_3.setObjectName("textBrowser_3")
self.textBrowser_4 = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser_4.setGeometry(QtCore.QRect(0, 100, 161, 51))
self.textBrowser_4.setObjectName("textBrowser_4")
self.textBrowser_5 = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser_5.setGeometry(QtCore.QRect(0, 150, 161, 51))
self.textBrowser_5.setObjectName("textBrowser_5")
self.textEdit = QtWidgets.QLineEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(160, 50, 341, 51))
self.textEdit.setObjectName("textEdit")
self.textEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.textEdit_2.setGeometry(QtCore.QRect(160, 100, 341, 51))
self.textEdit_2.setObjectName("textEdit_2")
self.textEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.textEdit_3.setGeometry(QtCore.QRect(160, 150, 341, 51))
self.textEdit_3.setObjectName("textEdit_3")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(0, 200, 501, 51))
self.pushButton.setStyleSheet("font: 75 18pt \"MS Shell Dlg 2\";")
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.push = self.pushButton.clicked.connect(lambda: self.crypte())
print('3')
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.textBrowser.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:7.8pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:20pt; font-weight:600;\">Зашифровка</span></p></body></html>"))
self.textBrowser_3.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:7.8pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:9pt; font-weight:600;\">Введите язык (eng, ru, ru-eng)</span></p></body></html>"))
self.textBrowser_4.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:7.8pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:9pt; font-weight:600;\">Введите спец-код от 1 до 100</span></p></body></html>"))
self.textBrowser_5.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:7.8pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:9pt; font-weight:600;\">Что хотите зашифровать?</span></p></body></html>"))
self.pushButton.setText(_translate("MainWindow", "Результат"))
print('2')
class OK_Window(Ui_MainWindow):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(500, 100)
MainWindow.setStyleSheet("background-color: rgb(255, 255, 255);")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(0, 0, 470, 150))
self.label.setObjectName("label")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.a = self.label.text()
print(self.a)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", qwerty))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
w2 = OK_Window()
MainWindow.show()
sys.exit(app.exec_())
print('end')
Ответы (1 шт):
НИКОГДА НЕ ИЗМЕНЯЙТЕ код, сгенерированный Qt Designer, НИКОГДА.
Создайте другой класс, который наследуется от соответствующего виджета, и используйте созданный класс для его заполнения.
# ??? import cryption as cry
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, \
QLabel, QVBoxLayout, QWidget
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(500, 250)
MainWindow.setMinimumSize(QtCore.QSize(500, 250))
MainWindow.setMaximumSize(QtCore.QSize(500, 250))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser.setGeometry(QtCore.QRect(0, 0, 501, 51))
self.textBrowser.setObjectName("textBrowser")
self.textBrowser_3 = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser_3.setGeometry(QtCore.QRect(0, 50, 161, 51))
self.textBrowser_3.setObjectName("textBrowser_3")
self.textBrowser_4 = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser_4.setGeometry(QtCore.QRect(0, 100, 161, 51))
self.textBrowser_4.setObjectName("textBrowser_4")
self.textBrowser_5 = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser_5.setGeometry(QtCore.QRect(0, 150, 161, 51))
self.textBrowser_5.setObjectName("textBrowser_5")
self.textEdit = QtWidgets.QLineEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(160, 50, 341, 51))
self.textEdit.setObjectName("textEdit")
self.textEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.textEdit_2.setGeometry(QtCore.QRect(160, 100, 341, 51))
self.textEdit_2.setObjectName("textEdit_2")
self.textEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.textEdit_3.setGeometry(QtCore.QRect(160, 150, 341, 51))
self.textEdit_3.setObjectName("textEdit_3")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(0, 200, 501, 51))
self.pushButton.setStyleSheet("font: 75 18pt \"MS Shell Dlg 2\";")
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
# self.push = self.pushButton.clicked.connect(lambda: self.crypte())
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.textBrowser.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:7.8pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:20pt; font-weight:600;\">Зашифровка</span></p></body></html>"))
self.textBrowser_3.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:7.8pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:9pt; font-weight:600;\">Введите язык (eng, ru, ru-eng)</span></p></body></html>"))
self.textBrowser_4.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:7.8pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:9pt; font-weight:600;\">Введите спец-код от 1 до 100</span></p></body></html>"))
self.textBrowser_5.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:7.8pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:9pt; font-weight:600;\">Что хотите зашифровать?</span></p></body></html>"))
self.pushButton.setText(_translate("MainWindow", "Результат"))
# ------------> vvvvvvvvvvv<>vvvvvvvvvvvvv <---------------------------------
class OK_Window(QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self) # +++
self.label = QtWidgets.QLabel(self.centralwidget) # ???
self.label.setGeometry(QtCore.QRect(0, 0, 470, 150))
self.label.setObjectName("label")
self.push = self.pushButton.clicked.connect(self.crypte)
# ??? self.a = self.label.text()
# ??? print(self.a)
def crypte(self):
self.edit_1 = self.textEdit.text()
self.edit_2 = self.textEdit_2.text()
self.edit_3 = self.textEdit_3.text()
print(self.edit_1, self.edit_2, self.edit_3, '1')
# ------------------> ??? <--------------------------------------------------
# self.qwerty = cry.crypt(self.edit_1, self.edit_2, self.edit_3)
self.qwerty = 'qwerty'
print(self.qwerty)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
# MainWindow = QtWidgets.QMainWindow()
# ui = Ui_MainWindow()
# ui.setupUi(MainWindow)
w2 = OK_Window() #
w2.show() # +++
# MainWindow.show()
sys.exit(app.exec_())
Update:
с вашим кодом у меня перестали работать
QLineEditвведения языка и введения спец-кода, вы не знаете почему?
Я просто не могу ввести туда текст, курсор остаётся обычным,
а не в формеI
Sorry, но код ваш. Я же вам отметил как # ??? и строки self.label... - что это? Вы этим self.label накрываете почти все в вашем окне, потому что self.label.setGeometry(QtCore.QRect(0, 0, 470, 150))
Научитесь использовать менеджеры компоновки (Layout Management) !!!
# ??? import cryption as cry
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, \
QLabel, QVBoxLayout, QWidget
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(500, 250)
MainWindow.setMinimumSize(QtCore.QSize(500, 250))
MainWindow.setMaximumSize(QtCore.QSize(500, 250))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser.setGeometry(QtCore.QRect(0, 0, 501, 51))
self.textBrowser.setObjectName("textBrowser")
self.textBrowser_3 = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser_3.setGeometry(QtCore.QRect(0, 50, 161, 51))
self.textBrowser_3.setObjectName("textBrowser_3")
self.textBrowser_4 = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser_4.setGeometry(QtCore.QRect(0, 100, 161, 51))
self.textBrowser_4.setObjectName("textBrowser_4")
self.textBrowser_5 = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser_5.setGeometry(QtCore.QRect(0, 150, 161, 51))
self.textBrowser_5.setObjectName("textBrowser_5")
self.textEdit = QtWidgets.QLineEdit(self.centralwidget)
self.textEdit.setGeometry(QtCore.QRect(160, 50, 341, 51))
self.textEdit.setObjectName("textEdit")
self.textEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.textEdit_2.setGeometry(QtCore.QRect(160, 100, 341, 51))
self.textEdit_2.setObjectName("textEdit_2")
self.textEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.textEdit_3.setGeometry(QtCore.QRect(160, 150, 341, 51))
self.textEdit_3.setObjectName("textEdit_3")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(0, 200, 501, 51))
self.pushButton.setStyleSheet("font: 75 18pt \"MS Shell Dlg 2\";")
self.pushButton.setObjectName("pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
# self.push = self.pushButton.clicked.connect(lambda: self.crypte())
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.textBrowser.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:7.8pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:20pt; font-weight:600;\">Зашифровка</span></p></body></html>"))
self.textBrowser_3.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:7.8pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:9pt; font-weight:600;\">Введите язык (eng, ru, ru-eng)</span></p></body></html>"))
self.textBrowser_4.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:7.8pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:9pt; font-weight:600;\">Введите спец-код от 1 до 100</span></p></body></html>"))
self.textBrowser_5.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:7.8pt; font-weight:400; font-style:normal;\">\n"
"<p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:9pt; font-weight:600;\">Что хотите зашифровать?</span></p></body></html>"))
self.pushButton.setText(_translate("MainWindow", "Результат"))
# ------------> vvvvvvvvvvv<>vvvvvvvvvvvvv <---------------------------------
class OK_Window(QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self) # +++
# ???
# self.label = QtWidgets.QLabel(self.centralwidget) # ???
# self.label.setGeometry(QtCore.QRect(0, 0, 470, 150)) # 470, 150 ???
# self.label.setObjectName("label")
self.push = self.pushButton.clicked.connect(self.crypte)
# +++ vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
self.textEdit.setPlaceholderText('Введите язык')
self.textEdit_2.setPlaceholderText('Введите спец-код от 1 до 100')
self.textEdit_3.setPlaceholderText('Введите то, что хотите зашифровать')
self.textEdit.setFocus()
# +++ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# ??? self.a = self.label.text()
# ??? print(self.a)
def crypte(self):
self.edit_1 = self.textEdit.text()
self.edit_2 = self.textEdit_2.text()
self.edit_3 = self.textEdit_3.text()
print(self.edit_1, self.edit_2, self.edit_3, '1')
# ------------------> ??? <--------------------------------------------------
# self.qwerty = cry.crypt(self.edit_1, self.edit_2, self.edit_3)
self.qwerty = 'qwerty'
print(self.qwerty)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w2 = OK_Window()
w2.show()
sys.exit(app.exec_())

