import sys
import sqlite3
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtWidgets import (QMainWindow, QDesktopWidget, QDialog, QLabel, QHBoxLayout,
QVBoxLayout, QWidget, QLineEdit, QApplication, QPushButton)
from PyQt5.QtCore import pyqtSlot
class Singleton():
_instance = None
def __new__(class_, *args, **kwargs):
if not isinstance(class_._instance, class_):
class_._instance = QWidget.__new__(class_, *args, **kwargs)
return class_._instance
class MainWindow(Singleton, QtWidgets.QWidget):
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.count_teached_word = QtWidgets.QLabel("Count teached word : Y")
self.count_teached_word.setAlignment(QtCore.Qt.AlignCenter)
self.count_word = QtWidgets.QLabel("Count word : X")
self.count_word.setAlignment(QtCore.Qt.AlignCenter)
self.btn_teach = QtWidgets.QPushButton("TEACH WORD")
self.btn_teach.clicked.connect(self.fun_teach)
self.btn_edit = QtWidgets.QPushButton("EDIT WORD")
self.btn_edit.clicked.connect(self.fun_edit)
self.btn_test = QtWidgets.QPushButton("TEST")
self.btn_test.clicked.connect(self.fun_test)
vertical_layout = QVBoxLayout()
vertical_layout.addWidget(self.count_word, QtCore.Qt.AlignCenter)
vertical_layout.addWidget(self.count_teached_word, QtCore.Qt.AlignVCenter)
vertical_layout.addWidget(self.btn_teach)
vertical_layout.addWidget(self.btn_edit)
vertical_layout.addWidget(self.btn_test)
self.setLayout(vertical_layout)
vertical_layout.setContentsMargins(0, 0, 0, 0)
self.setWindowTitle("tech word eng")
self.resize(180, 210)
self.center()
self.show()
def center(self):
qr = self.frameGeometry()
cp = QDesktopWidget().availableGeometry().center()
qr.moveCenter(cp)
self.move(qr.topLeft())
def fun_teach(self):
self.close()
return TeachingWindow()
def fun_edit(self):
self.close()
return EditWindow()
def fun_test(self):
self.close()
return TestWindow()
class TeachingWindow(Singleton, QtWidgets.QWidget):
"""on page show word for teach"""
def __init__(self, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.english_show_word = QtWidgets.QLabel("english word")
self.english_show_word.setAlignment(QtCore.Qt.AlignCenter)
self.option_one = QtWidgets.QPushButton("Option 1")
#self.option_one.clicked.connect(self)
self.option_two = QtWidgets.QPushButton("Option 2")
#self.option_two.clicked.connect(self)
self.option_three = QtWidgets.QPushButton("Option 3")
#self.option_three.clicked.connect(self)
self.option_four = QtWidgets.QPushButton("Option 4")
#self.option_four.clicked.connect(self)
self.btn_exit = QtWidgets.QPushButton("MAIN WINDOW")
#self.btn_exit.clicked.connect(self.fun_exit)
vertical_layout = QVBoxLayout()
vertical_layout.addWidget(self.english_show_word)
vertical_layout.addWidget(self.option_one)
vertical_layout.addWidget(self.option_two)
vertical_layout.addWidget(self.option_three)
vertical_layout.addWidget(self.option_four)
vertical_layout.addWidget(self.btn_exit)
self.setLayout(vertical_layout)
vertical_layout.setContentsMargins(0, 0, 0, 0)
self.setWindowTitle("Teaching")
self.resize(180, 210)
self.show()
def fun_learned_word(self):
pass
def fun_not_learned_word(self):
pass
def fun_exit(self):
self.close()
return MainWindow()
if __name__ == '__main__':
app = QApplication(sys.argv)
#mainn_window = TeachingWindow()
#main_window = EditWindow()
mainnn_window = MainWindow()
sys.exit(app.exec_())
При нажатии на кнопку self.btn_exit = QtWidgets.QPushButton("MAIN WINDOW") мне нужно вернуться в MainWindow. Я обрабатываю событие нажатия self.btn_exit.clicked.connect(self.fun_exit) и вместо возвращения на главную получаю ошибку AttributeError: 'TeachingWindow' object has no attribute 'fun_exit'. Много статей/форумов посмотрел, но так и не понял почему она возникла.
Помогите плз) Заранее огромное спасибо!