Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fa26ad2

Browse files
committed
PySide6 Multiple Windows
1 parent 81a2a61 commit fa26ad2

File tree

10 files changed

+953
-0
lines changed

10 files changed

+953
-0
lines changed

‎pyqt6/tutorials/creating-multiple-windows/pyside6-creating-multiple-windows.md

Lines changed: 504 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import sys
2+
3+
from PySide6.QtWidgets import (
4+
QApplication,
5+
QLabel,
6+
QMainWindow,
7+
QPushButton,
8+
QVBoxLayout,
9+
QWidget,
10+
)
11+
12+
13+
class AnotherWindow(QWidget):
14+
"""
15+
This "window" is a QWidget. If it has no parent, it
16+
will appear as a free-floating window as we want.
17+
"""
18+
19+
def __init__(self):
20+
super().__init__()
21+
layout = QVBoxLayout()
22+
self.label = QLabel("Another Window")
23+
layout.addWidget(self.label)
24+
self.setLayout(layout)
25+
26+
27+
class MainWindow(QMainWindow):
28+
29+
def __init__(self):
30+
super().__init__()
31+
self.button = QPushButton("Push for Window")
32+
self.button.clicked.connect(self.show_new_window)
33+
self.setCentralWidget(self.button)
34+
35+
def show_new_window(self, checked):
36+
w = AnotherWindow()
37+
w.show()
38+
39+
40+
app = QApplication(sys.argv)
41+
w = MainWindow()
42+
w.show()
43+
app.exec()
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import sys
2+
from random import randint
3+
4+
from PySide6.QtWidgets import (
5+
QApplication,
6+
QLabel,
7+
QMainWindow,
8+
QPushButton,
9+
QVBoxLayout,
10+
QWidget,
11+
)
12+
13+
14+
class AnotherWindow(QWidget):
15+
"""
16+
This "window" is a QWidget. If it has no parent,
17+
it will appear as a free-floating window.
18+
"""
19+
20+
def __init__(self):
21+
super().__init__()
22+
layout = QVBoxLayout()
23+
self.label = QLabel("Another Window % d" % randint(0, 100))
24+
layout.addWidget(self.label)
25+
self.setLayout(layout)
26+
27+
28+
class MainWindow(QMainWindow):
29+
def __init__(self):
30+
super().__init__()
31+
self.window1 = AnotherWindow()
32+
self.window2 = AnotherWindow()
33+
34+
l = QVBoxLayout()
35+
button1 = QPushButton("Push for Window 1")
36+
button1.clicked.connect(lambda checked: self.toggle_window(self.window1))
37+
l.addWidget(button1)
38+
39+
button2 = QPushButton("Push for Window 2")
40+
button2.clicked.connect(lambda checked: self.toggle_window(self.window2))
41+
l.addWidget(button2)
42+
43+
w = QWidget()
44+
w.setLayout(l)
45+
self.setCentralWidget(w)
46+
47+
def toggle_window(self, window):
48+
if window.isVisible():
49+
window.hide()
50+
51+
else:
52+
window.show()
53+
54+
55+
app = QApplication(sys.argv)
56+
w = MainWindow()
57+
w.show()
58+
app.exec_()
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import sys
2+
from random import randint
3+
4+
from PySide6.QtWidgets import (
5+
QApplication,
6+
QLabel,
7+
QMainWindow,
8+
QPushButton,
9+
QVBoxLayout,
10+
QWidget,
11+
)
12+
13+
14+
class AnotherWindow(QWidget):
15+
"""
16+
This "window" is a QWidget. If it has no parent,
17+
it will appear as a free-floating window.
18+
"""
19+
20+
def __init__(self):
21+
super().__init__()
22+
layout = QVBoxLayout()
23+
self.label = QLabel("Another Window % d" % randint(0, 100))
24+
layout.addWidget(self.label)
25+
self.setLayout(layout)
26+
27+
28+
class MainWindow(QMainWindow):
29+
def __init__(self):
30+
super().__init__()
31+
self.window1 = AnotherWindow()
32+
self.window2 = AnotherWindow()
33+
34+
layout = QVBoxLayout()
35+
button1 = QPushButton("Push for Window 1")
36+
button1.clicked.connect(
37+
lambda checked: self.toggle_window(self.window1),
38+
)
39+
layout.addWidget(button1)
40+
41+
button2 = QPushButton("Push for Window 2")
42+
button2.clicked.connect(
43+
lambda checked: self.toggle_window(self.window2),
44+
)
45+
layout.addWidget(button2)
46+
47+
w = QWidget()
48+
w.setLayout(layout)
49+
self.setCentralWidget(w)
50+
51+
def toggle_window(self, window):
52+
if window.isVisible():
53+
window.hide()
54+
else:
55+
window.show()
56+
57+
58+
app = QApplication(sys.argv)
59+
window = MainWindow()
60+
window.show()
61+
app.exec()
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import sys
2+
from random import randint
3+
4+
from PySide6.QtWidgets import (
5+
QApplication,
6+
QLabel,
7+
QMainWindow,
8+
QPushButton,
9+
QVBoxLayout,
10+
QWidget,
11+
)
12+
13+
14+
class AnotherWindow(QWidget):
15+
"""
16+
This "window" is a QWidget. If it has no parent,
17+
it will appear as a free-floating window.
18+
"""
19+
20+
def __init__(self):
21+
super().__init__()
22+
layout = QVBoxLayout()
23+
self.label = QLabel("Another Window % d" % randint(0, 100))
24+
layout.addWidget(self.label)
25+
self.setLayout(layout)
26+
27+
28+
class MainWindow(QMainWindow):
29+
def __init__(self):
30+
super().__init__()
31+
self.window1 = AnotherWindow()
32+
self.window2 = AnotherWindow()
33+
34+
layout = QVBoxLayout()
35+
button1 = QPushButton("Push for Window 1")
36+
button1.clicked.connect(self.toggle_window1)
37+
layout.addWidget(button1)
38+
39+
button2 = QPushButton("Push for Window 2")
40+
button2.clicked.connect(self.toggle_window2)
41+
layout.addWidget(button2)
42+
43+
w = QWidget()
44+
w.setLayout(layout)
45+
self.setCentralWidget(w)
46+
47+
def toggle_window1(self, checked):
48+
if self.window1.isVisible():
49+
self.window1.hide()
50+
else:
51+
self.window1.show()
52+
53+
def toggle_window2(self, checked):
54+
if self.window2.isVisible():
55+
self.window2.hide()
56+
else:
57+
self.window2.show()
58+
59+
60+
app = QApplication(sys.argv)
61+
window = MainWindow()
62+
window.show()
63+
app.exec()
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import sys
2+
3+
from PySide6.QtWidgets import (
4+
QApplication,
5+
QLabel,
6+
QMainWindow,
7+
QPushButton,
8+
QVBoxLayout,
9+
QWidget,
10+
)
11+
12+
13+
class AnotherWindow(QWidget):
14+
"""
15+
This "window" is a QWidget. If it has no parent, it
16+
will appear as a free-floating window as we want.
17+
"""
18+
19+
def __init__(self):
20+
super().__init__()
21+
layout = QVBoxLayout()
22+
self.label = QLabel("Another Window")
23+
layout.addWidget(self.label)
24+
self.setLayout(layout)
25+
26+
27+
class MainWindow(QMainWindow):
28+
def __init__(self):
29+
super().__init__()
30+
self.button = QPushButton("Push for Window")
31+
self.button.clicked.connect(self.show_new_window)
32+
self.setCentralWidget(self.button)
33+
34+
def show_new_window(self, checked):
35+
self.w = AnotherWindow()
36+
self.w.show()
37+
38+
39+
app = QApplication(sys.argv)
40+
window = MainWindow()
41+
window.show()
42+
app.exec()
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import sys
2+
from random import randint
3+
4+
from PySide6.QtWidgets import (
5+
QApplication,
6+
QLabel,
7+
QMainWindow,
8+
QPushButton,
9+
QVBoxLayout,
10+
QWidget,
11+
)
12+
13+
14+
class AnotherWindow(QWidget):
15+
"""
16+
This "window" is a QWidget. If it has no parent, it
17+
will appear as a free-floating window as we want.
18+
"""
19+
20+
def __init__(self):
21+
super().__init__()
22+
layout = QVBoxLayout()
23+
self.label = QLabel("Another Window % d" % randint(0, 100))
24+
layout.addWidget(self.label)
25+
self.setLayout(layout)
26+
27+
28+
class MainWindow(QMainWindow):
29+
def __init__(self):
30+
super().__init__()
31+
self.w = AnotherWindow()
32+
self.button = QPushButton("Push for Window")
33+
self.button.clicked.connect(self.show_new_window)
34+
self.setCentralWidget(self.button)
35+
36+
def show_new_window(self, checked):
37+
self.w.show()
38+
39+
40+
app = QApplication(sys.argv)
41+
window = MainWindow()
42+
window.show()
43+
app.exec()
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import sys
2+
from random import randint
3+
4+
from PySide6.QtWidgets import (
5+
QApplication,
6+
QLabel,
7+
QMainWindow,
8+
QPushButton,
9+
QVBoxLayout,
10+
QWidget,
11+
)
12+
13+
14+
class AnotherWindow(QWidget):
15+
"""
16+
This "window" is a QWidget. If it has no parent, it
17+
will appear as a free-floating window as we want.
18+
"""
19+
20+
def __init__(self):
21+
super().__init__()
22+
layout = QVBoxLayout()
23+
self.label = QLabel("Another Window % d" % randint(0, 100))
24+
layout.addWidget(self.label)
25+
self.setLayout(layout)
26+
27+
28+
class MainWindow(QMainWindow):
29+
def __init__(self):
30+
super().__init__()
31+
self.w = None # No external window yet.
32+
self.button = QPushButton("Push for Window")
33+
self.button.clicked.connect(self.show_new_window)
34+
self.setCentralWidget(self.button)
35+
36+
def show_new_window(self, checked):
37+
if self.w is None:
38+
self.w = AnotherWindow()
39+
self.w.show()
40+
41+
42+
app = QApplication(sys.argv)
43+
window = MainWindow()
44+
window.show()
45+
app.exec()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /