Python uses wxpython to develop a simple notepad method

Source: Internet
Author: User
Tags wxwidgets
This article mainly introduces python's method of using wxpython to develop a simple Notepad, involving the skills of using wxPython to implement desktop graphics applications, for more information about how to use wxpython to develop a simple Notepad, see the example in this article. Share it with you for your reference. The specific analysis is as follows:

WxPython is a GUI toolbox in the Python programming language. He enables Python programmers to easily create programs with robust and powerful graphical user interfaces. It is used by the Python language to bind the popular wxWidgets cross-platform GUI Tool Library. WxWidgets is written in C ++.

Like the Python language and the wxWidgetsGUI Tool Library, wxPython is an open source software. This means that anyone can use it for free, view and modify its source code, or contribute patches to add features.

WxPython is cross-platform. This means that the same program can run on multiple platforms without modification. Currently, the supported platforms include 32-bit Microsoft Windows OS, most Unix or Unix-like systems, and Apple MacOS X.

The following uses wxpython to compile a simple Notepad program, which can open, edit, and save local files.

#!/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() 

Shows the running effect:

This example is an example in the basic Python tutorial and some modifications are made. Although the basic notepad function is completed, the interface is a little simple, and the code does not follow the object-oriented programming principles well.

I hope 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.