Okay, I am jumping from Tkinter to PyQt, because PyQt is just so much more advanced, and nicer to work with. BUT! I am having some troubles here.
I am trying to chan
The solution I propose is based on the function setupUi()
that generates Qt Designer, this is responsible for creating the internal elements of the window.
import sys
from PyQt4.QtGui import QApplication, QMainWindow, QPushButton, QWidget
#For PyQt5 :
#from PyQt5.QWidgets import QApplication , QMainWindow , QPushButton , QWidget
class UIWindow(object):
def setupUI(self, MainWindow):
MainWindow.setGeometry(50, 50, 400, 450)
MainWindow.setFixedSize(400, 450)
MainWindow.setWindowTitle("UIWindow")
self.centralwidget = QWidget(MainWindow)
# mainwindow.setWindowIcon(QtGui.QIcon('PhotoIcon.png'))
self.ToolsBTN = QPushButton('text', self.centralwidget)
self.ToolsBTN.move(50, 350)
MainWindow.setCentralWidget(self.centralwidget)
class UIToolTab(object):
def setupUI(self, MainWindow):
MainWindow.setGeometry(50, 50, 400, 450)
MainWindow.setFixedSize(400, 450)
MainWindow.setWindowTitle("UIToolTab")
self.centralwidget = QWidget(MainWindow)
self.CPSBTN = QPushButton("text2", self.centralwidget)
self.CPSBTN.move(100, 350)
MainWindow.setCentralWidget(self.centralwidget)
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.uiWindow = UIWindow()
self.uiToolTab = UIToolTab()
self.startUIWindow()
def startUIToolTab(self):
self.uiToolTab.setupUI(self)
self.uiToolTab.CPSBTN.clicked.connect(self.startUIWindow)
self.show()
def startUIWindow(self):
self.uiWindow.setupUI(self)
self.uiWindow.ToolsBTN.clicked.connect(self.startUIToolTab)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWindow()
sys.exit(app.exec_())
Start:
After button clicked:
After another button clicked:
and more elegant solution:
import sys
from PyQt4.QtGui import QApplication, QMainWindow, QPushButton, QWidget
#For PyQt5 :
#from PyQt5.QWidgets import QApplication , QMainWindow , QPushButton , QWidget
class UIWindow(QWidget):
def __init__(self, parent=None):
super(UIWindow, self).__init__(parent)
# mainwindow.setWindowIcon(QtGui.QIcon('PhotoIcon.png'))
self.ToolsBTN = QPushButton('text', self)
self.ToolsBTN.move(50, 350)
class UIToolTab(QWidget):
def __init__(self, parent=None):
super(UIToolTab, self).__init__(parent)
self.CPSBTN = QPushButton("text2", self)
self.CPSBTN.move(100, 350)
class MainWindow(QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setGeometry(50, 50, 400, 450)
self.setFixedSize(400, 450)
self.startUIToolTab()
def startUIToolTab(self):
self.ToolTab = UIToolTab(self)
self.setWindowTitle("UIToolTab")
self.setCentralWidget(self.ToolTab)
self.ToolTab.CPSBTN.clicked.connect(self.startUIWindow)
self.show()
def startUIWindow(self):
self.Window = UIWindow(self)
self.setWindowTitle("UIWindow")
self.setCentralWidget(self.Window)
self.Window.ToolsBTN.clicked.connect(self.startUIToolTab)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
w = MainWindow()
sys.exit(app.exec_())