添加带有按钮的小部件 - wxPython [英] Adding a widget with a button - wxPython
问题描述
我正在尝试使用 wxPython 在 Wordpress 中创建类似于类别面板的内容.
I'm trying to create something like the categories panel in Wordpress, with wxPython.
我想弄清楚的是如何在用户单击按钮时添加小部件(例如添加新类别")
What I'm trying to figure out, is how to add a widget when the user clicks a button (like "Add New Category")
这是我的代码:
import wx
class MainWindow(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(300,200))
self.panel = wx.Panel(self, -1)
button = wx.Button(self.panel,-1,"Button")
self.vbox = wx.BoxSizer(wx.VERTICAL)
self.vbox.Add(button)
add_btn = wx.Button(self.panel,-1,"Add")
add_btn.Bind(wx.EVT_BUTTON, self.add)
hbox = wx.BoxSizer(wx.HORIZONTAL)
hbox.Add(add_btn)
main_vbox = wx.BoxSizer(wx.VERTICAL)
main_vbox.Add(self.vbox)
main_vbox.Add(hbox)
self.panel.SetSizer(main_vbox)
self.Centre()
self.Show(True)
def add(self,event):
self.vbox.Add((wx.Button(self.panel,-1,"Button")))
if __name__ == "__main__":
app = wx.App()
MainWindow(None, -1, 'Add a Button')
app.MainLoop()
我的问题是,该按钮被添加到上一个按钮之上.我对此感到很困惑,因为如果我删除 add()
函数的 event
参数,然后在 __init__
中调用它方法,self.add()
,它工作正常.但这对我没有任何帮助,因为我需要在用户单击按钮时添加小部件.
My problem is, the button gets added on top of the previous button. I'm rather mystified by this, because if I delete the event
argument of the add()
function, and then call it in the __init__
method, self.add()
, it works fine. But that doesn't help me any because I need to add the widgets when the user clicks the button.
非常感谢任何帮助.
推荐答案
在添加按钮后调用 self.panel.Layout()
.当您调整带有子项的窗口大小时(尝试使用当前代码),会自动调用此函数,但不会在向其添加小部件时调用.
Call self.panel.Layout()
after adding the button. This function is called automatically when you resize a window with children (try it with your current code), but not when you add widgets to it.
这篇关于添加带有按钮的小部件 - wxPython的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!