Python uses Wxpython to develop a simple Notepad method

Source: Internet
Author: User
Tags 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 ToolPak. And Wxwidgets is written in the 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 multiple platforms without modification. Today's supported platforms are: 32-bit Microsoft Windows operating system, most Unix or Unix-like systems, Apple MacOS X.

Below use Wxpython write a simple Notepad program, can open local file, edit, save.

#!/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.read ()) 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) # 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 follows:

This example is an example of the basic Python tutorial and makes some changes. Although the basic Notepad functionality has been completed, the interface is slightly simpler and the code does not follow the object-oriented programming principles well.

Hopefully this article will help you with Python programming.

  • 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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.