Python体验(10)-图形界面之计算器

 

 

  1 import wx
  2 class Form(wx.Frame):
  3     def __init__( self, parent, id, title ):
  4         wx.Frame.__init__(self,parent,id,title,wx.DefaultPosition,wx.Size(300, 250))
  5         self.formula = False
  6         menuBar = wx.MenuBar()
  7         mnuFile = wx.Menu()
  8         mnuFile.Append( 22, '&Quit', 'Exit Calculator' )
  9         menuBar.Append( mnuFile, '&File' )
 10         wx.EVT_MENU( self, 22, self.OnClose )
 11         self.SetMenuBar( menuBar )
 12 
 13         self.display = wx.TextCtrl(self, -1, '', style=wx.TE_RIGHT)
 14         gs = wx.GridSizer(5, 4, 3, 3)
 15         gs.AddMany(
 16             [
 17                 (wx.Button(self, 12, '-'), 0, wx.EXPAND),
 18                 (wx.Button(self, 20, 'Cls'), 0, wx.EXPAND),
 19                 (wx.Button(self, 21, 'Bck'), 0, wx.EXPAND),
 20                 (wx.StaticText(self, -1, ''), 0, wx.EXPAND),
 21                 (wx.Button(self, 22, 'Close'), 0, wx.EXPAND),
 22                 (wx.Button(self, 1, '7'), 0, wx.EXPAND),
 23                 (wx.Button(self, 2, '8'), 0, wx.EXPAND),
 24                 (wx.Button(self, 3, '9'), 0, wx.EXPAND),
 25                 (wx.Button(self, 4, '/'), 0, wx.EXPAND),
 26                 (wx.Button(self, 5, '4'), 0, wx.EXPAND),
 27                 (wx.Button(self, 6, '5'), 0, wx.EXPAND),
 28                 (wx.Button(self, 7, '6'), 0, wx.EXPAND),
 29                 (wx.Button(self, 8, '*'), 0, wx.EXPAND),
 30                 (wx.Button(self, 10, '2'), 0, wx.EXPAND),
 31                 (wx.Button(self, 11, '3'), 0, wx.EXPAND),
 32                 (wx.Button(self, 9, '1'), 0, wx.EXPAND),
 33                 (wx.Button(self, 16, '+'), 0, wx.EXPAND),
 34                 (wx.Button(self, 15, '='), 0, wx.EXPAND),
 35                 (wx.Button(self, 14, '.'), 0, wx.EXPAND),
 36                 (wx.Button(self, 13, '0'), 0, wx.EXPAND)
 37             ]
 38         )
 39         sizer = wx.BoxSizer( wx.VERTICAL )
 40         sizer.Add(self.display, 0, wx.EXPAND|wx.TOP|wx.BOTTOM, 4)
 41         sizer.Add(gs, 1, wx.EXPAND)
 42         self.SetSizer(sizer)
 43         self.Centre()
 44 
 45         wx.EVT_BUTTON(self, 20, self.OnClear)
 46         wx.EVT_BUTTON(self, 21, self.OnBackspace)
 47         wx.EVT_BUTTON(self, 22, self.OnClose)
 48         wx.EVT_BUTTON(self, 1, self.OnNumber)
 49         wx.EVT_BUTTON(self, 2, self.OnNumber)
 50         wx.EVT_BUTTON(self, 3, self.OnNumber)
 51         wx.EVT_BUTTON(self, 4, self.OnFormula)
 52         wx.EVT_BUTTON(self, 5, self.OnNumber)
 53         wx.EVT_BUTTON(self, 6, self.OnNumber)
 54         wx.EVT_BUTTON(self, 7, self.OnNumber)
 55         wx.EVT_BUTTON(self, 8, self.OnFormula)
 56         wx.EVT_BUTTON(self, 9, self.OnNumber)
 57         wx.EVT_BUTTON(self, 10, self.OnNumber)
 58         wx.EVT_BUTTON(self, 11, self.OnNumber)
 59         wx.EVT_BUTTON(self, 12, self.OnFormula)
 60         wx.EVT_BUTTON(self, 13, self.OnNumber)
 61         wx.EVT_BUTTON(self, 14, self.OnFormula)
 62         wx.EVT_BUTTON(self, 15, self.OnEqual)
 63         wx.EVT_BUTTON(self, 16, self.OnFormula)
 64 
 65     def OnClear(self, event):
 66         self.display.Clear()
 67     def OnBackspace(self, event):
 68         formula = self.display.GetValue()
 69         self.display.Clear()
 70         self.display.SetValue(formula[:-1])
 71     def OnClose(self, event):
 72         self.Close()
 73     def OnEqual(self,event):
 74         if self.formula:
 75             return
 76         formula = self.display.GetValue()
 77         self.formula = True
 78         try:
 79             self.display.Clear()
 80             output = eval(formula)
 81             self.display.AppendText(str(output))
 82         except StandardError:
 83             self.display.AppendText("Error")
 84 
 85     def OnFormula(self,event):
 86         if self.formula:
 87             return
 88         self.display.AppendText(event.EventObject.LabelText)
 89 
 90     def OnNumber(self,event):
 91         if self.formula:
 92             self.display.Clear()
 93             self.formula=False
 94         self.display.AppendText(event.EventObject.LabelText)
 95 
 96 class MyApp(wx.App):
 97     def OnInit(self):
 98         frame = Form(None, -1, "Phoenix Caculator")
 99         frame.Show(True)
