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