Install the Python GUI tool wxPython in Ubuntu

Source: Internet
Author: User
Tags glob
This article describes how to install wxPython, a Python GUI tool, In Ubuntu. wxPython can provide powerful graphical interface development support for Python. For more information, see (1) wxpython Installation

Ubuntu installation is relatively simple.

# Use: apt-cache search wxpython for testing. You can see the related information digoal @ digoal-pc :~ /Python $ apt-cache search wxpythoncain-simulations of chemical reactionscain-examples-simulations of chemical reactionscain-solvers-simulations of chemical reactionsgnumed-client-medical practice management-Client... # In this case, use sudo apt-get install python-wxtools to install digoal @ digoal-pc :~ /Python $ sudo apt-get install python-wxtools [sudo] password for digoal: Reading package lists... DoneBuilding dependency tree...

Test whether the installation is successful. In Python, import wx does not report an error.

dizzy@dizzy-pc:~/Python$ pythonPython 2.7.3 (default, Apr 20 2012, 22:44:07) [GCC 4.6.3] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import wx>>>

(2) display a window

#! /Usr/bin/python # coding: UTF-8 import wx def main (): app = wx. app () win = wx. frame (None) win. show () app. mainLoop () if _ name _ = '_ main _': main () # This is the simplest implementation of a visual window.

(3) add visual components and simple layout

# Coding: UTF-8 import wx def main (): app = wx. app () win = wx. frame (None, title = 'notepad ', size = (440,320) # obviously, title is the title and size is bt_open = wx. button (win, label = 'open', pos = (275,2), size = (80, 30) bt_save = wx. button (win, label = 'save', pos = (355, 2), size = (80, 30) # label is the label displayed by the Button, and pos is the relative position in the upper left corner of the control, size is the absolute size of the Control. text_title = wx. textCtrl (win, pos = (5, 2), size = (265, 30) text_content = wx. textCtrl (win, pos = (5, 34), size = (4 30,276), style = wx. TE_MULTILINE | wx. HSCROLL) # style, wx. TE_MULTILINE enables multiline input, wx. HSCROOL has a horizontal scroll bar win. show () app. mainLoop () if _ name _ = '_ main _': main () # I have been developing desktop software. I am very familiar with this. # Since I have learned a little about VB, VC, and Delphi before, it is easy to learn. # Add the control provided by wx to a Frame and set its own properties. # The size attribute of the text control is the absolute value. So there will be some problems ......

(4) interface layout management

Since the previous controls are directly bound to the Frame, this may cause some problems. The Panel is used for management.

# Of course, there may be some problems in writing the positions of various controls as absolute positions and sizes. This is not correct # Sometimes Dynamic Layout is required, and sometimes static layout is required. import wx def main (): # create App app = wx. app () # create Frame win = wx. frame (None, title = 'notepad ', size = (440,320) win. show () # create Panel = wx. panel (win) # create open, save button bt_open = wx. button (panel, label = 'open') bt_save = wx. button (panel, label = 'save') # create a text box, text field text_filename = wx. textCtrl (panel) text_contents = wx. textCtrl (panel, style = wx. TE_MULTILINE | wx. HSCROLL) # Add the layout manager bsizer_t Op = wx. boxSizer () bsizer_top.Add (text_filename, proportion = 1, flag = wx. EXPAND) bsizer_top.Add (bt_open, proportion = 0, flag = wx. LEFT, border = 5) bsizer_top.Add (bt_save, proportion = 0, flag = wx. LEFT, border = 5) bsizer_all = wx. boxSizer (wx. VERTICAL) # wx. VERTICAL horizontal segmentation bsizer_all.Add (bsizer_top, proportion = 0, flag = wx. EXPAND | wx. LEFT, border = 5) bsizer_all.Add (text_contents, proportion = 1, flag = wx. EXPAND | wx. ALL, border = 5) pan El. SetSizer (bsizer_all) app. MainLoop () if _ name _ = '_ main _': main () # This is a dynamic layout. Of course, this is just a view. # This is just a surface, and the soul is not here!

