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?
12 odpowiedzi
+ 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_())
+ 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.
+ 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__()
+ 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
+ 1
thank you, thats work, thannnksssss
0
ok mate, i'll post it tomorrow (22:47 in Ukraine ;) )
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_())
0
Better give me your code, it will be easier to find error
0
in my code this error happen:
'mainwin' object has no attribute 'secondwindow. why?'
0
thank you, thats work, thannnksssss
0
You'r welcome! Im glad im helped you :)
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