wxPython - 使用按钮更改标签
在本文中,我们将学习如何使按钮与框架交互。在本文中,我们将更改按下按钮上的文本标签。因此,让我们从步骤开始。
Step 1: Create a static text on the frame.
Step 2: Add button to the frame.
Step 3: Create event function for the button.
Step 4: Add code to change text label in this function.
代码 :
# import wxPython
import wx
class Example(wx.Frame):
def __init__(self, *args, **kw):
super(Example, self).__init__(*args, **kw)
# function to get response after click
def onButton(event):
print("Button")
st.SetLabel("GeeksforGeeks")
# static text
st = wx.StaticText(self, label ="Welcome to ")
# create button
button = wx.Button(self, wx.ID_ANY, 'Test', (10, 40))
# bind onButton() function with button
button.Bind(wx.EVT_BUTTON, onButton)
def main():
app = wx.App()
ex = Example(None)
ex.Show()
app.MainLoop()
if __name__ == '__main__':
main()
输出:
1.点击前:
2.点击后