pyqt 自定义组合控件

xiaoxiao2025-06-05  57

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函数进行修改。

转载请注明原文地址: https://www.6miu.com/read-5031338.html

最新回复(0)