pyqt5创建一个新窗口
时间: 2024-04-23 20:21:11 浏览: 10
在PyQt5中创建一个新窗口可以通过以下步骤实现:
1. 导入必要的模块:
```python
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget
```
2. 创建一个继承自QMainWindow的主窗口类:
```python
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("主窗口")
```
3. 创建一个继承自QWidget的新窗口类:
```python
class NewWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("新窗口")
```
4. 在主窗口类中添加一个按钮,并连接到打开新窗口的槽函数:
```python
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("主窗口")
self.button = QPushButton("打开新窗口", self)
self.button.clicked.connect(self.open_new_window)
def open_new_window(self):
self.new_window = NewWindow()
self.new_window.show()
```
5. 创建应用程序对象并运行:
```python
if __name__ == "__main__":
app = QApplication([])
main_window = MainWindow()
main_window.show()
app.exec_()
```
这样,当点击主窗口中的按钮时,就会打开一个新的窗口。