Python interface builder wxFormBuilder simple to use introductory tutorial

Python desktop UI design and processing, using wxPython + wxFromBuilder is a relatively simple solution.

1. Download  

  Use pip install wxpython to download the latest wxPython.

2. Download wxfrombuilder

 wxFormBuilder download link  , this one is directly used for drag and drop.

3. Open wxformbuilder,

1. Click the Frame in the form first, you must use this at the beginning, other controls are in it. Choose a layout in Layout. The layout is used to divide the Frame into separate small squares. Various layouts are different. Some have only rows and no columns, and some have rows and columns. Add as needed.  

2. Select basic buttons and other controls in common. It is difficult to control the position here, because you can only add it when you have selected which square. The position after addition does not change well. Therefore, you must first design a formal layout, and then add small controls to it.

If the dissatisfaction is all on the left, you can choose whether to center or align the left in the above .

3. Key event

At this time, you can see the key event function in the code

4. Generate python code or other code such as C++

Give the project a suitable name and choose Python as the code generation language. In the object properties window, as shown below

Save the project, and then press F8 to generate a GUI for Python code development. Let the generated file be named Demo.py

5. Open Demo.py in the integrated development environment and write the code in Demo.py, especially the realization of the key event function

Then create a new main.py file, import Demo.py, declare the Application object and start the main event loop. Following is the executable code −

The content of the main file is as follows;


import wx 
  
#import the newly created GUI file 
import Demo  
class CalcFrame(demo.MyFrame1): 
   def __init__(self,parent): 
      demo.MyFrame1.__init__(self,parent)  
	#按键事件触发函数
   def btn_submit(self,event): 
      num = int(self.m_textCtrl1.GetValue()) 
      self.m_textCtrl2.SetValue (str(num*num)) 

def main():        
    app = wx.App(False) 
    frame = CalcFrame(None) 
    frame.Show(True) 
    #start the applications 
    app.MainLoop() 

if __name__ == '__main__':
    main()

operation result:

 

Guess you like

Origin blog.csdn.net/Thanours/article/details/83789047