`
yunhaifeiwu
  • 浏览: 161326 次
  • 性别: Icon_minigender_1
  • 来自: 宁波
社区版块
存档分类
最新评论

pyqt5之树 (实例教程)

阅读更多
说明:
   python 3.5 ,pyqt5..


import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class TreeWidget(QMainWindow):
    myControls ={}
    def __init__(self,parent=None):
        QWidget.__init__(self,parent)
        self.setWindowTitle('TreeWidget')
        self.tree = QTreeWidget()
        self.myControls['tree']=self.tree
        self.tree.setColumnCount(2) # 说明是树形的表,
        self.tree.setHeaderLabels(['Key','Value']) # 是表,则有表头
        # 根节点的父是 QTreeWidget对象
        root= QTreeWidgetItem(self.tree)

        root.setText(0,'root')
        child1 = QTreeWidgetItem(root) #指出父结点
        child1.setText(0,'child1')
        child1.setText(1,'name1')
        child2 = QTreeWidgetItem(root)
        child2.setText(0,'child2')
        child2.setText(1,'name2')
        child3 = QTreeWidgetItem(root)
        child3.setText(0,'child3')
        child4 = QTreeWidgetItem(child3)
        child4.setText(0,'child4')
        child4.setText(1,'name4')
        #以下两句是主窗口的设置
        self.tree.addTopLevelItem(root)
        self.setCentralWidget(self.tree)

        #带图标是这形式QAction(QIcon("ss.png"), "add", self)
        addAction = QAction( "增加", self)
        addAction.triggered.connect(self.on_addAction_triggered)

        editAction = QAction("修改", self)
        editAction.triggered.connect(self.on_editdAction_triggered)

        deleteAction = QAction("删除", self)
        deleteAction.triggered.connect(self.on_deleteAction_triggered)

        findAction = QAction("查找", self)
        findAction.triggered.connect(self.on_findAction_triggered)

        toolbar = self.addToolBar("aa")
        toolbar.addAction(addAction)
        toolbar.addAction(editAction)
        toolbar.addAction(deleteAction)
        toolbar.addAction(findAction)

    def on_addAction_triggered(self):
        currNode = self.tree.currentItem()
        addChild1 =QTreeWidgetItem()
        addChild1.setText(0,'addChild1_key')
        addChild1.setText(1, 'addChild1_val')
        currNode.addChild(addChild1)

    def on_editdAction_triggered(self):
        currNode = self.tree.currentItem()
        currNode.setText(0,'editkey')
        currNode.setText(1, 'editvalue')

    def on_deleteAction_triggered(self):
        currNode = self.tree.currentItem()
        parent1=currNode.parent();
        parent1.removeChild (currNode)

    def on_findAction_triggered(self):
         #MatchRegExp 正则查找,MatchRecursive递归遍历,最后是指树表的第几列值
         #本例是 查找第0中 所有开头含有”child“文字的节点
        nodes=self.tree.findItems ("^child[\w|\W]*",Qt.MatchRegExp | Qt.MatchRecursive  ,0)
        for node in nodes :
            QMessageBox.information(self, '', node.text(0))


app = QApplication(sys.argv)
tp = TreeWidget()
tp.show()
app.exec_()

分享到:
评论

相关推荐

    pyqt5 python Gui入门教程 中文PDF高清版

    这是一个关于pyqt5-python-Gui的入门教程,干货 ,新手必备,pyqt5 GUI开发教程,详细介绍了基本的程序结构和细小知识点。简单好用,GUI指导,需要的朋友可下载试试! pyqt5新手入门教程,以章节的形式加以讲述,每...

    python的pyqt5,常用实例代码

    pyqt5的基础实例代码,可供初学参考,直接运行即可,内含:窗体标签栏,图标应用,测试文本框,按钮实现,消息窗口,状态栏,菜单栏,工具栏,不同控件组合,网格布局,信号槽,时间发送,颜色选择对话框,等等。

    PyQt5入门教程

    PyQt5入门教程,适合初学者,写的非常详细,欢迎下载评论

    pythonqt5教程从零开始-pyQt5QtDesigner简易入门教程.pdf

    pythonqt5教程从零开始_pyQt5QtDesigner简易⼊门教程 python3.6 & pyQt5 & QtDesigner 简易⼊门教程 1. python 官⽹下载安装python3.6并配置好环境; 2.cmd下 运⾏: pip install PyQt5 安装PyQt库; 3.cmd下运⾏: ...

    PyQt5中文教程 chinese

    PyQt5中文教程,本教程的目的是带领你入门PyQt5。教程内所有代码都在Linux上测试 通过。PyQt4 教程是PyQt4的教程,PyQt4是一个Python(同时支持 2和3)版的Qt库。

    Python3.x+Pyqt5实现界面左侧树形导航栏

    Python3.x+Pyqt5实现界面左侧树形导航栏;主要用到的工具是Pyqt5的QTreeWidget。

    Python Gui编程,PyQt5下载安装教程

    Python Gui编程,PyQt5下载安装教程 Python Gui编程,PyQt5下载安装教程 Python Gui编程,PyQt5下载安装教程 Python Gui编程,PyQt5下载安装教程 Python Gui编程,PyQt5下载安装教程 Python Gui编程,PyQt5下载安装教程 ...

    PyQt5中文教程.pdf

    PyQt5中文教程,快速上手,内有源码及详细解析。 这个教程比较好的地方是,能讲解每一段代码的含义。 虽然PyQt的函数命名已经非常语义化了,但是对于新手来说,有这一步还是更好的。 所以如果你选择了这篇教程,能...

    PyQt5 tutorial PyQt5教程英文版

    PyQt5 Tutorial Documentation Release 1.0 Andrew Steele March 01, 2016 1 Introduction 3 2 Hello World 5 3 Window 7 4 BoxLayout 9 5 GridLayout 11 6 Label 13 7 PushButton 15 8 RadioButton 17 9 ...

    pyqt5+yolov5+python

    内容概要:多目标检测+pyqt5+yolov5+python+实现yolov5GUI开发。 适用人群:刚刚接触pyqt5开发以及刚刚接触yolo算法的初学者,想要有现成项目练手的人。 从本项目中能学到什么:pyqt5的开发、如何界面设计与后端逻辑...

    pyqt5基础教程

    pyqt5基础教程 ,英文版 pdf,可以用于PYQT5的基础学习。包含有游戏一款游戏开发。

    python PyQt5基础图形实例代码

    参考PyQt5官方网站的基础图形绘制文档,实现了各种基础图元参数设置的图形绘制,包含有常见基础控件。原文档讲的很好,但是给出的示例代码完全是用C++写的,本萌新看不懂!只好自己学着用PyQt5重新写了一遍,(肯定...

    python pyqt5 gui 入门教程 干货

    python pyqt5 gui 入门教程 干货 ,新手必备,pyqt5 GUI开发教程,详细介绍了基本的程序结构和细小知识点。

    PyQt5最新中文__英文实例教程_PDF和源码

    该压缩包是最新的PyQt5最新中文__英文实例教程_PDF和源码,里面包含有PYQT5的详细中英文教程PDF版和实例代码,欢迎大家下载,希望得到大家的好评哈!

    PyQt5入门教程.pdf

    PyQt5入门.pdf 原始下载要18分,我只要2分!18分太多了

    PyQt5 tutorial pdf PyQt5教程英文版

    这个PyQt5 tutorial是本人从国外网站找到的教程,原始格式是html网页版,本人把html格式转成了pdf格式并修改了一些格式,特此分享。2016-02-20 by yinkaisheng PyQt5 tutorial.pdf 123页 This is PyQt5 tutorial. ...

    PyQt5-QWidgets部件进阶教程之图标资源

    PyQt5-QWidgets部件进阶教程之图标的相关资源

    pyqt5和pyqt5-sip

    PyQt5和Pyqt5-sip直接下载太慢了。。。。,所以自己下载了下来 输入pip install 文件>安装

Global site tag (gtag.js) - Google Analytics