Hello!
I have designed a dialog that shows a wx.Grid with some data (full code at the end). Then, there is a search area. The other buttons are not working now. When you fill any search control with some data shown on the grid, it prints the index where the data is to the console. I would like it to set the cursor focus to the corresponding index. The corresponding code for the search function is

def OnSearch(self, event):
		CommonName = self.CommonNameTXT.GetValue()
		if CommonName != '':
			self.ind = self.EnglishNameList.index(CommonName)
			print self.ind
			self.SpeciesGrid.SetGridCursor(self.ind, -1)
		Genus = self.GenusTXT.GetValue()
		if Genus != '':
			ind = self.GenusList.index(Genus)
			print ind
			self.SpeciesGrid.SetGridCursor(ind, -1)
		Species = self.SpeciesTXT.GetValue()
		if Species != '':
			nd = self.SpeciesList.index(Species)
			print ind
			self.SpeciesGrid.SetGridCursor(ind, -1)
		ScientificName = self.ScientificNameTXT.GetValue()
		if ScientificName != '':
			self.ScientificNameList = []
			for i in range(0,len(self.GenusList)):
				name = self.GenusList[i]+' '+self.SpeciesList[i]
				self.ScientificNameList.append(name)
			ind = self.ScientificNameList.index(ScientificName)
			print ind
			self.SpeciesGrid.SetGridCursor(ind, -1)

It returns the correct index, but the

self.SpeciesGrid.SetGridCursor(ind, -1)

does not set the cursor focus. How can I solve it?
Cheers!

Dani

#! /usr/bin/env python
# TestDialogs.py

import wx, MySQLdb, wx.lib.intctrl, wx.grid
ID_SPECIES=1

class SpeciesGrid(wx.Frame):
	def __init__(self, parent, id, title = 'Select species'):
		self.OrderList = ['Passeriformes', 'Passeriformes', 'Passeriformes', 'Passeriformes']
		self.FamilyList = ['Paridae', 'Paridae', 'Paridae', 'Paridae']
		self.GenusList = ['Parus','Poecile','Periparus','Lophophane']
		self.SpeciesList = ['major','palustris','ater','cristatus']
		self.EnglishNameList = ['Great Tit','Marsh Tit','Coal Tot','Crested Tit']
		wx.Frame.__init__(self, parent, id, title, size=(900,650))