(5) event handling for adding controls

Directly add the code.

#! /Usr/bin/python # coding: UTF-8 import wx def openfile (evt): filepath = text_filename.GetValue () fopen = file (filepath) fcontent = fopen. read () text_contents.SetValue (fcontent) fopen. close () def savefile (evt): filepath = text_filename.GetValue () filecontents = text_contents.GetValue () fopen = file (filepath, 'w') fopen. write (filecontents) fopen. close () app = wx. app () # create Framewin = wx. frame (None, title = 'no Tepad', size = (440,320) # create Panelpanel = wx. panel (win) # create open, save button bt_open = wx. button (panel, label = 'open') bt_open.Bind (wx. EVT_BUTTON, openfile) # Add openbutton event binding. The openfile () function processes bt_save = wx. button (panel, label = 'save') bt_save.Bind (wx. EVT_BUTTON, savefile) # Add save button event binding, savefile () function processing # create a text box, text field text_filename = wx. textCtrl (panel) text_contents = wx. textCtrl (panel, style = wx. TE_MULTILINE | wx. HSCROLL) # Add the layout manager bsizer_t Op = wx. boxSizer () bsizer_top.Add (text_filename, proportion = 1, flag = wx. EXPAND, border = 5) bsizer_top.Add (bt_open, proportion = 0, flag = wx. LEFT, border = 5) bsizer_top.Add (bt_save, proportion = 0, flag = wx. LEFT, border = 5) bsizer_all = wx. boxSizer (wx. VERTICAL) bsizer_all.Add (bsizer_top, proportion = 0, flag = wx. EXPAND | wx. LEFT, border = 5) bsizer_all.Add (text_contents, proportion = 1, flag = wx. EXPAND | wx. ALL, border = 5) panel. setSizer (bs Izer_all) win. show () app. mainLoop () 47,0-1 Bot #################################### #################### open, the SAVE Function is basically implemented, but there are still many bugs. # How can I calculate my second Python applet !! ######################################## ####################################

(6) example of using the ListCtrl list control
The ListCtrl control is powerful and is one of my favorite controls.
The following is a simple usage in list_report.py:

import wximport sys, glob, randomimport dataclass DemoFrame(wx.Frame):  def __init__(self):    wx.Frame.__init__(self, None, -1,             "wx.ListCtrl in wx.LC_REPORT mode",             size=(600,400))    il = wx.ImageList(16,16, True)    for name in glob.glob("smicon??.png"):      bmp = wx.Bitmap(name, wx.BITMAP_TYPE_PNG)      il_max = il.Add(bmp)    self.list = wx.ListCtrl(self, -1, style=wx.LC_REPORT)    self.list.AssignImageList(il, wx.IMAGE_LIST_SMALL)    # Add some columns    for col, text in enumerate(data.columns):      self.list.InsertColumn(col, text)    # add the rows    for item in data.rows:      index = self.list.InsertStringItem(sys.maxint, item[0])      for col, text in enumerate(item[1:]):        self.list.SetStringItem(index, col+1, text)      # give each item a random image      img = random.randint(0, il_max)      self.list.SetItemImage(index, img, img)            # set the width of the columns in various ways    self.list.SetColumnWidth(0, 120)    self.list.SetColumnWidth(1, wx.LIST_AUTOSIZE)    self.list.SetColumnWidth(2, wx.LIST_AUTOSIZE)    self.list.SetColumnWidth(3, wx.LIST_AUTOSIZE_USEHEADER)app = wx.PySimpleApp()frame = DemoFrame()frame.Show()app.MainLoop()

How do I obtain the selected project?
The most common method is to obtain the first selected Item: GetFirstSelected (). This function returns an int, that is, the ID of the Item in ListCtrl.

Another method is GetNextSelected (itemid). After obtaining the specified itemid, the first selected item is returned.

Through these two methods, we can traverse all the selected items.

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.