python做界面 pyqt 和javafx对比_pyqt5之树 (实例教程)

本文通过一个实例展示了如何使用PyQT5创建一个树形界面,包括添加、修改、删除和查找功能,并与JavaFX进行了简单对比。通过创建QTreeWidget,设置表头和添加顶层节点,实现了树形结构。此外,还添加了工具栏来触发各项操作。
摘要由CSDN通过智能技术生成

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_()

分享到:

2017-11-22 16:09

浏览 6098

评论

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值