代码拉取完成,页面将自动刷新
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'src_24_TreeWidget.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QTreeWidgetItem
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(531, 413)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.treeWidget = QtWidgets.QTreeWidget(self.centralwidget)
self.treeWidget.setGeometry(QtCore.QRect(0, 0, 531, 421))
self.treeWidget.setObjectName("treeWidget")
# self.treeWidget.headerItem().setText(0, "1")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.treeWidget.clicked.connect(self.gettreetext)
self.treeWidget.setColumnCount(2) # 设置树结构中的列数
self.treeWidget.setAlternatingRowColors(True) # 设置隔行变色
self.treeWidget.setHeaderLabels(['姓名', '职务']) # 设置列标题名
root = QTreeWidgetItem(self.treeWidget) # 创建节点
root.setText(0, "组织结构") # 设置顶级节点文本
# 定义字典,存储树结构中显示的数据
dict = {'任正非': '华为董事长', '马云': '阿里巴巴创始人', '马化腾': '腾讯CEO', '李彦宏': '百度CEO', '董明珠': '格力董事长'}
for key, value in dict.items(): # 遍历字典
child = QTreeWidgetItem(root) # 创建字节点
child.setText(0, key) # 设置第1列的值
child.setText(1, value) # 设置第2列的值
self.treeWidget.addTopLevelItem(root) # 将创建的数节点添加到树控件中
self.treeWidget.expandAll() # 展开所有树节点
child.setCheckState(0,QtCore.Qt.Checked) # 为节点设置复选框,并且选中
# 为节点设置图标
if key == '任正非':
child.setIcon(0, QtGui.QIcon('./图标/华为.jpg'))
elif key == '马云':
child.setIcon(0, QtGui.QIcon('./图标/阿里巴巴.jpg'))
elif key == '马化腾':
child.setIcon(0, QtGui.QIcon('./图标/腾讯.png'))
elif key == '董明珠':
child.setIcon(0, QtGui.QIcon('./图标/格力.jpeg'))
elif key == '李彦宏':
child.setIcon(0, QtGui.QIcon('./图标/百度.jpg'))
def gettreetext(self, index):
item = self.treeWidget.currentItem() # 获取当前选中项
# 弹出提示框,显示选中的文本
QtWidgets.QMessageBox.information(Mainwindow, '提示', '您选择的是:%s--%s'%(item.text(0), item.text(1)),
QtWidgets.QMessageBox.Ok)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Mainwindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(Mainwindow)
Mainwindow.show()
sys.exit(app.exec_())
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。