编程语言
首页 > 编程语言> > 小渣渣学习笔记 python day48【PyQt5 树控件 】

小渣渣学习笔记 python day48【PyQt5 树控件 】

作者:互联网

#表格中显示 上下文菜单ContextMenu

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
class QTableWidgetMenuDemo(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('表格中上下文菜单演示')
        self.resize(500,400)
        self.initUI()
    def initUI(self):
        layout = QVBoxLayout(self)
        self.tableWidget = QTableWidget()
        self.tableWidget.setRowCount(4)
        self.tableWidget.setColumnCount(3)
        self.tableWidget.setHorizontalHeaderLabels(['name','sex','age'])
        layout.addWidget(self.tableWidget)

        item = QTableWidgetItem('tom')
        self.tableWidget.setItem(0,0,item)
        item = QTableWidgetItem('男')
        self.tableWidget.setItem(0, 1, item)
        item = QTableWidgetItem('25')
        self.tableWidget.setItem(0, 2, item)

        self.tableWidget.setContextMenuPolicy(Qt.CustomContextMenu)
        self.tableWidget.customContextMenuRequested.connect(self.generateMenu)
    def generateMenu(self,pos): #单击 参数是鼠标点击的位置
        print(pos)
        screenPos = self.tableWidget.mapToGlobal(pos)
        for i in self.tableWidget.selectionModel().selection().indexes():
            rowNo = i.row()
        if rowNo <2:
            menu = QMenu()
            item1 = menu.addAction('menu1')
            item2 = menu.addAction('menu2')
            item3 = menu.addAction('menu3')
            action = menu.exec(screenPos)
            if action == item1:
                print('选择了第一个按钮',self.tableWidget.item(rowNo,0).text(),
                      self.tableWidget.item(rowNo,1).text(),
                      self.tableWidget.item(rowNo,2).text())
            elif action == item2:
                print('选择了第一个按钮',self.tableWidget.item(rowNo,0).text(),
                      self.tableWidget.item(rowNo,1).text(),
                      self.tableWidget.item(rowNo,2).text())
            elif action == item3:
                print('选择了第一个按钮',self.tableWidget.item(rowNo,0).text(),
                      self.tableWidget.item(rowNo,1).text(),
                      self.tableWidget.item(rowNo,2).text())
            else:return

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = QTableWidgetMenuDemo()
    main.show()
    sys.exit(app.exec_())
# 树控件 QTreeWidget
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys


class QTableWidgetMenuDemo(QMainWindow):  # 这里用QMainWindow
    def __init__(self, parent=None):
        super(QTableWidgetMenuDemo, self).__init__(parent)
        self.setWindowTitle('表格中上下文菜单演示')
        self.resize(500, 400)
        layout = QVBoxLayout(self)
        self.treeWidget = QTreeWidget() #实例化控件
        self.setCentralWidget(self.treeWidget) #设置为主窗口
        self.treeWidget.setColumnCount(2) #设置2列

        root = QTreeWidgetItem(self.treeWidget) #设置根节点
        root.setText(0,'根节点')
        root.setIcon(0,QIcon('./back.png')) #加图片
        self.treeWidget.setColumnWidth(0,220) #设置宽度

        child1 = QTreeWidgetItem(root) #设置子节点1
        child1.setText(0,'子节点1')
        child1.setText(1,'子节点1的数据')
        child1.setIcon(0,QIcon('./download.png'))
        child1.setCheckState(0,Qt.Checked)

        child2 = QTreeWidgetItem(root) #设置子节点2
        child2.setText(0, '子节点2')
        child2.setText(1, '子节点2的数据')
        child2.setIcon(0, QIcon('./rainbow.png'))
        child2.setCheckState(0, Qt.Checked)

        child3 = QTreeWidgetItem(child2) #为子节点2添加子节点
        child3.setText(0, '子节点3')
        child3.setText(1, '子节点3的数据')
        child3.setIcon(0, QIcon('./rainbow.png'))
        child3.setCheckState(0, Qt.Checked)

        self.treeWidget.expandAll()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = QTableWidgetMenuDemo()
    main.show()
    sys.exit(app.exec_())
# 为树节点添加响应事件
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys
class QTableWidgetMenuDemo(QMainWindow):  # 这里用QMainWindow
    def __init__(self, parent=None):
        super(QTableWidgetMenuDemo, self).__init__(parent)
        self.setWindowTitle('表格中上下文菜单演示')
        self.resize(500, 400)
        layout = QVBoxLayout(self)
        self.treeWidget = QTreeWidget() #实例化控件
        self.treeWidget.setHeaderLabels(['节点','描述'])
        self.setCentralWidget(self.treeWidget) #设置为主窗口
        self.treeWidget.setColumnCount(2) #设置2列

        root = QTreeWidgetItem(self.treeWidget) #设置根节点
        root.setText(0,'根节点')
        root.setIcon(0,QIcon('./back.png')) #加图片
        self.treeWidget.setColumnWidth(0,220) #设置宽度

        child1 = QTreeWidgetItem(root) #设置子节点1
        child1.setText(0,'子节点1')
        child1.setText(1,'子节点1的数据')
        child1.setIcon(0,QIcon('./download.png'))
        child1.setCheckState(0,Qt.Checked)

        self.treeWidget.clicked.connect(self.onTreeClicked)
        self.treeWidget.expandAll()

    def onTreeClicked(self,index):
        item = self.treeWidget.currentItem()
        print(index.row())
        print('key=%s,value=%s' %(item.text(0),item.text(1)))

if __name__ == '__main__':
    app = QApplication(sys.argv)
    main = QTableWidgetMenuDemo()
    main.show()
    sys.exit(app.exec_())

标签:__,控件,python,self,小渣,tableWidget,treeWidget,import,节点
来源: https://www.cnblogs.com/94xzz/p/13597588.html