Python GUI programming-wxpython simple Notepad program

Source: Internet
Author: User
Tags wxwidgets


Wxpython is PythonProgramming LanguageA Gui toolbox. He makes PythonProgramUsers can easily create programs with robust and powerful graphic 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 and can view and modify it.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()





The program running test is as follows:









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 andCodeAnd does not follow the object-oriented programming principles well.


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.