weixin_43660316 2019-09-29 11:34 采纳率: 75%
浏览 864
已采纳

wxpython中如何实现点击按钮产生新的文本框控件?

用wxFormbuilder画了一个界面
现在想实现,点击add按钮,产生新的文本框控件
下面的代码产生的文本框控件会和之前的文本框重叠,请问要如何修改代码,才能让新增的文本框控件按照顺序排列。

import wx

class MyFrame1(wx.Frame):

    def __init__(self, parent):
        global bSizer2 ,fgSizer2

        wx.Frame.__init__(self, parent, id=wx.ID_ANY, title=wx.EmptyString, pos=wx.DefaultPosition,
                          size=wx.Size(561, 430), style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL)

        self.SetSizeHintsSz(wx.DefaultSize, wx.DefaultSize)

        bSizer2 = wx.BoxSizer(wx.VERTICAL)

        fgSizer2 = wx.FlexGridSizer(0, 2, 0, 0)
        fgSizer2.SetFlexibleDirection(wx.BOTH)
        fgSizer2.SetNonFlexibleGrowMode(wx.FLEX_GROWMODE_SPECIFIED)

        self.m_textCtrl12 = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0)
        fgSizer2.Add(self.m_textCtrl12, 0, wx.ALL, 5)

        self.m_button17 = wx.Button(self, wx.ID_ANY, u"MyButton", wx.DefaultPosition, wx.DefaultSize, 0)
        fgSizer2.Add(self.m_button17, 0, wx.ALL, 5)

        self.m_textCtrl13 = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0)
        fgSizer2.Add(self.m_textCtrl13, 0, wx.ALL, 5)

        self.m_button18 = wx.Button(self, wx.ID_ANY, u"MyButton", wx.DefaultPosition, wx.DefaultSize, 0)
        fgSizer2.Add(self.m_button18, 0, wx.ALL, 5)

        self.m_textCtrl14 = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0)
        fgSizer2.Add(self.m_textCtrl14, 0, wx.ALL, 5)

        self.m_button19 = wx.Button(self, wx.ID_ANY, u"MyButton", wx.DefaultPosition, wx.DefaultSize, 0)
        fgSizer2.Add(self.m_button19, 0, wx.ALL, 5)

        self.m_textCtrl15 = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0)
        fgSizer2.Add(self.m_textCtrl15, 0, wx.ALL, 5)

        self.m_button20 = wx.Button(self, wx.ID_ANY, u"MyButton", wx.DefaultPosition, wx.DefaultSize, 0)
        fgSizer2.Add(self.m_button20, 0, wx.ALL, 5)

        bSizer2.Add(fgSizer2, 1, wx.EXPAND, 5)

        self.m_button6 = wx.Button(self, wx.ID_ANY, u"add", wx.DefaultPosition, wx.DefaultSize, 0)
        bSizer2.Add(self.m_button6, 0, wx.ALL, 5)

        self.SetSizer(bSizer2)
        self.Layout()

        self.Centre(wx.BOTH)

        # Connect Events
        self.m_button6.Bind(wx.EVT_BUTTON, self.add_file)

    def __del__(self):
        pass

    # Virtual event handlers, overide them in your derived class
    def add_file(self, event):
        event.Skip()



class MyProject_snMyFrame(MyFrame1):
    def __init__(self, parent):
        MyFrame1.__init__(self, parent)

    def add_file(self, event):
        # TODO: Implement add_file
        # fgSizer2 = wx.FlexGridSizer(0, 2, 0, 0)
        # fgSizer2.SetFlexibleDirection(wx.BOTH)
        # fgSizer2.SetNonFlexibleGrowMode(wx.FLEX_GROWMODE_SPECIFIED)

        self.m_textCtrl4 = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0)
        fgSizer2.Add(self.m_textCtrl4, 0, wx.ALL, 5)
        self.m_button30 = wx.Button(self, wx.ID_ANY, u"MyButton", wx.DefaultPosition, wx.DefaultSize, 0)
        fgSizer2.Add(self.m_button30, 0, wx.ALL, 5)

    pass


if __name__ == '__main__':
    app = wx.App()
    frame = MyProject_snMyFrame(None)
    frame.Show()
    app.MainLoop()
  • 写回答

1条回答 默认 最新

  • threenewbee 2019-09-30 14:10
    关注

    wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0)
    DefaultPosition换成你要的坐标位置

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已采纳回答 11月14日

悬赏问题

  • ¥15 爬虫保存的scv文件0kb
  • ¥20 如何实现基于强化学习的带电作业机械臂的运动规划与控制
  • ¥15 使用wpf制作打砖块游戏时遇到的一个Bug
  • ¥15 qrCodeDetector.detectAndDecode
  • ¥15 海洋可控源和大地电磁一维联合反演
  • ¥15 MFC多文档程序获取视图指针问题
  • ¥20 解体如何编写程序还有运行结果截图
  • ¥15 如何把matlabR2023遗传算法工具箱里面的各类选项对应的代码调出来呢?
  • ¥30 MDIGetActive() 获取活动view
  • ¥15 有数据,如何安装使用CLIP模型检索图片