wxPython:计算器的用户输入显示

时间:2014-05-05 03:26:17

标签: python wxpython

我是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中有什么?

1 个答案:

答案 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'