wxpython module details

 

1. Introduction of python gui module:

Tkinter: The simplest graphical module for python, there are only 14 builds

- Pyqt     : python's most complex and widely used graphics

* Wx       : Is a graphical one in the middle of python, learning structure is clear

- Pywin   : Is a module under python windows, camera control (opencv), commonly used for making hangs

2. Installation of wx module:

1 C:\Users\Administrator> pip install wxpython

3. Graphical Introduction

......

 

 

4. Introduction of the main components of wx

1. frame (window)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Parameters:   parent = None #The parent element, if None, represents the top-level window   id = None #Identification of the component, unique if id is -1 to assign id to the system   title = None #Name of the window component   pos = None #The location of a component is the distance from the top left corner of the component to the parent component or the top left of the desktop   size = None #Dimensions of components, width and height   style = None #Styles for components   name = None #The name of the component, which is also used to identify the component, but is used to pass values

2. TextCtrl (Text Box)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Parameters:   parent = None #The parent element, if None, represents the top-level window   id = None #Identification of the component, unique if id is -1 to assign id to the system   value = None   #Content in Text Box          GetValue #Get the value of the text box          SetValue #Set the value of the text box   pos = None #The location of a component is the distance from the top left corner of the component to the parent component or the top left of the desktop   size = None #Dimensions of components, width and height   style = None #Styles for components   validator = None #Verification   name = None #The name of the component, which is also used to identify the component, but is used to pass values

3. Button (button)

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 Parameters:   parent = None #The parent element, if None, represents the top-level window   id = None #Identification of the component, unique if id is -1 to assign id to the system   lable = None #Label for button   pos = None #The location of a component is the distance from the top left corner of the component to the parent component or the top left of the desktop   size = None #Dimensions of components, width and height   style = None #Styles for components   validator = None #Verification   name = None #The name of the component, which is also used to identify the component, but is used to pass values

Parameters for other components are similar

4. Create Window Base Code

1 2 3 4 5 Basic Creation Window Code Description:<br><br>import wx #Introducing wx module <br> app = wx.App() #Instantiate a main loop <br> frame = wx.Frame(None) #Instantiate a window <br> frame.Show()#Call window display <br> app.MainLoop()#Start Main Loop

The effect is as follows:

5. Simple Example Written by Gui

Implement the following GUI interface, enter the text file address in the text box above, and click the Open button to display the text file content in the text box below.

1. Graphical Writing

1 2 3 4 5 6 7 8 import wx   app = wx.App() frame = wx.Frame(None,title = "Gui Test Editor",pos = (1000,200),size = (500,400))   path_text = wx.TextCtrl(frame,pos = (5,5),size = (350,24)) open_button = wx.Button(frame,label = "open",pos = (370,5),size = (50,24)) save_button = wx.Button(frame,label = "Preservation",pos = (430,5),size = (50,24))
content_text= wx.TextCtrl(frame,pos = (5,39),size = (475,300),style = wx.TE_MULTILINE)  # wx.TE_MULTILINE can wrap lines without displaying text documents as one line
1 frame.Show() app.MainLoop()

2. Event Binding

1 2 3 4 5 6 7 8 9 10 11 1,Define event functions     Event functions have one parameter and are called event       def openfile(event):     path = path_text.GetValue()     with open(path,"r",encoding="utf-8") as f:  #Encoding sets the utf8 encoding when the file is opened to avoid encoding errors when writing the file         content_text.SetValue(f.read())   2,Bind conditions and components for starting events   open_button.Bind(wx.EVT_BUTTON,openfile)

3. Complete Code

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #coding:utf-8 import wx   def openfile(event):     # Define open file events     path = path_text.GetValue()     with open(path,"r",encoding="utf-8") as f:  # The encoding parameter is used to convert encoding to utf8 when opening a file         content_text.SetValue(f.read())   app = wx.App() frame = wx.Frame(None,title = "Gui Test Editor",pos = (1000,200),size = (500,400))   path_text = wx.TextCtrl(frame,pos = (5,5),size = (350,24)) open_button = wx.Button(frame,label = "open",pos = (370,5),size = (50,24)) open_button.Bind(wx.EVT_BUTTON,openfile)    # Bind open file event to open_ On button button   save_button = wx.Button(frame,label = "Preservation",pos = (430,5),size = (50,24))   content_text= wx.TextCtrl(frame,pos = (5,39),size = (475,300),style = wx.TE_MULTILINE) #  wx.TE_MULTILINE enables multiple lines of text to be displayed as scrollbars without the ability to display text documents as one line   frame.Show() app.MainLoop()

  

  6. Dimensors

There is a flaw in the GUI code above, because our components are fixed in size, so when the frame is stretched, the corresponding components will not stretch, which will affect the user experience.

To solve this problem, we can use a dimensioner for layout, similar to the CSS style of HTML.

1. BoxSizer (Dimmer)

  • Dimensions act on the canvas (panel)
  • Default horizontal layout
  • Vertical layout can be adjusted
  • In relative proportion

2. Steps

  • Instantiate Dimensions (can be multiple)
  • Add components to different sizers
  • Set parameters such as relative scale, fill style and direction, border, etc.
  • Set up the main sizer

3. Rewrite the above code through a sizer

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 #coding:utf-8 import wx   def openfile(event):     # Define open file events     path = path_text.GetValue()     with open(path,"r",encoding="utf-8") as f:  # The encoding parameter is used to convert encoding to utf8 when opening a file         content_text.SetValue(f.read())   app = wx.App() frame = wx.Frame(None,title = "Gui Test Editor",pos = (1000,200),size = (500,400))   panel = wx.Panel(frame)   path_text = wx.TextCtrl(panel) open_button = wx.Button(panel,label = "open") open_button.Bind(wx.EVT_BUTTON,openfile)    # Bind open file event to open_ On button button   save_button = wx.Button(panel,label = "Preservation")   content_text= wx.TextCtrl(panel,style = wx.TE_MULTILINE) #  wx.TE_MULTILINE enables multiple lines of text to be displayed as scrollbars without the ability to display text documents as one line   box = wx.BoxSizer() # Indicates default instantiation of a horizontal dimensioner without parameters box.Add(path_text,proportion = 5,flag = wx.EXPAND|wx.ALL,border = 3) # add component     #Proportion: relative proportion     #flag: fill style and direction, wx.EXPAND as full fill, wx.ALL as fill direction     #Border: border box.Add(open_button,proportion = 2,flag = wx.EXPAND|wx.ALL,border = 3) # add component box.Add(save_button,proportion = 2,flag = wx.EXPAND|wx.ALL,border = 3) # add component   v_box = wx.BoxSizer(wx.VERTICAL) # wx.VERTICAL parameter representation instantiates a vertical dimensioner v_box.Add(box,proportion = 1,flag = wx.EXPAND|wx.ALL,border = 3) # add component v_box.Add(content_text,proportion = 5,flag = wx.EXPAND|wx.ALL,border = 3) # add component   panel.SetSizer(v_box) # Set up the main sizer   frame.Show() app.MainLoop()

With a sizer, the internal components change proportionally regardless of how the width is stretched.

        

 

 

Original Link:

https://www.cnblogs.com/morries123/p/8568666.html

 

Tags: Python

Posted on Wed, 24 Nov 2021 15:25:13 -0500 by supernova