如何用pyqt5,用两个按键连接两个class
时间: 2023-05-19 09:04:46 浏览: 106
您可以使用以下代码来使用 PyQt5 连接两个类:
```python
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton
import sys
class Class1:
def __init__(self):
print("Class1 initialized")
class Class2:
def __init__(self):
print("Class2 initialized")
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'PyQt5 button example'
self.left = 10
self.top = 10
self.width = 320
self.height = 200
self.initUI()
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
button1 = QPushButton('Class1', self)
button1.setToolTip('This is an example button')
button1.move(100, 70)
button1.clicked.connect(self.on_button1_clicked)
button2 = QPushButton('Class2', self)
button2.setToolTip('This is an example button')
button2.move(100, 100)
button2.clicked.connect(self.on_button2_clicked)
self.show()
def on_button1_clicked(self):
c1 = Class1()
def on_button2_clicked(self):
c2 = Class2()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
```
这个例子中,我们创建了两个类 Class1 和 Class2。然后,我们创建了一个 PyQt5 应用程序,其中包含两个按钮,分别连接到 Class1 和 Class2。当用户单击按钮时,将创建相应的类实例。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)