Creating a new window
basic/windows_1.py
import sys
from PyQt6.QtWidgets import (
QApplication,
QLabel,
QMainWindow,
QPushButton,
QVBoxLayout,
QWidget,
)
class AnotherWindow(QWidget):
"""
This "window" is a QWidget. If it has no parent, it
will appear as a free-floating window.
"""
def __init__(self):
super().__init__()
layout = QVBoxLayout()
self.label = QLabel("Another Window")
layout.addWidget(self.label)
self.setLayout(layout)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.button = QPushButton("Push for Window")
self.button.clicked.connect(self.show_new_window)
self.setCentralWidget(self.button)
def show_new_window(self, checked):
w = AnotherWindow()
w.show()
app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec()
basic/windows_1b.py
self객체를 꼭 생성하라!!!
def show_new_window (self, checked):
self.w = AnotherWindow()
self.w.show()
basic/windows_3.py
if you want to show a particular plot, or log output.
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.w = None # No external window yet.
self.button = QPushButton("Push for Window")
self.button.clicked.connect(self.show_new_window)
self.setCentralWidget(self.button)
def show_new_window(self, checked):
if self.w is None:
self.w = AnotherWindow()
self.w.show()
Closing a window
basic/windows_4.py
def show_new_window(self, checked):
if self.w is None:
self.w = AnotherWindow()
self.w.show()
else:
self.w = None # Discard reference, close window.
동일 기능
def show_new_window(self, checked):
if self.w is None:
self.w = AnotherWindow()
self.w.show()
else:
self.w.close()
self.w = None # Discard reference, close window.
Persistent windows
basic/windows_5.py
import sys
from random import randint
from PyQt6.QtWidgets import (
QApplication,
QLabel,
QMainWindow,
QPushButton,
QVBoxLayout,
QWidget,
)
class AnotherWindow(QWidget):
"""
This "window" is a QWidget. If it has no parent, it
will appear as a free-floating window.
"""
def __init__(self):
super().__init__()
layout = QVBoxLayout()
self.label = QLabel("Another Window % d" % randint(0, 100))
layout.addWidget(self.label)
self.setLayout(layout)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.w = AnotherWindow()
self.button = QPushButton("Push for Window")
self.button.clicked.connect(self.show_new_window)
self.setCentralWidget(self.button)
def show_new_window(self, checked):
self.w.show()
app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec()
Showing & hiding windows
basic/windows_6.py
def toggle_window(self, checked):
if self.w.isVisible():
self.w.hide()
else:
self.w.show()
Connecting signals between windows
신호와 슬롯을 사용하여 위젯을 직접 연결하는 방법을 보았습니다. 창을 가로질러 신호를 연결할 때도 동일한 원리가 적용됩니다 — 한 창의 신호를 다른 창의 슬롯에 연결할 수 있으며 슬롯에 액세스할 수 있기만 하면 됩니다.
basic/windows_7.py
상단 상자에 텍스트를 입력하면 레이블에 즉시 표시됩니다. 텍스트는 창이 숨겨져 있는 동안에도 업데이트됩니다 — 위젯의 상태를 업데이트하는 것은 위젯이 표시되는 것에 의존하지 않습니다.
import sys
from random import randint
from PyQt6.QtWidgets import (
QApplication,
QLabel,
QMainWindow,
QPushButton,
QVBoxLayout,
QWidget,
QLineEdit,
)
class AnotherWindow(QWidget):
"""
This "window" is a QWidget. If it has no parent, it
will appear as a free-floating window.
"""
def __init__(self):
super().__init__()
layout = QVBoxLayout()
self.label = QLabel("Another Window") # <2>
layout.addWidget(self.label)
self.setLayout(layout)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.w = AnotherWindow()
self.button = QPushButton("Push for Window")
self.button.clicked.connect(self.toggle_window)
self.input = QLineEdit()
self.input.textChanged.connect(self.w.label.setText) # <1>
layout = QVBoxLayout()
layout.addWidget(self.button)
layout.addWidget(self.input)
container = QWidget()
container.setLayout(layout)
self.setCentralWidget(container)
def toggle_window(self, checked):
if self.w.isVisible():
self.w.hide()
else:
self.w.show()
app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec()
댓글