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)视图结果
在这里插入图片描述

发布了78 篇原创文章 · 获赞 83 · 访问量 5396

猜你喜欢

转载自blog.csdn.net/qxyloveyy/article/details/104805444