wxpython 基础代码

import wx
class MyFrame(wx.Frame):
def __init__(self):
super().__init__(parent=None, title='计算器', size=(450, 250),
style = wx.CAPTION|wx.CLOSE_BOX|wx.TAB_TRAVERSAL)
panel = wx.Panel(self)
panel.SetBackgroundColour(wx.BLUE)
panel.SetBackgroundColour(wx.Colour(200,200,200))
self.Centre(wx.BOTH)
self.icon1 = wx.Icon(name="test.ico", type=wx.BITMAP_TYPE_PNG)
self.SetIcon(self.icon1)
self.text_ctrl = wx.TextCtrl(panel, pos=(30, 20),size=(80, 25))
self.text_ctrl.SetValue("加数1")
self.jia=wx.StaticText(panel,label="+", pos=(120, 20))
self.text_ctrl01 = wx.TextCtrl(panel, pos=(160, 20),size=(80, 25))
self.text_ctrl01.SetValue("加数2")
self.dengyu=wx.StaticText(panel,label="=", pos=(260, 20))
self.text_ctrl02 = wx.TextCtrl(panel, pos=(290, 20))
self.text_ctrl02.SetValue("他们的和")
self.button = wx.Button(panel, label='Click Me', pos=(120, 60))
self.button.Bind(wx.EVT_BUTTON, self.on_button_click)
self.button01 = wx.Button(panel, label='求和', pos=(120, 100))
self.button01.Bind(wx.EVT_BUTTON, self.on_button_click01)
def on_button_click(self, event):
message = self.text_ctrl.GetValue()
wx.MessageBox(message, 'Message', wx.OK | wx.ICON_INFORMATION)
def on_button_click01(self, event):
try:
a=float(self.text_ctrl.GetValue())
b=float(self.text_ctrl01.GetValue())
message = a+b
self.text_ctrl02.SetValue(str(message))
except Exception as e:
wx.MessageBox("没有正确输入数字", '信息', wx.OK | wx.ICON_INFORMATION)
print(e)
else:
self.text_ctrl02.SetValue(str(message))
print("OK")
class MyApp(wx.App):
def OnInit(self):
frame = MyFrame()
frame.Show()
return True
if __name__ == "__main__":
app = MyApp()
app.MainLoop()```