GUI programming 1 wxPython

Knowledge content:

1. Introduction to wxPython

2. wxPython components

3.wxPython layout

4.wxPython event handling

5. wxPython combat

 

Reference: https://www.yiibai.com/wxpython

 

 

 

1. Introduction to wxPython

1. What is wxPython

官方解释:wxPython, the cross-platform GUI toolkit for the Python language. With wxPython software developers can create truly native user interfaces for their Python applications, that run with little or no modifications on Windows, Macs and Linux or other unix-like systems

The translation is: wxPython is a cross-platform GUI tool in python, which can be used to quickly build a user interface for python programs, and can be run on various operating systems

 

 

2.wxPython installation and basic use

(1) Installation

Install using pip or directly in settings in pycharm

 

(2) Basic use

The three main steps to create a GUI program with wxPython are as follows:

  • import wxPython package
  • Create a framework class
  • Build the main program

The following is an example of the above three steps:

 1 # __author__ = "wyb"
 2 # date: 2018/4/29
 3 
 4 import wx           # 1.导入wxPython
 5 
 6 
 7 # 2.建立框架类
 8 class MyFrame(wx.Frame):
 9     def __init__(self, superior):
10         wx.Frame.__init__(self, parent=superior, title=u'my first from', size=(500, 500))
11         self.Bind(wx.EVT_SIZE, self.OnSize)
12         self.Bind(wx.EVT_MOVE, self.OnFrameMove)
13 
14         panel = wx.Panel(self, -1)
15         label1 = wx.StaticText(panel, -1, "FrameSize:")
16         label2 = wx.StaticText(panel, -1, "FramePos:")
17         label3 = wx.StaticText(parent=panel, label="MouseSize:")
18         self.sizeFrame = wx.TextCtrl(panel, -1, "", style=wx.TE_READONLY)
19         self.posFrame = wx.TextCtrl(panel, -1, "", style=wx.TE_READONLY)
20         self.posMouse = wx.TextCtrl(panel, -1,"", style=wx.TE_READONLY)
21         panel.Bind(wx.EVT_MOTION, self.OnMouseMove)
22         self.panel = panel
23 
24         sizer = wx.FlexGridSizer(3, 2, 5, 5)
25         sizer.Add(label1)
26         sizer.Add(self.sizeFrame)
27         sizer.Add(label2)
28         sizer.Add(self.posFrame)
29         sizer.Add(label3)
30         sizer.Add(self.posMouse)
31 
32         border = wx.BoxSizer()
33         border.Add(sizer, 0, wx.ALL, 15)
34         panel.SetSizerAndFit(border)
35         self.Fit()
36 
37     def OnSize(self, event):
38         size = event.GetSize()
39         self.sizeFrame.SetValue("%s, %s" % (size.width, size.height))
40         event.Skip()
41 
42     def OnFrameMove(self, event):
43         pos = event.GetPosition()
44         self.posFrame.SetValue("%s, %s" % (pos.x, pos.y))
45 
46     defOnMouseMove(self, event):
 47          pos = event.GetPosition()
 48          self.posMouse.SetValue( " %s, %s " % (pos.x, pos.y))
 49  
50  
51  # 3. Create the main program 
52  if  __name__ == ' __main__ ' :
 53      app = wx.App()               #Create application object 
54      frame = MyFrame(None) #Create        frame class object 
55      frame.Show(True)             #Show frame 56      app.MainLoop(               ) #
start event loop

 

 

 

2. wxPython components

1. Frame component

 

2. Buttons, static text boxes, text boxes

Button、StaticText、TextCtrl

 

3. Menu

Menu

 

4. Toolbar and status bar

ToolBar、StatusBar

 

5. Dialog

 

 

 

Three, wxPython layout

1. Introduction to wxPython layout

 

2. BoxSizer layout

 

3.GridSizer layout

 

4. Layout actual combat

 

 

 

Fourth, wxPython event processing

 

 

Five, wxPython combat

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325613233&siteId=291194637