weixin_43660316 2019-09-29 11:34 采纳率: 69.2%
浏览 865
已采纳

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 Matlab安装yalmip和cplex功能安装失败
  • ¥15 加装宝马安卓中控改变开机画面
  • ¥15 STK安装问题问问大家,这种情况应该怎么办
  • ¥15 关于罗技鼠标宏lua文件的问题
  • ¥15 halcon ocr mlp 识别问题
  • ¥15 已知曲线满足正余弦函数,根据其峰值,还原出整条曲线
  • ¥20 无法创建新的堆栈防护界面
  • ¥15 sessionStorage在vue中的用法
  • ¥15 wordpress更换域名后用户图片头像不显示
  • ¥15 如何在ubunto上安装CEF (Chromium Embedded Framework),并且基于qt实现打开一个web