There are a lot of GUI Libraries in python. I chose mature wxpython. This basically satisfies our needs and is easy to operate. Next we will learn wxpython through some simple examples.
1. Create a simplest GUI window:
3 import wx 2 app=wx.App() 1 win=wx.Frame(None)7 win.Show() 1 app.MainLoop()
2. Add necessary text descriptions.
1 import wx 2 app=wx.App() 3 win=wx.Frame(None,title="example") 4 win.Show() 5 app.MainLoop()
3. Add a button.
1 import wx 2 app=wx.App() 3 win=wx.Frame(None,title="example") 4 btn=wx.Button(win,label='open') 5 win.Show() 6 app.MainLoop()
4. page layout.
1 import wx 2 app=wx.App() 3 win=wx.Frame(None,title="editor",size=(400,300)) 4 texinput=wx.TextCtrl(win,pos=(5,5),size=(210,25)) 5 openbtn=wx.Button(win,label='open',pos=(225,5),size=(80,25)) 6 savebtn=wx.Button(win,label='save',pos=(315,5),size=(80,25)) 7 contents=wx.TextCtrl(win,pos=(5,35),size=(380,250)) 8 win.Show() 9 app.MainLoop()
Result:
5. Event Control <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + expires/expires + tPrC66O6PC9wPgo8cD48L3A + CjxwcmUgY2xhc3M9 "brush: java;"> import wx 2 class Frame (wx. frame): 3 def _ init _ (self, superion): 4 wx. frame. _ init _ (self, parent = superion, title = "input and output computer", size = (400,200) 5 panel = wx. panel (self) 6 wx. staticText (panel, label = 'input n: ', pos = (10, 10) 7 self. inputN = wx. textCtrl (panel, pos = (150,10) 8 wx. staticText (panel, label = 'the sum till n: ', pos = (10, 50) 9 self. outsum = wx. textCtrl (panel, pos = (150,50) 10 self. btnsum = wx. button (panel, label = 'compute', pos = (150,100), size = (50, 30) 11 self. bind (wx. EVT_BUTTON, self. f, self. btnsum) 12 13 def f (self, event): 14 n = self. inputN. getValue () 15 n = int (n) 16 I = 1 17 s = 0 18 for I in range (1, n + 1): 19 s = s + I 20 self. outsum. setValue (str (s) 21 22 if _ name __= = '_ main _': 23 app = wx. app () 24 frame = Frame (None) 25 frame. show () 26 app. mainLoop ()
Result: