学习wxPython,基础的东西

发布于 2024-12-06 22:35:21 字数 1246 浏览 1 评论 0原文

我想显示一个按钮,当我单击该按钮时,会向主面板添加一个静态文本,该文本会自动添加到面板的 BoxSizer 中。我有这段代码,但效果不好。有人可以帮助我吗?我很绝望。谢谢

import wx

class MyApp(wx.App):
    def OnInit(self):
        self.frame = MainFrame(None,title='')
        self.SetTopWindow(self.frame)
        self.frame.Show()
        return True

class MainFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(MainFrame, self).__init__(*args, **kwargs)

        #Atributos
        self.panel = MainPanel(self)
        self.CreateStatusBar()

        #Layout
        self.sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.sizer.Add(self.panel,1,wx.EXPAND)
        self.SetSizer(self.sizer)

class MainPanel(wx.Panel):
    def __init__(self, parent):
        super(MainPanel, self).__init__(parent)

        #Atributos
        bmp = wx.Bitmap('./img.png',wx.BITMAP_TYPE_PNG)
        self.boton = wx.BitmapButton(self,bitmap=bmp)

        # Layout
        self.sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.sizer.Add(self.boton)
        self.SetSizer(self.sizer)

        self.Bind(wx.EVT_BUTTON,self.add,self.boton)

    def add(self,event):
        self.sizer.Add(wx.StaticText(self,label='Testing'))


if __name__ == "__main__":
    app = MyApp(False)
    app.MainLoop()

I want to display a button that when I click adds to the main panel a static text that automatic adds to the BoxSizer of the panel. I have this code but dosen't work good. Anyone can help me? I am desperate. Thanks

import wx

class MyApp(wx.App):
    def OnInit(self):
        self.frame = MainFrame(None,title='')
        self.SetTopWindow(self.frame)
        self.frame.Show()
        return True

class MainFrame(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(MainFrame, self).__init__(*args, **kwargs)

        #Atributos
        self.panel = MainPanel(self)
        self.CreateStatusBar()

        #Layout
        self.sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.sizer.Add(self.panel,1,wx.EXPAND)
        self.SetSizer(self.sizer)

class MainPanel(wx.Panel):
    def __init__(self, parent):
        super(MainPanel, self).__init__(parent)

        #Atributos
        bmp = wx.Bitmap('./img.png',wx.BITMAP_TYPE_PNG)
        self.boton = wx.BitmapButton(self,bitmap=bmp)

        # Layout
        self.sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.sizer.Add(self.boton)
        self.SetSizer(self.sizer)

        self.Bind(wx.EVT_BUTTON,self.add,self.boton)

    def add(self,event):
        self.sizer.Add(wx.StaticText(self,label='Testing'))


if __name__ == "__main__":
    app = MyApp(False)
    app.MainLoop()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

趁微风不噪 2024-12-13 22:35:21

如果您的问题是单击按钮时文本最初显示在按钮后面,您可以通过添加对面板的 Layout 方法的调用来强制调整大小调整器。

class MainPanel(wx.Panel):
    def __init__(self, parent):
        super(MainPanel, self).__init__(parent)

        #Atributos
        bmp = wx.Bitmap('./img.png',wx.BITMAP_TYPE_PNG)
        self.boton = wx.BitmapButton(self,bitmap=bmp)

        # Layout
        self.sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.sizer.Add(self.boton)
        self.SetSizer(self.sizer)

        self.Bind(wx.EVT_BUTTON,self.add,self.boton)

    def add(self,event):
        self.sizer.Add(wx.StaticText(self,label='Testing'))
        self.Layout()

If your problem is that your text initially shows up behind the button when it is clicked, you can force the sizer to update by adding a call to your Panel's Layout method.

class MainPanel(wx.Panel):
    def __init__(self, parent):
        super(MainPanel, self).__init__(parent)

        #Atributos
        bmp = wx.Bitmap('./img.png',wx.BITMAP_TYPE_PNG)
        self.boton = wx.BitmapButton(self,bitmap=bmp)

        # Layout
        self.sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.sizer.Add(self.boton)
        self.SetSizer(self.sizer)

        self.Bind(wx.EVT_BUTTON,self.add,self.boton)

    def add(self,event):
        self.sizer.Add(wx.StaticText(self,label='Testing'))
        self.Layout()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文