在pyqt5设计的GUI中如何通过已有的控件添加其他控件

由于课题需要开发一个桌面软件,软件中有一个功能是能够通过已有的控件“按钮”添加“文本框”,一头雾水,疯狂找资料,终于找到可以借鉴的资料,在此非常感谢原博主的代码,以下为原博文:
原文链接

如果需要通过添加其他控件,可进行类似操作

以下为我修改的操作代码,后续会继续修改:

from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QFrame,QTextEdit
import sys

class ui(QWidget):
    def __init__(self):
        super().__init__()
        self.show_ui()
        self.show_te()

    def show_ui(self, y = 50):
        self.pu = QPushButton(self)
        self.pu.setText('按钮')
        self.pu.setGeometry(50, y, 100, 50)
        self.pu.clicked.connect(self.mousePressEvent)

    def show_te(self,y=150):
        self.te = QTextEdit(self)
        self.te.setText('文本框')
        self.te.setGeometry(150, y, 50,50)
        self.te.setVisible(False) #设置为不显示

    def mousePressEvent(self, QMouseEvent): #鼠标触发
        self.show_te(70)
        self.te.setVisible(True) # 只有设置为True才能显示,之前默认都是显示的,但是在这添加就默认不显示了
        print('文本框')   #验证事件是否触发

if __name__=='__main__':
    app = QApplication(sys.argv)
    u = ui()
    u.show()
    sys.exit(app.exec_())

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在动图上添加控件,可以使用QGraphicsView和QGraphicsScene来实现。首先,需要创建一个QGraphicsView对象,并将其设置为动图的父对象。然后,创建一个QGraphicsScene对象,将其设置为QGraphicsView的场景。接着,可以在场景添加需要的控件,例如QPushButton、QLabel等控件,使用QGraphicsScene的addItem()函数添加到场景。 下面是一个简单的示例代码: ```python import sys from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import * class MyScene(QGraphicsScene): def __init__(self, parent=None): super().__init__(parent) # 添加一个按钮控件 self.button = QPushButton("Click me") self.button.setGeometry(QRectF(0, 0, 100, 30)) self.addItem(self.button) class MyView(QGraphicsView): def __init__(self, parent=None): super().__init__(parent) # 设置场景 self.scene = MyScene(self) self.setScene(self.scene) # 设置动画 self.animation = QPropertyAnimation(self.scene.button, b"pos") self.animation.setDuration(5000) self.animation.setStartValue(QPointF(0, 0)) self.animation.setEndValue(QPointF(200, 200)) self.animation.start() if __name__ == "__main__": app = QApplication(sys.argv) view = MyView() view.show() sys.exit(app.exec_()) ``` 在这个示例代码,我们创建了一个QGraphicsView对象,并将其设置为动图的父对象。然后,创建了一个MyScene对象作为QGraphicsView的场景,并在场景添加一个QPushButton控件。最后,设置了一个QPropertyAnimation动画,将按钮控件从左上角移动到右下角。 请注意,在动图添加控件时,需要将控件添加到场景,而不是直接添加到QGraphicsView。因此,需要自定义一个QGraphicsScene对象,并在其添加控件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值