100         self.SetTopWindow(frame)
101         return True
102 
103 app = MyApp(0)
104 app.MainLoop()
import wx
class Form(wx.Frame):
    def __init__( self, parent, id, title ):
        wx.Frame.__init__(self,parent,id,title,wx.DefaultPosition,wx.Size(300, 250))
        self.formula = False
        menuBar = wx.MenuBar()
        mnuFile = wx.Menu()
        mnuFile.Append( 22, '&Quit', 'Exit Calculator' )
        menuBar.Append( mnuFile, '&File' )
        wx.EVT_MENU( self, 22, self.OnClose )
        self.SetMenuBar( menuBar )

        self.display = wx.TextCtrl(self, -1, '', style=wx.TE_RIGHT)
        gs = wx.GridSizer(5, 4, 3, 3)
        gs.AddMany(
            [
                (wx.Button(self, 12, '-'), 0, wx.EXPAND),
                (wx.Button(self, 20, 'Cls'), 0, wx.EXPAND),
                (wx.Button(self, 21, 'Bck'), 0, wx.EXPAND),
                (wx.StaticText(self, -1, ''), 0, wx.EXPAND),
                (wx.Button(self, 22, 'Close'), 0, wx.EXPAND),
                (wx.Button(self, 1, '7'), 0, wx.EXPAND),
                (wx.Button(self, 2, '8'), 0, wx.EXPAND),
                (wx.Button(self, 3, '9'), 0, wx.EXPAND),
                (wx.Button(self, 4, '/'), 0, wx.EXPAND),
                (wx.Button(self, 5, '4'), 0, wx.EXPAND),
                (wx.Button(self, 6, '5'), 0, wx.EXPAND),
                (wx.Button(self, 7, '6'), 0, wx.EXPAND),
                (wx.Button(self, 8, '*'), 0, wx.EXPAND),
                (wx.Button(self, 10, '2'), 0, wx.EXPAND),
                (wx.Button(self, 11, '3'), 0, wx.EXPAND),
                (wx.Button(self, 9, '1'), 0, wx.EXPAND),
                (wx.Button(self, 16, '+'), 0, wx.EXPAND),
                (wx.Button(self, 15, '='), 0, wx.EXPAND),
                (wx.Button(self, 14, '.'), 0, wx.EXPAND),
                (wx.Button(self, 13, '0'), 0, wx.EXPAND)
            ]
        )
        sizer = wx.BoxSizer( wx.VERTICAL )
        sizer.Add(self.display, 0, wx.EXPAND|wx.TOP|wx.BOTTOM, 4)
        sizer.Add(gs, 1, wx.EXPAND)
        self.SetSizer(sizer)
        self.Centre()

        wx.EVT_BUTTON(self, 20, self.OnClear)
        wx.EVT_BUTTON(self, 21, self.OnBackspace)
        wx.EVT_BUTTON(self, 22, self.OnClose)
        wx.EVT_BUTTON(self, 1, self.OnNumber)
        wx.EVT_BUTTON(self, 2, self.OnNumber)
        wx.EVT_BUTTON(self, 3, self.OnNumber)
        wx.EVT_BUTTON(self, 4, self.OnFormula)
        wx.EVT_BUTTON(self, 5, self.OnNumber)
        wx.EVT_BUTTON(self, 6, self.OnNumber)
        wx.EVT_BUTTON(self, 7, self.OnNumber)
        wx.EVT_BUTTON(self, 8, self.OnFormula)
        wx.EVT_BUTTON(self, 9, self.OnNumber)
        wx.EVT_BUTTON(self, 10, self.OnNumber)
        wx.EVT_BUTTON(self, 11, self.OnNumber)
        wx.EVT_BUTTON(self, 12, self.OnFormula)
        wx.EVT_BUTTON(self, 13, self.OnNumber)
        wx.EVT_BUTTON(self, 14, self.OnFormula)
        wx.EVT_BUTTON(self, 15, self.OnEqual)
        wx.EVT_BUTTON(self, 16, self.OnFormula)

    def OnClear(self, event):
        self.display.Clear()
    def OnBackspace(self, event):
        formula = self.display.GetValue()
        self.display.Clear()
        self.display.SetValue(formula[:-1])
    def OnClose(self, event):
        self.Close()
    def OnEqual(self,event):
        if self.formula:
            return
        formula = self.display.GetValue()
        self.formula = True
        try:
            self.display.Clear()
            output = eval(formula)
            self.display.AppendText(str(output))
        except StandardError:
            self.display.AppendText("Error")

    def OnFormula(self,event):
        if self.formula:
            return
        self.display.AppendText(event.EventObject.LabelText)

    def OnNumber(self,event):
        if self.formula:
            self.display.Clear()
            self.formula=False
        self.display.AppendText(event.EventObject.LabelText)

class MyApp(wx.App):
    def OnInit(self):
        frame = Form(None, -1, "Phoenix Caculator")
        frame.Show(True)
        self.SetTopWindow(frame)
        return True

app = MyApp(0)
app.MainLoop()

 

你可能感兴趣的:(Python体验(10)-图形界面之计算器)