wxPython - 使用 Destroy()函数销毁按钮小部件
在本文中,我们将学习如何使用 wxPython 的 wx.Button 类中的 Destroy()函数从窗口中销毁按钮小部件。 Destroy()函数用于简单地安全地销毁窗口或小部件。
如果窗口已成功删除,或者已添加到待实际删除的窗口列表中,则 Destroy() 返回 True。
Syntax: wx.StaticText.Destroy(self)
Parameters: Destroy() function takes no arguments.
Return Type: bool
Returns: True if the window has either been successfully deleted, or it has been added to the list of windows pending real deletion.
代码示例:
import wx
class Example(wx.Frame):
def __init__(self, *args, **kwargs):
super(Example, self).__init__(*args, **kwargs)
self.InitUI()
def InitUI(self):
self.locale = wx.Locale(wx.LANGUAGE_ENGLISH)
# create parent panel
self.pnl = wx.Panel(self)
# create a button at point (20, 50)
self.btn1 = wx.Button(self.pnl, id = 1, label ="Remove Text", pos =(20, 50))
# create button to destroy
self.btn0 = wx.Button(self.pnl, id = 1, label ="Click button to remove", pos =(20, 20))
# bind Onclick() function with button
self.btn1.Bind(wx.EVT_BUTTON, self.Onclick)
self.SetSize((350, 250))
self.SetTitle('wx.Button')
self.Centre()
def Onclick(self, e):
# destroy btn0 button
self.btn0.Destroy()
def main():
app = wx.App()
ex = Example(None)
ex.Show()
app.MainLoop()
if __name__ == '__main__':
main()
输出窗口:
在销毁()之前
在销毁()之后