First, create a simple layout with a reasonable graphical interface, but press the button and nothing happens.
1 #Encoding=utf-82 __author__='Heng'3 #Write a GUI4 ImportWX5App = WX. APP ()#Create a graphical interface first6Win = WX. Frame (None,title ="Simple Editor", size = (500,500))#title is used to set the caption of the graphical interface, and size is used to set the initial size of the window7bkg = wx. Panel (Win)#using the Panel management interface layout8Savebutton = wx. Button (Bkg,label ='Save')#Set the button labeled Save on the canvas bkg9Openbutton = wx. Button (Bkg,label ='Open')Tenfilename = wx. Textctrl (BKG)#This function is used to create a text input box on the canvas OneContents = WX. Textctrl (Bkg,style = Wx.te_multiline | wx. HScroll)#creates a wrapped text input box on the canvas and adds a scroll bar A #Start layout text box, left and right layout -Hbox = wx. Boxsizer ()#start adding a dimension, default to Horizontal -Hbox. ADD (filename,proportion = 1,flag = wx. EXPAND)#proportion is used to set the proportions of the object, 1 represents all the space that occupies the remaining extra space, and flag is used to set the icon's properties, WX. Expand representative icon expandable theHbox. ADD (savebutton,proportion = 0,flag = wx. Left,border = 5)#represents the distance from the left edge to 5 pixels (the width of the edge, the interval) -Hbox. ADD (openbutton,proportion = 0,flag = wx. Left,border = 5) - #start up and down layout -Bbox = wx. Boxsizer (WX. VERTICAL)#up and down layouts, vertical layouts +Bbox. ADD (hbox,proportion = 0,flag = wx. EXPAND | Wx. All,border = 5) -Bbox. ADD (contents,proportion = 1,flag = wx. expand| Wx. Left|wx. Right|wx. Bottom,border = 5) + A bkg. Setsizer (bbox) at win. Show () -App. Mainloop ()
A graphical interface to handle key events is set up below
1 #Encoding=utf-82 __author__='Heng'3 #Write a GUI4 ImportWX5 #adding events for keystrokes6 defLoad (event):7FILE = open (filename. GetValue ())#open a file in a small text box8Contents. SetValue (File.read ())#read the text information9 file.close ()Ten defSave (event): OneFILE = open (filename. GetValue (),'W') A File.write (contents. GetValue ()) - file.close () - theApp = WX. APP ()#Create a graphical interface first -Win = WX. Frame (None,title ="Simple Editor", size = (500,500))#title is used to set the caption of the graphical interface, and size is used to set the initial size of the window -bkg = wx. Panel (Win)#using the Panel management interface layout -Savebutton = wx. Button (Bkg,label ='Save')#Set the button labeled Save on the canvas bkg +Savebutton.bind (WX. Evt_button,save)#bind the event handling of save to the key -Openbutton = wx. Button (Bkg,label ='Open') + Openbutton.bind (WX. Evt_button,load) Afilename = wx. Textctrl (BKG)#This function is used to create a text input box on the canvas atContents = WX. Textctrl (Bkg,style = Wx.te_multiline | wx. HScroll)#creates a wrapped text input box on the canvas and adds a scroll bar - #Start layout text box, left and right layout -Hbox = wx. Boxsizer ()#start adding a dimension, default to Horizontal -Hbox. ADD (filename,proportion = 1,flag = wx. EXPAND)#proportion is used to set the proportions of the object, 1 represents all the space that occupies the remaining extra space, and flag is used to set the icon's properties, WX. Expand representative icon expandable -Hbox. ADD (savebutton,proportion = 0,flag = wx. Left,border = 5)#represents the distance from the left edge to 5 pixels (the width of the edge, the interval) -Hbox. ADD (openbutton,proportion = 0,flag = wx. Left,border = 5) in #start up and down layout -Bbox = wx. Boxsizer (WX. VERTICAL)#up and down layouts, vertical layouts toBbox. ADD (hbox,proportion = 0,flag = wx. EXPAND | Wx. All,border = 5) +Bbox. ADD (contents,proportion = 1,flag = wx. expand| Wx. Left|wx. Right|wx. Bottom,border = 5) - the bkg. Setsizer (bbox) * win. Show () $App. Mainloop ()
The above code adds a function to handle the event, and you can bind the function to the corresponding key. This event uses a read-write operation for the file.
Writing a GUI with Wxpython