利用python在ypqt中点击一个按钮出现两个子按钮,点击一个子按钮触发一个函数
时间: 2024-05-04 12:15:53 浏览: 18
可以使用Qt的信号与槽机制来实现这个功能。首先,在Qt Designer中设计好按钮和子按钮,并给它们分别设置好相应的名称和对象名称。然后,在python代码中,通过以下代码将子按钮添加到主按钮中:
```
sub_btn1 = QPushButton("子按钮1", self.main_btn)
sub_btn2 = QPushButton("子按钮2", self.main_btn)
sub_btn1.move(0, 30)
sub_btn2.move(0, 60)
```
这样,子按钮就会随着主按钮的移动而移动。接下来,为子按钮分别连接相应的槽函数:
```
sub_btn1.clicked.connect(self.func1)
sub_btn2.clicked.connect(self.func2)
```
这样,当点击子按钮1时,会触发self.func1函数,点击子按钮2时,会触发self.func2函数。
相关问题
利用python实现pyqt中点击一个按钮后出现两个子按钮
可以通过在按钮的clicked信号中添加槽函数,在槽函数中创建两个子按钮并显示出来。具体实现可以参考以下代码:
```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
self.btn = QPushButton('Click me!', self)
self.btn.clicked.connect(self.showSubButtons)
self.vbox = QVBoxLayout()
self.vbox.addWidget(self.btn)
self.setLayout(self.vbox)
self.setGeometry(300, 300, 300, 200)
self.setWindowTitle('Two Sub-Buttons')
def showSubButtons(self):
btn1 = QPushButton('Sub-Button 1', self)
btn2 = QPushButton('Sub-Button 2', self)
self.vbox.addWidget(btn1)
self.vbox.addWidget(btn2)
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
ex.show()
sys.exit(app.exec_())
```
当点击主按钮时,会在窗口中动态添加两个子按钮。
pyqt如何实现点击一个按钮出现两个子按钮python
在 PyQt 中可以使用 QToolButton 控件来实现一个按钮下拉出现多个子按钮的效果。具体实现步骤如下:
1. 创建一个 QToolButton 控件作为父按钮。
2. 创建多个 QAction 控件作为子按钮。
3. 将这些 QAction 控件添加到一个 QMenu 控件中。
4. 将 QMenu 控件设置为父按钮的下拉菜单。
5. 将父按钮的 clicked 信号连接到一个槽函数,在槽函数中显示 QMenu 控件。
以下是一个简单的示例代码:
```python
from PyQt5.QtWidgets import QApplication, QMainWindow, QToolButton, QAction, QMenu
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# 创建一个父按钮
button = QToolButton(self)
button.setText('Click Me')
# 创建两个子按钮
action1 = QAction('Button 1', self)
action2 = QAction('Button 2', self)
# 将子按钮添加到菜单中
menu = QMenu(self)
menu.addAction(action1)
menu.addAction(action2)
# 设置父按钮的下拉菜单
button.setMenu(menu)
# 连接父按钮的 clicked 信号到槽函数
button.clicked.connect(self.showMenu)
def showMenu(self):
# 在槽函数中显示菜单
button = self.sender()
menu = button.menu()
menu.exec(button.mapToGlobal(button.rect().bottomLeft()))
if __name__ == '__main__':
app = QApplication([])
window = MainWindow()
window.show()
app.exec_()
```
当你点击父按钮时,将会显示一个下拉菜单,里面包含了两个子按钮。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)