pyqt5中的多个windows
Multiple windows in pyqt5
这个算法允许每次点击按钮时打开一个新的window,但是当再次点击时window关闭,我需要修改为点击它每次都会生成一个新的 window 而不会关闭前一个。
代码:
import sys
from random import randint
from PyQt5.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.window1 = AnotherWindow()
self.window2 = AnotherWindow()
l = QVBoxLayout()
button1 = QPushButton("Push for Window 1")
button1.clicked.connect(self.toggle_window1)
l.addWidget(button1)
button2 = QPushButton("Push for Window 2")
button2.clicked.connect(self.toggle_window2)
l.addWidget(button2)
w = QWidget()
w.setLayout(l)
self.setCentralWidget(w)
def toggle_window1(self, checked):
if self.window1.isVisible():
self.window1.hide()
else:
self.window1.show()
def toggle_window2(self, checked):
if self.window2.isVisible():
self.window2.hide()
else:
self.window2.show()
app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()
我尝试在每次给出按钮时更改新小部件的 windows 变量,但它不起作用
您的代码正在生成三个 windows 并使用 MainWindow 中的按钮 hide/show 其他两个 windows。要在按下按钮时生成新的 windows,您需要调用 AnotherWindow 的新实例并将它们存储在 MainWindow 中。
例如:
import sys
from random import randint
from PyQt5.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.windows = []
l = QVBoxLayout()
button1 = QPushButton("Push for new window")
button1.clicked.connect(self.open_newWindow)
l.addWidget(button1)
w = QWidget()
w.setLayout(l)
self.setCentralWidget(w)
def open_newWindow(self):
window = AnotherWindow()
self.windows.append(window)
window.show()
app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()
这个算法允许每次点击按钮时打开一个新的window,但是当再次点击时window关闭,我需要修改为点击它每次都会生成一个新的 window 而不会关闭前一个。
代码:
import sys
from random import randint
from PyQt5.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.window1 = AnotherWindow()
self.window2 = AnotherWindow()
l = QVBoxLayout()
button1 = QPushButton("Push for Window 1")
button1.clicked.connect(self.toggle_window1)
l.addWidget(button1)
button2 = QPushButton("Push for Window 2")
button2.clicked.connect(self.toggle_window2)
l.addWidget(button2)
w = QWidget()
w.setLayout(l)
self.setCentralWidget(w)
def toggle_window1(self, checked):
if self.window1.isVisible():
self.window1.hide()
else:
self.window1.show()
def toggle_window2(self, checked):
if self.window2.isVisible():
self.window2.hide()
else:
self.window2.show()
app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()
我尝试在每次给出按钮时更改新小部件的 windows 变量,但它不起作用
您的代码正在生成三个 windows 并使用 MainWindow 中的按钮 hide/show 其他两个 windows。要在按下按钮时生成新的 windows,您需要调用 AnotherWindow 的新实例并将它们存储在 MainWindow 中。
例如:
import sys
from random import randint
from PyQt5.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.windows = []
l = QVBoxLayout()
button1 = QPushButton("Push for new window")
button1.clicked.connect(self.open_newWindow)
l.addWidget(button1)
w = QWidget()
w.setLayout(l)
self.setCentralWidget(w)
def open_newWindow(self):
window = AnotherWindow()
self.windows.append(window)
window.show()
app = QApplication(sys.argv)
w = MainWindow()
w.show()
app.exec_()