wxpython+wxFormBuilder加法运算示例
在wxFormBuilder里设计一个窗体,生成如下代码:calc_From.py 放在lib文件夹下面
import gettext
import wx
import wx.xrc
_ = gettext.gettext
class MyFrame1(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, id=wx.ID_ANY, title=_(u"Login_demo"), pos=wx.DefaultPosition,
size=wx.Size(271, 258), style=wx.CAPTION | wx.CLOSE_BOX | wx.TAB_TRAVERSAL)
self.SetSizeHints(wx.DefaultSize, wx.DefaultSize)
self.SetBackgroundColour(wx.Colour(239, 235, 235))
bSizer1 = wx.BoxSizer(wx.VERTICAL)
gSizer1 = wx.GridSizer(0, 2, 0, 0)
self.m_staticText1 = wx.StaticText(self, wx.ID_ANY, _(u"MyLabel"), wx.DefaultPosition, wx.DefaultSize, 0)
self.m_staticText1.Wrap(-1)
gSizer1.Add(self.m_staticText1, 0, wx.ALL | wx.ALIGN_CENTER_HORIZONTAL, 5)
self.m_textCtrl2 = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0)
gSizer1.Add(self.m_textCtrl2, 0, wx.ALL, 5)
self.m_staticText2 = wx.StaticText(self, wx.ID_ANY, _(u"MyLabel"), wx.DefaultPosition, wx.DefaultSize, 0)
self.m_staticText2.Wrap(-1)
gSizer1.Add(self.m_staticText2, 0, wx.ALL | wx.ALIGN_CENTER_HORIZONTAL, 5)
self.m_textCtrl1 = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0)
gSizer1.Add(self.m_textCtrl1, 0, wx.ALL | wx.ALIGN_CENTER_HORIZONTAL, 5)
self.m_staticText3 = wx.StaticText(self, wx.ID_ANY, _(u"MyLabel"), wx.DefaultPosition, wx.DefaultSize, 0)
self.m_staticText3.Wrap(-1)
gSizer1.Add(self.m_staticText3, 0, wx.ALL | wx.ALIGN_CENTER_HORIZONTAL, 5)
self.m_textCtrl3 = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0)
gSizer1.Add(self.m_textCtrl3, 0, wx.ALL | wx.ALIGN_CENTER_HORIZONTAL, 5)
bSizer1.Add(gSizer1, 1, wx.EXPAND, 5)
self.m_button1 = wx.Button(self, wx.ID_ANY, _(u"MyButton"), wx.DefaultPosition, wx.DefaultSize, 0)
bSizer1.Add(self.m_button1, 0, wx.ALL | wx.ALIGN_CENTER_HORIZONTAL, 5)
self.SetSizer(bSizer1)
self.Layout()
self.Centre(wx.BOTH)
def __del__(self):
pass
调用:
import wx
import lib.calc_From as Logindemo
class myUi(Logindemo.MyFrame1):
def __init__(self, parent):
# Logindemo.MyFrame1.__init__(self, parent)
Logindemo.MyFrame1.__init__(self, parent)
self.SetTitle("计算器")
icon = wx.Icon("test.ico", wx.BITMAP_TYPE_PNG)
self.SetIcon(icon) # 窗体图标
self.m_button1.Bind(wx.EVT_BUTTON, self.on_mybBut01)
self.m_textCtrl1.SetValue("请输入一个数")
self.m_textCtrl2.SetValue("请输入一个数")
self.m_button1.LabelText = "求和"
self.m_staticText1.LabelText = "加数:"
self.m_staticText2.LabelText = "加数:"
self.m_staticText3.LabelText = "和:"
def on_mybBut01(self, event):
a=self.m_textCtrl1.GetValue()
b=self.m_textCtrl2.GetValue()
sum=int(a)+int(b)
self.m_textCtrl3.SetValue(str(int(self.m_textCtrl1.GetValue()) + int(self.m_textCtrl2.GetValue())))
print(sum)
wx.MessageBox("运算结果是:"+str(sum), "结果", wx.OK | wx.ICON_INFORMATION)
if __name__ == '__main__':
app = wx.App(False)
frame = myUi(None)
frame.Show()
app.MainLoop()