How Python uses Wxpython to develop simple notepad

Source: Internet
Author: User
Tags bind wxwidgets

This example describes how Python uses Wxpython to develop a simple notepad. Share to everyone for your reference. The specific analysis is as follows:

Wxpython is a GUI toolbox for the Python programming language. He makes it easy for Python programmers to create programs with robust, powerful graphical user interfaces. It is the Python language's binding to the popular Wxwidgets cross platform GUI Tool library. And Wxwidgets is written in C + + language.

Like the Python language and the Wxwidgetsgui tool Library, Wxpython is open source software. This means that anyone can use it for free and can view and modify its source code, or contribute patches to add functionality.

The Wxpython is cross-platform. This means that the same program can run on a variety of platforms without modification. The platforms supported today are 32 Microsoft Windows operating systems, most Unix or Unix-like systems, and Apple MacOS X.

The following use Wxpython to write a simple Notepad program, you can open local files, edit, save.


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-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57-58 #!/usr/bin/python import wx def OnOpen (event): "" "Load a file into the TextField." " Dialog = WX. FileDialog (None, ' Notepad ', style = WX. OPEN) If dialog. ShowModal () = = Wx.ID_OK:filename. SetValue (dialog. GetPath ()) file = Open (dialog. GetPath ()) contents. SetValue ( ()) File.close () dialog. Destroy () def OnSave (event): "" "Save text into the orignal file." "" If filename. GetValue () = = ': Dialog = WX. FileDialog (None, ' Notepad ', style = WX. SAVE) If dialog. ShowModal () = = Wx.ID_OK:filename. SetValue (dialog. GetPath ()) file = Open (dialog. GetPath (), ' W ') file.write (contents. GetValue ()) File.close () dialog. Destory () else:file = open (filename. GetValue (), ' W ') file.write (contents. GetValue ()) file.close () app = WX. APP () win = WX. Frame (None, title= "Simple Editor", size= (600,400)) bkg = WX. Panel (Win) # Define a ' load ' button and its-label, # Bind to a button event with a function ' load ' Loadbutton = WX. Button (bkg, label= ' Open ') loadbutton.bind (WX. Evt_button, ONOpen) # Define A ' save ' button and its-label, # Bind to a button event with a function ' save ' Savebutton = wx. Button (bkg, label= ' Save ') savebutton.bind (WX. Evt_button, OnSave) # Define a textBox for filename. filename = wx. Textctrl (bkg) # Define A textBox for file contents. Contents = WX. Textctrl (bkg, Style=wx.te_multiline | wx. HScroll) # Use Sizer to set relative position of the components. # horizontal Layout Hbox = wx. Boxsizer () Hbox. ADD (filename, proportion=1, flag=wx.) EXPAND) Hbox. ADD (Loadbutton, proportion=0, flag=wx. Left, border=5) Hbox. ADD (Savebutton, proportion=0, flag=wx. Left, border=5) # Vertical Layout vbox = wx. Boxsizer (WX. VERTICAL) VBox. ADD (Hbox, proportion=0, flag=wx. EXPAND | Wx. All, border=5) VBox. ADD (contents, Proportion=1, flag=wx. EXPAND | Wx. Left | Wx. BOTTOM | Wx. Right, border=5) bkg. Setsizer (VBox) win. Show () app. Mainloop ()

The effect is as shown in the following illustration:

This example is an example of the Python Basics tutorial and makes some changes. Although the basic Notepad function is complete, the interface is slightly simpler and the code does not follow the Object-oriented programming principles well.

I hope this article will help you with your Python programming.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.