`
dqifa
  • 浏览: 116932 次
社区版块
存档分类
最新评论

Dynamic list updating with a GridCellChoiceEditor

阅读更多

A common question is how to dynamically update the list in a GridCellChoiceEditor. The problem comes from not having direct access to the underlying ComboBox widget in the grid editor. Fortunately the grid editor throws an event that allows us to get to the underlying ComboBox.

In addition to dynamic updates to the choice list, having access to the underlying ComboBox allows us to use application data and the choice index.

The following example shows a method of doing this.

#-----------------------------------------------------------------------------
# Name:        GridCombo.py
# Purpose:     Dynamic list updating with a wx.grid.GridCellChoiceEditor
#
# Author:      Thomas M Wetherbee
#
# Created:     2009/04/27
# RCS-ID:      $Id: GridCombo.py $
# Copyright:   (c) 2009
# Licence:     Distributed under the terms of the GNU General Public License
#-----------------------------------------------------------------------------
#!/usr/bin/env python


'''
Dynamic list updating with a wx.grid.GridCellChoiceEditor.

This example shows how to dynamically update the choices in a 
GridCellChoiceEditor. This simple example creates a two column
grid where the top row in each column is a wx.grid.GridCellChoiceEditor.
The choices listed in the editor are created on the fly, and may change
with each selection. Text entered into the GridCellChoiceEditor cell 
is appended as an additional choice.

In addition to appending new choices, this example also shows how to get
the selection index and client data from the choice.

Cell editor interactions are printed for every step.

This example is deliberately simple, lacking sizers and other useful but 
confusing niceties.

Theory:
    
The GridCellChoiceEditor uses an underlying ComboBox to do the editing.
This underlying ComboBox is created when the cell editor is created. Normally
the ComboBox is completely hidden, but in this example we retrieve a reference 
to the ComboBox and use it to load choices and retrieve index and client data.

The example starts with a GridCellChoiceEditor attached to the two top cells of
the grid. When the GridCellChoiceEditor is invoked for the first time, two 
choice items are added to the choice list along with their associated user
data. The items are ('spam', 42) and ('eggs', 69), where spam is the text to
display and 42 is the associated client data. In this example 'spam' has an
index of 0 while eggs, being the second item of the list, has an index of 1.

Note that the index and user data are not required. The demonstrated method
works fine without either, but sometimes it is useful to know the index of a
selection, especially when the user is allowed to create choices. For example,
we might have the list ['spam', 'eggs', 'spam', 'spam'] where the three spam
items are different objects. In this case simply returning the item value
'spam' is ambiguous. We need to know the index, or perhaps some associated
client data.

In our example, when the user enters a new choice, the choice is appended to
the end of the choice list. A unique integer number is created for each new
choice, in succession, with the first number being 100. This number is used
for client data.

In this example we bind directly to the ComboBox events, rather than getting
the events through the frame. This is done to keep the grid from eating the
events. The difference in binding can be seen in the two binding methods:
    
    self.Bind(wx.EVT_BUTTON, self.OnButton, self.button)
    self.button.Bind(wx.EVT_BUTTON, self.OnButton)
    
The latter method binds directly to the widget, where the first method
receives the event up the chain through the parent.

Note that this example does not save the new choice list: it persists only
for the life of the program. In a real application, you will probably want
to save this list and reload it the next time the program runs.
'''

import wx
import wx.grid

##modules ={}

class Frame1(wx.Frame):
    def __init__(self, parent):
        wx.Frame.__init__(self, id=-1, name='', parent=None,
              pos=wx.Point(100, 100), size=wx.Size(480, 250),
              style=wx.DEFAULT_FRAME_STYLE, title='Spam & Eggs')
        self.SetClientSize(wx.Size(400, 250))

        self.scrolledWindow1 = wx.ScrolledWindow(id=-1,
              name='scrolledWindow1', parent=self, pos=wx.Point(0, 0),
              size=wx.Size(400, 250), style=wx.HSCROLL | wx.VSCROLL)

        self.grid1 = wx.grid.Grid(id=-1, name='grid1',
              parent=self.scrolledWindow1, pos=wx.Point(0, 0),
              size=wx.Size(400, 250), style=0)

        self.grid1.CreateGrid(4, 2)

        #Create the GridCellChoiceEditor with a blank list. Items will
        #be added later at runtime. "allowOthers" allows the user to
        #create new selection items on the fly.
        tChoiceEditor = wx.grid.GridCellChoiceEditor([], allowOthers=True)

        #Assign the cell editors for the top row (row 0). Note that on a
        #larger grid you would loop through the cells or set a default.
        self.grid1.SetCellEditor(0, 0, tChoiceEditor)
        self.grid1.SetCellEditor(0, 1, tChoiceEditor)

        #Create a starter list to seed the choices. In this list the item
        #format is (item, ClientData), where item is the string to display
        #in the drop list, and ClientData is a behind-the-scenes piece of
        #data to associate with this item. A seed list is optional.
        #If this were a real application, you would probably load this list
        #from a file.
        self.grid1.list = [('spam', 42), ('eggs', 69)]

        #Show the first item of the list in each ChoiceEditor cell. The
        #displayed text is optional. You could leave these cells blank, or
        #display 'Select...' or something of that nature.
        self.grid1.SetCellValue(0, 0, self.grid1.list[0][0])
        self.grid1.SetCellValue(0, 1, self.grid1.list[0][0])

        #The counter below will be used to automatically generate a new
        #piece of unique client data for each new item. This isn't very
        #useful, but it does let us demonstrate client data. Typically
        #you would use something meaningful for client data, such as a key
        #or id number.
        self.grid1.counter = 100

        #The following two objects store the client data and item index
        #from a choice selection. Client data and selection index are not
        #directly exposed to the grid object. We will get this information by
        #directly accessing the underlying ComboBox object created by the
        #GridCellChoiceEditor. 
        self.grid1.data = None
        self.grid1.index = None


        self.grid1.Bind(wx.grid.EVT_GRID_CELL_CHANGE,
              self.OnGrid1GridCellChange)

        self.grid1.Bind(wx.grid.EVT_GRID_EDITOR_CREATED,
              self.OnGrid1GridEditorCreated)

        self.grid1.Bind(wx.grid.EVT_GRID_EDITOR_HIDDEN,
              self.OnGrid1GridEditorHidden)


    #This method fires when a grid cell changes. We are simply showing
    #what has changed and any associated index and client data. Typically
    #this method is where you would put your real code for processing grid
    #cell changes.
    def OnGrid1GridCellChange(self, event):
        Row = event.GetRow()
        Col = event.GetCol()

        #All cells have a value, regardless of the editor.
        print 'Changed cell: (%u, %u)' % (Row, Col)
        print 'value: %s' % self.grid1.GetCellValue(Row, Col)

        #Row 0 means a GridCellChoiceEditor, so we should have associated
        #an index and client data.
        if Row == 0:
            print 'index: %u' % self.grid1.index
            print 'data: %s' % self.grid1.data

        print ''            #blank line to make it pretty.
        event.Skip()


    #This method fires when the underlying GridCellChoiceEditor ComboBox
    #is done with a selection.
    def OnGrid1ComboBox(self, event):
        #Save the index and client data for later use.
        self.grid1.index = self.comboBox.GetSelection()
        self.grid1.data = self.comboBox.GetClientData(self.grid1.index)

        print 'ComboBoxChanged: %s' % self.comboBox.GetValue()
        print 'ComboBox index: %u' % self.grid1.index
        print 'ComboBox data: %u\n' % self.grid1.data
        event.Skip()


    #This method fires when any text editing is done inside the text portion
    #of the ComboBox. This method will fire once for each new character, so
    #the print statements will show the character by character changes.
    def OnGrid1ComboBoxText(self, event):
        #The index for text changes is always -1. This is how we can tell
        #that new text has been entered, as opposed to a simple selection
        #from the drop list. Note that the index will be set for each character,
        #but it will be -1 every time, so the final result of text changes is
        #always an index of -1. The value is whatever text that has been 
        #entered. At this point there is no client data. We will have to add
        #that later, once all of the text has been entered.
        self.grid1.index = self.comboBox.GetSelection()

        print 'ComboBoxText: %s' % self.comboBox.GetValue()
        print 'ComboBox index: %u\n' % self.grid1.index
        event.Skip()


    #This method fires after editing is finished for any cell. At this point
    #we know that any added text is complete, if there is any.
    def OnGrid1GridEditorHidden(self, event):
        Row = event.GetRow()
        Col = event.GetCol()

        #If the following conditions are true, it means that new text has 
        #been entered in a GridCellChoiceEditor cell, in which case we want
        #to append the new item to our selection list.
        if Row == 0 and self.grid1.index == -1:
            #Get the new text from the grid cell
            item = self.comboBox.GetValue()

            #The new item will be appended to the list, so its new index will
            #be the same as the current length of the list (origin zero).
            self.grid1.index = self.comboBox.GetCount()

            #Generate some unique client data. Remember this counter example
            #is silly, but it makes for a reasonable demonstration. Client
            #data is optional. If you can use it, this is where you attach
            #your real client data.
            self.grid1.data = self.grid1.counter

            #Append the new item to the selection list. Remember that this list
            #is used by all cells with the same editor, so updating the list
            #here updates it for every cell using this editor.
            self.comboBox.Append(item, self.grid1.data)

            #Update the silly client data counter
            self.grid1.counter = self.grid1.counter + 1

        print 'OnGrid1EditorHidden: (%u, %u)\n' % (Row, Col)

        event.Skip()

    #This method fires when a cell editor is created. It appears that this
    #happens only on the first edit using that editor.
    def OnGrid1GridEditorCreated(self, event):
        Row = event.GetRow()
        Col = event.GetCol()

        print 'OnGrid1EditorCreated: (%u, %u)\n' % (Row, Col)

        #In this example, all cells in row 0 are GridCellChoiceEditors,
        #so we need to setup the selection list and bindings. We can't
        #do this in advance, because the ComboBox control is created with
        #the editor.
        if Row == 0:
            #Get a reference to the underlying ComboBox control.
            self.comboBox = event.GetControl()

            #Bind the ComboBox events.
            self.comboBox.Bind(wx.EVT_COMBOBOX, self.OnGrid1ComboBox)
            self.comboBox.Bind(wx.EVT_TEXT, self.OnGrid1ComboBoxText)

            #Load the initial choice list.
            for (item, data) in self.grid1.list:
                self.comboBox.Append(item, data)

        event.Skip()


if __name__ == '__main__':
    app = wx.PySimpleApp()
    frame = Frame1(None)
    frame.Show(True)
    app.MainLoop()

 

 

from:http://wiki.wxpython.org/GridCellChoiceEditor

分享到:
评论

相关推荐

    Updating multigranulation rough approximations with increasing of granular structures

    Dynamic updating of the rough approximations is a critical factor for the success of the rough set theory since data is growing at an unprecedented rate in the information-explosion era. Though many ...

    Local Dynamic Updating Method of Orebody Model

    ### 局部动态更新矿体模型方法解析 #### 摘要与背景 本文献介绍了一种基于网格重建和网格变形的局部动态更新矿体模型的方法。该方法旨在解决传统矿体模型更新过程中存在的问题,如计算成本高、耗时长等问题。...

    WPF Data Binding with LINQ to SQL

    This is the final part of a three-part series on using LINQ to SQL: Part 1: Mapping Tables to Objects Part 2: Adding/Updating/Deleting Data Part 3: WPF Data Binding with LINQ to SQL These tutorials ...

    3D Path Planning and Stereo-based Obstacle Avoidance for Rotorcraft UAVs

    obstacle detection and dynamic path updating. A 3D occupancy map is used to represent the environment, and is updated online using stereo data. The target application is autonomous helicopter-based ...

    PyCharm出现Error loading package list:Request failed with status code 403怎么解决

    今天我在做东西出现了Error loading package list:Request failed with status code 403这个问题。 解决方法 : 1.点击settings,然后点击下图中Project Interpreter 2.点击右边加号 ,点击Manage Repositories,将源...

    Beginning JavaScript with DOM Scripting and Ajax: Second Editon

    have experience but need help updating your skills, or you’re coming from another language, Beginning JavaScript with DOM Scripting and Ajax can help. Table of Contents Getting Started with ...

    曲线,柱状图,饼图,动态曲线

    Spline updating each second Click to add a point Master-detail chart Combinations Column, line and pie Dual axes, line and column Multiple axes Scatter with regression line

    Beginning JavaScript with DOM Scripting and Ajax (pdf + ePub)

    have experience but need help updating your skills, or you’re coming from another language, Beginning JavaScript with DOM Scripting and Ajax can help. Table of Contents Chapter 1. Getting Started ...

    phpmaker610官方安装版

    Dynamic Selection List with multiple selection support (SELECT, RADIO and CHECKBOX) Auto-login and Auto-Redirect Multi-page add/edit/view pages as tabs with page captions Audit Trail Email ...

    ASP.NET.MVC.5.with.Bootstrap.and.Knockout.js.1491914394

    experienced and aspiring web developers alike will learn how to build a complete shopping cart that demonstrates how these technologies interact with each other in a sleek, dynamic, and responsive ...

    PATCH - Updating System INDIE V2.3.0

    PATCH-更新系统是坚如磐石,专业,智能,清洁的解决方案,用于管理和分发游戏和应用程序的更新。 您的玩家最终可以毫无痛苦地更新您的游戏,而不必在新版本到来时再次下载整个游戏。这将为他们节省大量带宽和时间!...

    TCL TK 语言8.5编程指导

    Updating variables from a dictionary 96 Determining the size of a dictionary 96 Getting all records 97 Assigning values 97 Chapter 7: File Operations 99 Introduction 99 Opening a file 100 Configuring ...

    Professional.MFC.with.VC6

    Using Dynamic-link Libraries with C++ Name Mangling Alternative Calling Conventions Using and Writing DLLs with MFC MFC DLL Models Summary of DLL Models About DLLApp Troubleshooting DLL ...

    idea的文件一直在不停闪烁,并不停updating and index.doc

    ### IntelliJ IDEA 文件持续闪烁与Updating and Indexing问题详解 #### 一、问题概述 在使用IntelliJ IDEA进行开发过程中,部分用户可能会遇到一个较为常见的现象:IDE中的文件图标会不断闪烁,同时状态栏提示...

    Pycharm Available Package无法显示/安装包的问题Error Loading Package List解决

    主要介绍了Pycharm Available Package无法显示/安装包的问题Error Loading Package List解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习...

    PATCH-Updating System.zip

    针对“PATCH-Updating System.zip”这个压缩包文件,我们可以推断它包含了一个与Unity引擎相关的版本更新或补丁包,主要服务于PC平台。下面将详细讨论Unity引擎的版本更新、补丁管理和Unitypackage文件格式。 Unity...

    Myeclipse禁用updating indexes

    ### MyEclipse禁用Updating Indexes详解 在开发过程中,我们常常会遇到IDE(集成开发环境)中的某些功能设置对工作效率产生一定影响的情况。对于MyEclipse用户而言,“Updating Indexes”这一特性就是一个典型例子...

    LeetCode of algorithms with golang solution(updating)..zip

    本资源"LeetCode of algorithms with golang solution(updating)",是用Go语言解决LeetCode算法问题的集合,持续更新,旨在帮助Go语言开发者提升算法能力,理解并运用到实际项目中。 Go语言,又称Golang,是由...

    Research on dynamic update transaction for Java classes

    Dynamic software updating is critical for many systems that must provide continuous service. In addition, the Java language is gaining increasing popularity in developing distributed systems. Most ...

Global site tag (gtag.js) - Google Analytics