用PythonSudio在控件中添加、删除控件,并传递参数(以ScrollBox中添加删除按钮为例)

如下图,当点击"添加按钮"图标后,在ScrollBox中自动添加按钮,每个按钮点击后,将在列表框中加入相应参数的条目,并弹出有参数的对话框。

第一步:设计窗体

在窗体中添加ScrollBox(为便于理解超过长宽的内容)、Button、ListBox三个控件

第二步:为按钮添加事件

python 复制代码
    def Button1Click(self, Sender):
    	# 各个按钮的名称和参数
        arrDic=[
            {"Caption":"一号按钮","value":1},
            {"Caption":"二号按钮","value":2},
            {"Caption":"三号按钮","value":3},
            {"Caption":"四号按钮","value":4},
            {"Caption":"五号按钮","value":5},
            {"Caption":"六号按钮","value":6},
            {"Caption":"七号按钮","value":7},
            {"Caption":"八号按钮","value":8},
            {"Caption":"九号按钮","value":9},
            {"Caption":"十号按钮","value":10},
            {"Caption":"十一号按钮","value":11},
            {"Caption":"十二号按钮","value":12}

        ]
        # 用来设置按钮位置,每4个换一行
        i=0
        # 添加第一个按钮
        for dic in arrDic:
            BtnList=BitBtn(self) 
            BtnList.Caption=dic["Caption"]+chr(10)+"自动生成"
            BtnList.Tag=dic["value"]
            BtnList.Parent=self.ScrollBox1
            BtnList.Width=80
            BtnList.Height=60
            BtnList.Top=10+int(i/4)*60
            BtnList.Left=10+int(i%4)*80

            BtnList.OnClick=self.ShowTag
            BtnList.Show()
            i+=1
    # 点击按钮执行的事件
    def ShowTag(self,Sender):
        self.ListBox1.Items.Add(Sender.Tag)
        ShowMessage(Sender.Tag)

说明:

1、采用BitBtn与普通的Button相比,可以在按钮中添加图像和增加换行等,此处显示了添加换行功能

2、Tag是控件中用来传递参数的,只能用数值型内容,如果要用其他类型,可以以字典的形式用这个Tag来进行查询

3、此处定义了ShowTag过程,用来执行所有按钮的响应,注意,在按钮的Onclick中,需要写ShowTag,不是ShowTag()

第三步:删除按钮

如果点击按钮后,需要删除本按钮,可以修改ShowTag如下

python 复制代码
    def ShowTag(self,Sender):
        self.ListBox1.Items.Add(Sender.Tag)
        ShowMessage(Sender.Tag)
        Sender.Free()

注意要在使用完Sender的所有内容后再进行Free()释放

第四步:重新排列按钮

如果删除按钮后需要重新排列按钮,可以修改ShowTag如下:

python 复制代码
    def ShowTag(self,Sender):
        self.ListBox1.Items.Add(Sender.Tag)
        ShowMessage(Sender.Tag)
        Sender.Free()
        for i in range(self.ScrollBox1.ControlCount):
            self.ScrollBox1.Controls[i].Top=10+int(i/4)*60
            self.ScrollBox1.Controls[i].Left=10+int(i%4)*80

在释放完按钮后,通过遍历 剩下的控件,进行重新摆放,效果如下:

第五步:点击列表,还原按钮

添加列表框的事件如下:

python 复制代码
    def ListBox1Click(self, Sender):
        # 原始数据
        arrDic=[
            {"Caption":"一号按钮","value":1},
            {"Caption":"二号按钮","value":2},
            {"Caption":"三号按钮","value":3},
            {"Caption":"四号按钮","value":4},
            {"Caption":"五号按钮","value":5},
            {"Caption":"六号按钮","value":6},
            {"Caption":"七号按钮","value":7},
            {"Caption":"八号按钮","value":8},
            {"Caption":"九号按钮","value":9},
            {"Caption":"十号按钮","value":10},
            {"Caption":"十一号按钮","value":11},
            {"Caption":"十二号按钮","value":12}

        ]
        # 根据列表内容返回标题
        BtnValue=int(self.ListBox1.Items[self.ListBox1.ItemIndex])
        Caption=""
        for dic in arrDic:
            if dic["value"]==BtnValue:
                BtnCaption=dic["Caption"]
        # 建立新按钮
        BtnList=BitBtn(self)
        BtnList.Caption=BtnCaption+chr(10)+"自动生成"
        BtnList.Tag=BtnValue
        BtnList.Parent=self.ScrollBox1
        BtnList.Width=80
        BtnList.Height=60
        BtnList.OnClick=self.ShowTag
        BtnList.Show()

        # 重新排列按钮
        for i in range(self.ScrollBox1.ControlCount):
            self.ScrollBox1.Controls[i].Top=10+int(i/4)*60
            self.ScrollBox1.Controls[i].Left=10+int(i%4)*80

        # 删除选中项
        self.ListBox1.DeleteSelected()

删除1,7,10后,再点击列表框中的1,效果如下:

相关推荐
Mikhail_G12 分钟前
Python应用变量与数据类型
大数据·运维·开发语言·python·数据分析
YuTaoShao17 分钟前
Java八股文——集合「List篇」
java·开发语言·list
hello kitty w42 分钟前
Python学习(7) ----- Python起源
linux·python·学习
Bl_a_ck1 小时前
【JS进阶】ES6 实现继承的方式
开发语言·前端·javascript
站大爷IP1 小时前
Python文本序列的类型
python
愈努力俞幸运1 小时前
c++ 头文件
开发语言·c++
千千寰宇1 小时前
[Java/Python] Java 基于命令行调用 Python
python·java se-jdk/jvm
永日456701 小时前
学习日记-day24-6.8
开发语言·学习·php
BillKu1 小时前
Java后端检查空条件查询
java·开发语言
十五年专注C++开发2 小时前
CMake基础:gcc/g++编译选项详解
开发语言·c++·gcc·g++