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.




#!/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 an 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 an 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

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.