码迷,mamicode.com
首页 > 其他好文 > 详细

PyQt5复杂控件(树控件、选项卡控件(滚动条控件、多文档控件、停靠控件)

时间:2020-02-13 12:47:44      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:颜色   setw   新建   http   复杂   教育   owa   tde   ini   

1、树控件的基本使用方法QTreeWidget
‘‘‘
QTreeWidget树控件的使用方法
添加图标,添加表格,添加复选框等
‘‘‘
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon,QBrush,QColor
import sys

class treewidget(QMainWindow):
def __init__(self):
super(treewidget,self).__init__()

self.setWindowTitle("树控件的基本用法")
self.resize(800,300)

#设置树控件
self.tree=QTreeWidget()
self.tree.setColumnCount(2) #制定树控件为两列
self.tree.setHeaderLabels(["key","value"]) #设置列标签

#添加根节点1
root=QTreeWidgetItem(self.tree)
root.setText(0,"根节点")
root.setIcon(0,QIcon("./image/1.png"))
self.tree.setColumnWidth(0,300)

#添加子节点1
n1=QTreeWidgetItem(root)
n1.setText(0,"子节点1")
n1.setText(1,"子节点的数据")
n1.setIcon(0,QIcon("./image/1.png"))
n1.setCheckState(0,Qt.Checked) #添加复选框
#添加子节点2
n2=QTreeWidgetItem(root)
n2.setText(0, "子节点2")
n2.setText(1, "子节点2的数据")
n2.setIcon(0, QIcon("./image/1.png"))
n2.setCheckState(0, Qt.Checked) # 添加复选框

#为子节点再添加子节点2-1
n3 = QTreeWidgetItem(n2)
n3.setText(0, "子节点2-1")
n3.setText(1, "子节点2-1的数据")
n3.setIcon(0, QIcon("./image/1.png"))
n3.setCheckState(0, Qt.Checked) # 添加复选框

self.tree.expandAll() #设置所有的节点为展开的状态
self.setCentralWidget(self.tree)

if __name__=="__main__":
app=QApplication(sys.argv)
p=treewidget()
p.show()
sys.exit(app.exec_())

技术图片

2、树控件添加响应事件
‘‘‘
树控件响应事件
‘‘‘
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import QIcon,QBrush,QColor
import sys

class treeevent(QMainWindow):
def __init__(self):
super(treeevent,self).__init__()

self.setWindowTitle("树控件的基本用法")
self.resize(800,300)

#创建树控件,设置列数为2
self.tree=QTreeWidget()
self.tree.setColumnCount(2)
self.tree.setHeaderLabels(["key","value"])

root=QTreeWidgetItem(self.tree)
root.setText(0,"root")
root.setText(1,"1")

c1=QTreeWidgetItem(root)
c1.setText(0,"child1")
c1.setText(1,"2")

c2=QTreeWidgetItem(c1)
c2.setText(0,"child2")
c2.setText(1,"3")
c3 = QTreeWidgetItem(c1)
c3.setText(0, "child3")
c3.setText(1, "4")

#为树节点设置信号与槽函数
self.tree.clicked.connect(self.ontreeclick)
self.setCentralWidget(self.tree)

def ontreeclick(self,index):
i=self.tree.currentItem()
print(index.row())
print(‘key=%s,value=%s‘ %(i.text(0),i.text(1)))

if __name__=="__main__":
app=QApplication(sys.argv)
p=treeevent()
p.show()
sys.exit(app.exec_())

技术图片

3、树控件增加、修改与删减节点
‘‘‘
树控件修改操作
‘‘‘
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import QIcon,QBrush,QColor
import sys

class treechange(QWidget):
def __init__(self):
super(treechange,self).__init__()

self.setWindowTitle("树控件的节点删减操作")
self.resize(800,300)

layout=QHBoxLayout()
add=QPushButton("添加节点")
update=QPushButton("修改节点")
delete=QPushButton("删除节点")

layout.addWidget(add)
layout.addWidget(update)
layout.addWidget(delete)

add.clicked.connect(self.add1)
update.clicked.connect(self.update1)
delete.clicked.connect(self.delete1)

# 创建树控件,设置列数为2
self.tree = QTreeWidget()
self.tree.setColumnCount(2)
self.tree.setHeaderLabels(["key", "value"])

root = QTreeWidgetItem(self.tree)
root.setText(0, "root")
root.setText(1, "1")

c1 = QTreeWidgetItem(root)
c1.setText(0, "child1")
c1.setText(1, "2")

c2 = QTreeWidgetItem(c1)
c2.setText(0, "child2")
c2.setText(1, "3")
c3 = QTreeWidgetItem(c1)
c3.setText(0, "child3")
c3.setText(1, "4")
# 为树节点设置信号与槽函数
self.tree.clicked.connect(self.ontreeclick)

layout1=QVBoxLayout(self)
layout1.addLayout(layout)
layout1.addWidget(self.tree)
self.setLayout(layout1)

#显示的系统的根目录
model = QDirModel() # 当前的系统model
tree = QTreeView()
tree.setModel(model)
tree.setWindowTitle("QTreeView控件与系统控件")
tree.resize(800, 1000)
layout1.addWidget(tree)

def ontreeclick(self, index):
i = self.tree.currentItem()
print(index.row())
print(‘key=%s,value=%s‘ % (i.text(0), i.text(1)))

#添加树节点
def add1(self):
print("添加节点")
i=self.tree.currentItem() #获取当前节点
print(i)
node=QTreeWidgetItem(i) #为当前节点增加节点
node.setText(0,"新节点")
node.setText(1,"信值")

#修改节点
def update1(self):
print("修改节点")
node= self.tree.currentItem() # 获取当前节点
node.setText(0, "修改节点")
node.setText(1, "节点值已修改")

#删除节点
def delete1(self):
print("删除节点")
node = self.tree.currentItem() # 获取当前节点
root=self.tree.invisibleRootItem() #根是不可见的,所以需要另外出来进行删除
if node in self.tree.selectedItems():
(node.parent() or root).removeChild(node)

if __name__=="__main__":
app=QApplication(sys.argv)
p=treechange()
p.show()
sys.exit(app.exec_())

技术图片


4、树控件QTreeview控件
‘‘‘
QTreeview控件与系统定制模式
MODEL
QDirModel
‘‘‘
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import sys

if __name__=="__main__":
app=QApplication(sys.argv)

#显示当前的系统根目录的列表
model=QDirModel() #当前的系统model
tree=QTreeView()
tree.setModel(model)
tree.setWindowTitle("QTreeView控件与系统控件")
tree.resize(800,1000)
tree.show()
sys.exit(app.exec_())
技术图片
5、选项卡控件QTabWidget控件
‘‘‘
QTabWidget
‘‘‘
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys

class tabwidgetDemo(QTabWidget):
def __init__(self):
super(tabwidgetDemo,self).__init__()

self.setWindowTitle("选项卡控件QTabWidget")
self.resize(800,300)

#创建用于显示控件的窗口
self.tab1=QWidget()
self.tab2=QWidget()
self.tab3=QWidget()

self.addTab(self.tab1,"选项卡1")
self.addTab(self.tab2, "选项卡2")
self.addTab(self.tab3, "选项卡3")

self.tab1UI() #初始化第一个选项卡显示页面
self.tab2UI()
self.tab3UI()

#第一个选项卡的页面设置
def tab1UI(self):
layout=QFormLayout()
layout.addRow("姓名", QLineEdit())
layout.addRow("地址",QLineEdit())
self.setTabText(0,"联系方式") #设置第一个选项卡的标题为联系方式
self.tab1.setLayout(layout)

def tab2UI(self):
layout1=QFormLayout()
sex=QHBoxLayout()
sex.addWidget(QRadioButton("男"))
sex.addWidget(QRadioButton("女"))
layout1.addRow(QLabel("性别"),sex)
layout1.addRow("生日",QLineEdit())
self.setTabText(1,"个人详细信息")
self.tab2.setLayout(layout1)

def tab3UI(self):
layout2=QHBoxLayout()
layout2.addWidget(QLabel("科目"))
layout2.addWidget(QCheckBox("物理"))
layout2.addWidget(QCheckBox("高数"))
self.setTabText(2,"教育程度")
self.tab3.setLayout(layout2)

if __name__=="__main__":
app=QApplication(sys.argv)
p=tabwidgetDemo()
p.show()
sys.exit(app.exec_())

技术图片

6、堆栈窗口控件(选项卡控件2)QStackWidget堆栈窗口
‘‘‘
QStackWidget堆栈窗口
‘‘‘
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys

class stackwidgetDemo(QTabWidget):
def __init__(self):
super(stackwidgetDemo,self).__init__()

self.setWindowTitle("堆栈控件QStackWidget")
self.resize(1000,800)

self.list=QListWidget()
self.list.insertItem(0,"联系信息")
self.list.insertItem(1,"个人信息")
self.list.insertItem(2,"教育程度")

self.list.currentRowChanged.connect(self.display)
self.stack1=QWidget()
self.stack2=QWidget()
self.stack3=QWidget()

self.stack=QStackedWidget()
self.stack.addWidget(self.stack1)
self.stack.addWidget(self.stack2)
self.stack.addWidget(self.stack3)

hb=QHBoxLayout()
hb.addWidget(self.list)
hb.addWidget(self.stack)
self.setLayout(hb)

def display(self,index):
self.stack.setCurrentIndex(index)

self.tab1UI() #初始化第一个选项卡显示页面
self.tab2UI()
self.tab3UI()

#第一个选项卡的页面设置
def tab1UI(self):
layout=QFormLayout()
layout.addRow("姓名", QLineEdit())
layout.addRow("地址",QLineEdit())
self.stack1.setLayout(layout)

def tab2UI(self):
layout1=QFormLayout()
sex=QHBoxLayout()
sex.addWidget(QRadioButton("男"))
sex.addWidget(QRadioButton("女"))
layout1.addRow(QLabel("性别"),sex)
layout1.addRow("生日",QLineEdit())
self.stack2.setLayout(layout1)

def tab3UI(self):
layout2=QHBoxLayout()
layout2.addWidget(QLabel("科目"))
layout2.addWidget(QCheckBox("物理"))
layout2.addWidget(QCheckBox("高数"))
self.stack3.setLayout(layout2)

if __name__=="__main__":
app=QApplication(sys.argv)
p=stackwidgetDemo()
p.show()
sys.exit(app.exec_())

技术图片

7、停靠控件QDockWidget堆栈窗口
‘‘‘
QDockWidget堆栈窗口
‘‘‘
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys


class dockwidgetDemo(QMainWindow):
def __init__(self):
super(dockwidgetDemo, self).__init__()

self.setWindowTitle("停靠控件QDockWidget")
self.resize(1000, 800)

layout=QHBoxLayout()
self.dock=QDockWidget("Dockable",self)
self.listwidget=QListWidget()
self.listwidget.addItem("item1")
self.listwidget.addItem("item2")
self.listwidget.addItem("item3")

self.dock.setWidget(self.listwidget)
self.setCentralWidget(QTextEdit("编辑器"))

#设置默认停靠在右边
self.addDockWidget(Qt.RightDockWidgetArea,self.dock)
self.dock.setFloating(True) #使其处于悬浮状态
self.setLayout(layout)

if __name__ == "__main__":
app = QApplication(sys.argv)
p = dockwidgetDemo()
p.show()
sys.exit(app.exec_())

技术图片

8、容纳多文档窗口控件
‘‘‘
在主窗口中可以创建多个文档窗口,窗口容器
A:QMdiArea 容纳多窗口类
B:QMdiSubWindow 多窗口类
B放入A中
‘‘‘
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys

class multiwindow(QMainWindow):
count=0
def __init__(self,parent=None):
super(multiwindow, self).__init__(parent)
self.setWindowTitle("容纳多文档窗口")

self.mdi=QMdiArea() #创建一个多文档容纳器

bar=self.menuBar() #创建一个菜单
file=bar.addMenu("File")
file.addAction("new")
file.addAction("cascade")
file.addAction("tiled")

file.triggered.connect(self.windowaction)
self.setCentralWidget(self.mdi)

def windowaction(self,q):
if q.text()=="new":
#记录子窗口的数目
multiwindow.count=multiwindow.count+1
#x新建一个多文档窗口
sub=QMdiSubWindow()
sub.setWidget(QTextEdit())
sub.setWindowTitle("子窗口"+str(multiwindow.count))
#将多文档窗口放置在多文档容器中去
self.mdi.addSubWindow(sub)
sub.show()
#重叠的排列方式
elif q.text()=="cascade":
self.mdi.cascadeSubWindows()
#拼接的排列方式
elif q.text()=="tiled":
self.mdi.tileSubWindows()

if __name__ == "__main__":
app = QApplication(sys.argv)
p = multiwindow()
p.show()
sys.exit(app.exec_())

技术图片

9、滚动条控件QScrollBar控件
‘‘‘
QScrollBar控件
1.通过滚动条的值的变化来控制其他控件状态的变化
2.通过滚动条值的变化控制控件位置的变化
‘‘‘
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import sys

class scrollbar(QWidget):
count=0
def __init__(self):
super(scrollbar, self).__init__()
self.initUI()
def initUI(self):
h=QHBoxLayout()
self.label=QLabel("拖动滚动条去改变文字颜色")
h.addWidget(self.label)

self.scrol1=QScrollBar()
self.scrol1.setMaximum(255)
self.scrol1.sliderMoved.connect(self.slidemoved)

self.scrol2 = QScrollBar()
self.scrol2.setMaximum(255)
self.scrol2.sliderMoved.connect(self.slidemoved)

self.scrol3 = QScrollBar()
self.scrol3.setMaximum(255)
self.scrol3.sliderMoved.connect(self.slidemoved)

self.scrol4 = QScrollBar()
self.scrol4.setMaximum(255)
self.scrol4.sliderMoved.connect(self.slidemoved1)

h.addWidget(self.scrol1)
h.addWidget(self.scrol2)
h.addWidget(self.scrol3)
h.addWidget(self.scrol4)

self.y=self.label.pos().y()

self.setGeometry(300,300,300,200)
self.setLayout(h)
self.setWindowTitle("滚动条控件")

#设置一些控件的状态
def slidemoved(self):
print(self.scrol1.value(),self.scrol2.value(),self.scrol3.value())
p=QPalette()
c=QColor(self.scrol1.value(),self.scrol2.value(),self.scrol3.value(),255)
p.setColor(QPalette.Foreground,c)
self.label.setPalette(p)

#设置一些控件的位置
def slidemoved1(self):
self.label.move(self.label.x(),self.y+self.scrol4.value())

if __name__ == "__main__":
app = QApplication(sys.argv)
p = scrollbar()
p.show()
sys.exit(app.exec_())

技术图片



PyQt5复杂控件(树控件、选项卡控件(滚动条控件、多文档控件、停靠控件)

标签:颜色   setw   新建   http   复杂   教育   owa   tde   ini   

原文地址:https://www.cnblogs.com/Yanjy-OnlyOne/p/12302955.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!