How to define a widget or a new window inside in qmainwindow? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to define a widget or a new window inside in qmainwindow?

Think we have a qmainwindow with a qpushbutton, when click on qpushbutton the new window or qwidget getting open, how write this?

15th Mar 2017, 4:45 PM
hamid
hamid - avatar
12 Answers
+ 3
from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication, QLabel class MainWindow(QMainWindow): def __init__(self): super(MainWindow, self).__init__() btn = QPushButton('Click me!', self) btn.clicked.connect(self.onClick) def onClick(self): self.SW = SecondWindow() self.SW.show() class SecondWindow(QMainWindow): def __init__(self): super(SecondWindow, self).__init__() lbl = QLabel('Second Window', self) if __name__ == '__main__': import sys app = QApplication(sys.argv) MW = MainWindow() MW.show() sys.exit(app.exec_())
20th Mar 2017, 7:41 AM
Ruslan Ovcharenko
Ruslan Ovcharenko - avatar
+ 1
with two windows same as one. Just write func, that will open third window (you have my code for second) and write third class for third window. If you want, I can write code for you tomorrow.
28th Mar 2017, 7:35 PM
Ruslan Ovcharenko
Ruslan Ovcharenko - avatar
+ 1
this is my code: # I import all ao things that need. class mainwin(QMainWindow, Ui_MainWindow): def __init__(self): super(mainwin, self).__init__() self.setupUi(self) self.pushbutton1.clicked.connect(self.fform) self.pushbutton2.clicked.connect(self.sform) def fform(self): self.firstform.show() def sform(self): self.secondform.show() class firstform(QWidget, Ui_Form) def __init__(self): super(firstform, self).__init__() class secondwindow(QWidget, Ui_Form2) def __init__(self): super(secondwindow , self).__init__()
29th Mar 2017, 11:11 AM
hamid
hamid - avatar
+ 1
1. in func "sform" you call class "secondform", although it is called "secondwindow" 2. try this two funcs instead your: def fform(self): self.FF = firstform() self.FF.show() def sform(self): self.SW = secondwindow() self.SW.show() 3. And no ":" after class
29th Mar 2017, 11:32 AM
Ruslan Ovcharenko
Ruslan Ovcharenko - avatar
+ 1
thank you, thats work, thannnksssss
29th Mar 2017, 11:49 AM
hamid
hamid - avatar
0
ok mate, i'll post it tomorrow (22:47 in Ukraine ;) )
28th Mar 2017, 7:47 PM
Ruslan Ovcharenko
Ruslan Ovcharenko - avatar
0
Here you have two buttons each of which open new window. from PyQt5.QtWidgets import QMainWindow, QPushButton, QApplication, QLabel class MainWindow(QMainWindow): def __init__(self): super(MainWindow, self).__init__() btn = QPushButton('Open Second', self) btn.move(10, 10) btn.clicked.connect(self.openSecond) btn2 = QPushButton('Open Third', self) btn2.move(110, 10) btn2.clicked.connect(self.openThird) self.resize(220, 50) def openSecond(self): self.SW = SecondWindow() self.SW.show() def openThird(self): self.TW = ThirdWindow() self.TW.show() class SecondWindow(QMainWindow): def __init__(self): super(SecondWindow, self).__init__() lbl = QLabel('Second Window', self) class ThirdWindow(QMainWindow): def __init__(self): super(ThirdWindow, self).__init__() lbl = QLabel('Third Window', self) if __name__ == '__main__': import sys app = QApplication(sys.argv) MW = MainWindow() MW.show() sys.exit(app.exec_())
29th Mar 2017, 7:04 AM
Ruslan Ovcharenko
Ruslan Ovcharenko - avatar
0
Better give me your code, it will be easier to find error
29th Mar 2017, 9:17 AM
Ruslan Ovcharenko
Ruslan Ovcharenko - avatar
0
in my code this error happen: 'mainwin' object has no attribute 'secondwindow. why?'
29th Mar 2017, 11:21 AM
hamid
hamid - avatar
0
thank you, thats work, thannnksssss
29th Mar 2017, 11:49 AM
hamid
hamid - avatar
0
You'r welcome! Im glad im helped you :)
29th Mar 2017, 12:04 PM
Ruslan Ovcharenko
Ruslan Ovcharenko - avatar
0
Do you know how to connect from the second window to the third window? I made a push button in the second window and try to connect it to the third window but it doesn't open the third one and ends the program
11th Jun 2021, 4:35 AM
unknown n
unknown n - avatar