我是Python新手,这是我第一次尝试使用wxPython构建我自己的Calculator应用程序。
import wx
########################################################################
class PyCalc(wx.Frame):
"""
Python Calculator using wxPython
"""
#----------------------------------------------------------------------
def __init__(self):
"""Constructor"""
wx.Frame.__init__(self, None, wx.ID_ANY,
"Py Calculator",
size=(320,240)
)
panel = wx.Panel(self)
button1 = wx.Button(panel, label="1")
button1.Bind(wx.EVT_BUTTON, lambda evt, name=button1.GetLabel(): self.onButton(evt, name))
button2 = wx.Button(panel, label="2")
button2.Bind(wx.EVT_BUTTON, lambda evt, name=button2.GetLabel(): self.onButton(evt, name))
button3 = wx.Button(panel, label="3")
button3.Bind(wx.EVT_BUTTON, lambda evt, name=button3.GetLabel(): self.onButton(evt, name))
button4 = wx.Button(panel, label="4")
button4.Bind(wx.EVT_BUTTON, lambda evt, name=button4.GetLabel(): self.onButton(evt, name))
button5 = wx.Button(panel, label="5")
button5.Bind(wx.EVT_BUTTON, lambda evt, name=button5.GetLabel(): self.onButton(evt, name))
button6 = wx.Button(panel, label="6")
button6.Bind(wx.EVT_BUTTON, lambda evt, name=button6.GetLabel(): self.onButton(evt, name))
button7 = wx.Button(panel, label="7")
button7.Bind(wx.EVT_BUTTON, lambda evt, name=button7.GetLabel(): self.onButton(evt, name))
button8 = wx.Button(panel, label="8")
button8.Bind(wx.EVT_BUTTON, lambda evt, name=button8.GetLabel(): self.onButton(evt, name))
button9 = wx.Button(panel, label="9")
button9.Bind(wx.EVT_BUTTON, lambda evt, name=button9.GetLabel(): self.onButton(evt, name))
button10 = wx.Button(panel, label="10")
button10.Bind(wx.EVT_BUTTON, lambda evt, name=button10.GetLabel(): self.onButton(evt, name))
sizer = wx.GridSizer(rows = 3, cols=3, hgap =5,vgap =5)
sizer.Add(button1)
sizer.Add(button2)
sizer.Add(button3)
sizer.Add(button4)
sizer.Add(button5)
sizer.Add(button6)
sizer.Add(button7)
sizer.Add(button8)
sizer.Add(button9)
sizer.Add(button10)
panel.SetSizer(sizer)
#----------------------------------------------------------------------
def onButton(self, event, buttonLabel):
""""""
print "You pressed the %s button!" % buttonLabel
# Run the program
if __name__ == "__main__":
app = wx.PySimpleApp()
frame = PyCalc().Show()
app.MainLoop()
我的问题是我应该在计算器中使用哪个显示框来显示用户输入的内容? WxPython中有什么?
答案 0 :(得分:1)
wx.StaticText
似乎适合这种情况。
self.display = wx.StaticText(panel, label='0')
sizer.Add(self.display)
您可以使用SetLabel
方法或Label
属性
self.display.SetLabel('calculated_value')
# or
self.display.Label = 'calculated_value'