Python程序設計之GUI(5)

1.關於樹形控件

樹形控件常用來顯示有嚴格層次關係的數據,可以非常清晰地表示各單元之間的從屬關係。

2.樹形控件源碼

①初始化

        self.tree=wx.TreeCtrl(parent=self.panel,pos=(5,5),size=(200,250))

②綁定函數

        #操作按鈕
        self.childButton=wx.Button(parent=self.panel,label="AddChild",pos=(250,125))
        self.deleteButton=wx.Button(parent=self.panel,label="DeleteChild",pos=(250,165))
        self.rootButton=wx.Button(parent=self.panel,label="AddRoot",pos=(250,205))
        #綁定事件
        self.Bind(wx.EVT_BUTTON,self.OnAddChild,self.childButton)
        self.Bind(wx.EVT_BUTTON,self.OnDelete,self.deleteButton)
        self.Bind(wx.EVT_BUTTON,self.OnAddRoot,self.rootButton)

③綁定函數實現

    def OnSelect(self,event):
        self.showString.SetValue(self.tree.GetItemText(self.tree.GetSelection()))

    #添加子節點
    def OnAddChild(self,event):
        self.OnSelect(event)
        itemSelect=self.tree.GetSelection()
        if not itemSelect:
            wx.MessageBox('Select a Node first!')
            return
        itemString=self.inputString.GetValue()
        self.inputString.SetValue('')
        self.tree.AppendItem(itemSelect,itemString)

    #刪除節點
    def OnDelete(self,event):
        self.OnSelect(event)
        itemSelect=self.tree.GetSelection()
        if not itemSelect:
            wx.MessageBox('Select a Node first!')
            return
        self.tree.Delete(itemSelect)

    #添加根節點
    def OnAddRoot(self,event):
        rootItem=self.tree.GetRootItem()
        if rootItem:
            wx.MessageBox('This tree is already a root.')
        else:
            itemString=self.inputString.GetValue()
            self.tree.AddRoot(itemString)

④運行結果
在這裏插入圖片描述

學習筆記

1.樹形控件可以用來清晰展示各單元之間的關係;
2.使用按鈕改變文本框內容:
①文本框

        #文本框
        wx.StaticText(self.panel,label="input a tree-name:",pos=(230,20))
        self.inputString=wx.TextCtrl(parent=self.panel,pos=(350,20))
        wx.StaticText(self.panel,label="show a tree-name:",pos=(230,50))
        self.showString=wx.TextCtrl(self.panel,pos=(350,50))

②函數綁定

    def OnSelect(self,event):
        self.showString.SetValue(self.tree.GetItemText(self.tree.GetSelection()))

3.使用按鈕改變樹形控件的背景顏色
①顏色選擇

     #設置顏色選擇框
        #複選框
        self.choicebgcolor=wx.ComboBox(self.panel,value="blue",choices=['white','red','pink','blue','green'],pos=(50,300),size=(100,30))
        #列表框
        self.choicectColor=wx.ListBox(self.panel,choices=['white','red','pink','blue','green'],pos=(300,300))

        #操作按鈕
        self.colorButton=wx.Button(self.panel,label="bg color",pos=(350,125))
        self.colorButton1=wx.Button(self.panel,label="tc color",pos=(350,205))
        #綁定操作函數
        self.Bind(wx.EVT_BUTTON,self.OnBgColor,self.colorButton)
        self.Bind(wx.EVT_BUTTON,self.OnTcColor,self.colorButton1)

②函數綁定

    def OnBgColor(self,event):
        color=self.choicebgcolor.GetValue()
        self.SetBackgroundColour(color)
        self.panel.SetBackgroundColour(color)
        print('1'+self.choicebgcolor.GetString(self.choicebgcolor.GetSelection()))
        print('2'+color)
        print('3'+str(self.panel.GetBackgroundColour()))

    def OnTcColor(self,event):
        color=self.choicectColor.GetString(sel
思考
關於修改窗體背景顏色:
①通過按鈕來改變背景顏色
    def OnBgColor(self,event):
        color=self.choicebgcolor.GetValue()
        self.SetBackgroundColour(color)
        self.panel.SetBackgroundColour(color)
        print('1'+self.choicebgcolor.GetString(self.choicebgcolor.GetSelection()))
        print('2'+color)
        print('3'+str(self.panel.GetBackgroundColour()))
②會改變背景顏色變量的信息,但是視圖背景顏色不變!!!

a)命令行輸出結果

(171, 171, 171, 255)
(0, 0, 255, 255)
1red
2red
3(255, 0, 0, 255)
1blue
2blue
3(0, 0, 255, 255)
1green
2green
3(0, 255, 0, 255)
1white
2white
3(255, 255, 255, 255)
1white
2white
3(255, 255, 255, 255)
1pink
2pink
3(255, 192, 203, 255)

b)視圖結果
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章