pyqt 自定义组合控件

37 篇文章 1 订阅
21 篇文章 0 订阅

comp_file.py

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from PyQt4 import QtGui, QtCore

class MyLineEdit(QtGui.QLineEdit):
        def __init__( self, parent=None ):
            super(MyLineEdit, self).__init__(parent)
            self.setDragEnabled(True)
        def dragEnterEvent( self, event ):
            data = event.mimeData()
            urls = data.urls()
            if ( urls and urls[0].scheme() == 'file' ):
                event.acceptProposedAction()
        def dragMoveEvent( self, event ):
            data = event.mimeData()
            urls = data.urls()
            if ( urls and urls[0].scheme() == 'file' ):
                event.acceptProposedAction()

        def dropEvent( self, event ):
            data = event.mimeData()
            urls = data.urls()
            if ( urls and urls[0].scheme() == 'file' ):
                filepath = str(urls[0].path())[1:]
                filepath=filepath.decode('utf-8')
                self.setText(filepath)

class CompTools(QtGui.QWidget):
    def __init__(self,label="label",content='',button=u"浏览"):
        super(CompTools, self).__init__()
        self._label=label
        self._line_content=content
        self._button=button
        self.initUI()
    def initUI(self):
        self.mylabel=QtGui.QLabel(self._label)
        self.mylineEdit=MyLineEdit()
        self.mylineEdit.setPlaceholderText(self._line_content)
        self.mybutton=QtGui.QPushButton(self._button)
        hbox = QtGui.QHBoxLayout()
        hbox.addWidget(self.mylabel)
        hbox.addWidget(self.mylineEdit)
        hbox.addWidget(self.mybutton)
        self.setLayout(hbox)
        self.setGeometry(300, 300, 390, 210)
        self.setWindowTitle('group widget')

        self.mybutton.clicked.connect(self.change_filepath)
        
        
        #self.show()
    def change_label(self,value):
        self.mylabel.setText(value)
    def change_content(self,value):
        self.mylineEdit.setPlaceholderText(value)
    def change_button(self,value):
        self.mybutton.setText(value)
    def change_filepath(self):
        filename=QtGui.QFileDialog.getExistingDirectory(self,                                             
                  u"choose directory",
                  r"C:\Users\Administrator\Desktop")
        self.mylineEdit.setText(filename)
        
def main():
    app = QtGui.QApplication(sys.argv)
    ex = CompTools(label="data:",content='data path (can drop)',button=u"浏览")
    ex.show()
    sys.exit(app.exec_())

    
if __name__ == '__main__':
    main()

在这里插入图片描述
之后使用时就可以把他们三个当成一个来使用。

new_tool.py

#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
from PyQt4 import QtGui, QtCore
from comp_file import CompTools


class Compview(QtGui.QWidget):
    def __init__(self):
        super(Compview, self).__init__()
        self.initUI()
    def initUI(self):
        self.myTools=CompTools(label="data:",content='data path (can drop)',button=u"浏览")
        
        #self.myTools.change_label("path:")
        
        self.myTree=QtGui.QTextEdit()
        ok=QtGui.QPushButton(u"确定")
        no=QtGui.QPushButton(u"取消")
        lbox = QtGui.QHBoxLayout()
        lbox.addWidget(ok)
        lbox.addWidget(no)
        
        hbox = QtGui.QVBoxLayout()
        hbox.addWidget(self.myTools)
        hbox.addWidget(self.myTree)

        lay=QtGui.QVBoxLayout()
        lay.addLayout(hbox)
        lay.addLayout(lbox)
        self.setLayout(lay)
        
        self.setGeometry(250,150,600,450)
        self.setWindowTitle('new widget')   
        #self.show()
 
        
def main():
    app = QtGui.QApplication(sys.argv)
    ex = Compview()
    ex.show()
    sys.exit(app.exec_())

    
if __name__ == '__main__':
    main()

在这里插入图片描述

想要改变标签,只需要改变参数就可以了。
CompTools(label=“data:”,content=‘data path (can drop)’,button=u"浏览")
也可以使用chang函数进行修改。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

佐倉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值