#Define main panel
		panel = wx.Panel(self, -1)
		vbox = wx.BoxSizer(wx.VERTICAL)
	#Define sizers
		#Horizontal sizers
		SpeciesTableSizer = wx.BoxSizer(wx.HORIZONTAL)
		BtnSizer = wx.BoxSizer(wx.HORIZONTAL)
		SearchSizer1 = wx.BoxSizer(wx.HORIZONTAL)
		SearchSizer2 = wx.BoxSizer(wx.HORIZONTAL)
		SearchSizer3 = wx.BoxSizer(wx.HORIZONTAL)
		SearchSizer4 = wx.BoxSizer(wx.HORIZONTAL)
		SearchSizer5 = wx.BoxSizer(wx.HORIZONTAL)

	#Add species table widget
		self.SpeciesGrid = wx.grid.Grid(panel, -1, size=(826,350))
		self.SpeciesGrid.CreateGrid(10396,4, wx.grid.Grid.SelectRows)
		self.SpeciesGrid.Bind(wx.grid.EVT_GRID_SELECT_CELL, self.OnSelectCell)
		self.SpeciesGrid.SetColLabelValue(0, 'Order')
		self.SpeciesGrid.SetColLabelValue(1, 'Family')
		self.SpeciesGrid.SetColLabelValue(2, 'Scientific name')
		self.SpeciesGrid.SetColLabelValue(3, 'English name')
		self.SpeciesGrid.SetRowLabelSize(0)
		for i in range(0,len(self.OrderList)):
			self.SpeciesGrid.SetCellValue(i,0,self.OrderList[i])
		for i in range(0,len(self.FamilyList)):
			self.SpeciesGrid.SetCellValue(i,1,self.FamilyList[i])
		for i in range(0,len(self.GenusList)):
			self.SpeciesGrid.SetCellValue(i,2,self.GenusList[i]+' '+self.SpeciesList[i])
		for i in range(0,len(self.EnglishNameList)):
			self.SpeciesGrid.SetCellValue(i,3,self.EnglishNameList[i])
		for i in range(0, len(self.OrderList)):
			for j in range(0,4):
				self.SpeciesGrid.SetReadOnly(i,j)
		self.SpeciesGrid.SetColSize(0,172)
		self.SpeciesGrid.SetColSize(1,143)
		self.SpeciesGrid.SetColSize(2,250)
		self.SpeciesGrid.SetColSize(3,245)
		SpeciesTableSizer.Add(self.SpeciesGrid, wx.ALIGN_CENTER | wx.ALL,0 )
		
	#Add buttons & controls
		searchBtn = wx.Button(panel, -1, 'Search')
		searchBtn.Bind(wx.EVT_BUTTON, self.OnSearch, searchBtn)
		useBtn = wx.Button(panel, -1, 'Use')
		rmvBtn = wx.Button(panel, -1, 'Remove')
		doneBtn = wx.Button(panel, -1, 'Done')
		SearchBy = wx.StaticText(panel, -1, 'Search species by:')
		CommonName = wx.StaticText(panel, -1, 'English name:', size = (125,-1))
		self.CommonNameTXT = wx.TextCtrl(panel, -1, size = (200,-1))
		ScientificName = wx.StaticText(panel, -1, 'Scientific name:', size = (125,-1))
		self.ScientificNameTXT = wx.TextCtrl(panel, -1, size = (200,-1))
		Genus = wx.StaticText(panel, -1, 'Genus:', size = (125,-1))
		self.GenusTXT = wx.TextCtrl(panel, -1, size = (200,-1))
		Species = wx.StaticText(panel, -1, 'Species:', size = (125,-1))
		self.SpeciesTXT = wx.TextCtrl(panel, -1, size = (200,-1))
		BtnSizer.Add(searchBtn, 0, wx.ALL, 5)
		BtnSizer.Add(useBtn, 0, wx.ALL, 5)
		BtnSizer.Add(rmvBtn, 0, wx.ALL, 5)
		BtnSizer.Add(doneBtn, 0, wx.ALL, 5)
		SearchSizer1.Add(SearchBy, 0, wx.ALL, 5)
		SearchSizer2.Add(CommonName, 0, wx.ALL, 5)
		SearchSizer2.Add(self.CommonNameTXT, 0, wx.ALL, 5)
		SearchSizer3.Add(ScientificName, 0, wx.ALL, 5)
		SearchSizer3.Add(self.ScientificNameTXT, 0, wx.ALL, 5)
		SearchSizer4.Add(Genus, 0, wx.ALL, 5)
		SearchSizer4.Add(self.GenusTXT, 0, wx.ALL, 5)
		SearchSizer5.Add(Species, 0, wx.ALL, 5)
		SearchSizer5.Add(self.SpeciesTXT, 0, wx.ALL, 5)
		vbox.Add(SpeciesTableSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		vbox.Add(BtnSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		vbox.Add(SearchSizer1, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		vbox.Add(SearchSizer2, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		vbox.Add(SearchSizer3, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		vbox.Add(SearchSizer4, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		vbox.Add(SearchSizer5, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		panel.SetSizer(vbox)
		self.Centre()
		self.Show(True)

	def OnSelectCell(self, event):
		self.row = event.GetRow()
		event.Skip()
		return self.row

	def OnSearch(self, event):
		CommonName = self.CommonNameTXT.GetValue()
		if CommonName != '':
			self.ind = self.EnglishNameList.index(CommonName)
			print self.ind
			self.SpeciesGrid.SetGridCursor(self.ind, -1)
		Genus = self.GenusTXT.GetValue()
		if Genus != '':
			ind = self.GenusList.index(Genus)
			print ind
			self.SpeciesGrid.SetGridCursor(ind, -1)
		Species = self.SpeciesTXT.GetValue()
		if Species != '':
			nd = self.SpeciesList.index(Species)
			print ind
			self.SpeciesGrid.SetGridCursor(ind, -1)
		ScientificName = self.ScientificNameTXT.GetValue()
		if ScientificName != '':
			self.ScientificNameList = []
			for i in range(0,len(self.GenusList)):
				name = self.GenusList[i]+' '+self.SpeciesList[i]
				self.ScientificNameList.append(name)
			ind = self.ScientificNameList.index(ScientificName)
			print ind
			self.SpeciesGrid.SetGridCursor(ind, -1)

app = wx.App()
SpeciesGrid(None, -1)
app.MainLoop()

Dani AI

Generated

As already pointed out, the column parameter must be a valid column index (not -1). There are two other things happening here that explain why you don't see the cursor move: calling SetGridCursor only moves the internal cursor and fires a select event, it does not guarantee the cell is scrolled into view or that the grid will show the focus/highlight if the grid does not have keyboard focus. Use GoToCell (or MakeCellVisible + SetGridCursor) to ensure the cell is visible, then select the row and give the grid focus. Note that SetGridCursor generates wxEVT_GRID_SELECT_CELL and a handler can veto that event. (docs.wxpython.org)

A simple, practical sequence that fixes the problem (and works whether the grid uses row-selection mode or not) is to make the cell visible, select the row, and set focus. If you are changing the cursor as the dialog/frame is being shown, run these with wx.CallAfter so they execute after the UI is realized:

# r is the row index you found (use a valid column like 0)
wx.CallAfter(self.SpeciesGrid.GoToCell, r, 0)
wx.CallAfter(self.SpeciesGrid.SelectRow, r)
wx.CallAfter(self.SpeciesGrid.SetFocus)

GoToCell ensures visibility; SelectRow highlights the whole row in row-selection mode; SetFocus makes the active-cell border/selection visible to the user. (wxpython.org)

Troubleshooting tips: confirm your column index is inside 0..GetNumberCols()-1; verify the grid's selection mode if you expect whole-row selection; check that any EVT_GRID_SELECT_CELL handlers call event.Skip() (otherwise they can prevent the cursor move); and if you try to move the cursor before Show(), schedule the calls with wx.CallAfter. These small changes will make the cursor movement visible and selectable in the grid. (docs.wxpython.org)

Recommended Answers

All 2 Replies

The usage is

SetGridCursor(self, row, col)

, so why collumn -1 on your code mate?

self.SpeciesGrid.SetGridCursor(ind, -1)

Cheers and Happy coding

Oops! I didn't notice the -1. However, I created using the wx.grid.Grid.SelectRows style, so it selects the whole row. It does not matter which column I put in the col argument, it does not work.

Cheers!

Dani

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.