Hello!
I have a table with some rows and cells (code at the end), and created a function which searches for text inside the cells and returns the row number where the text is (to simplify the example, only English Name is working). Then, it should set the grid cursor focus to that row (I want to use the wx.grid.Grid.SelectRows style when created the wx.Grid, but to reduce the number of variables I don't use it in the example). To set the cursor focus, I am using

def OnSearch(self, event):
	CommonName = self.CommonNameTXT.GetValue()
	if CommonName != '':
		ind = self.parent.EnglishNameList.index(CommonName)
		self.parent.SpeciesGrid.SetGridCursor(ind, 0)
		self.parent.SpeciesGrid.MakeCellVisible(ind,0)

Theoretically, it should work using only the SetGridCursor method, however somewhere I have read that it will not work unless you also use the MaceCellVisible method as well. Anyway, it does not work at all. Can anyone help?

Dani

#! /usr/bin/env python
# TestDialogs.py
 
import wx, MySQLdb, wx.lib.intctrl, wx.grid
ID_SPECIES=1
 
class SearchDlg(wx.Dialog):
	def __init__(self,parent):
		self.parent = parent
		wx.Dialog.__init__(self, None, -1, 'Search species', size=(400,400))
		panel = wx.Panel(self, -1)
		vbox = wx.BoxSizer(wx.VERTICAL)
		hbox1 = wx.BoxSizer(wx.HORIZONTAL)
		hbox2 = wx.BoxSizer(wx.HORIZONTAL)
		hbox3 = wx.BoxSizer(wx.HORIZONTAL)
		hbox4 = wx.BoxSizer(wx.HORIZONTAL)
		hbox5 = wx.BoxSizer(wx.HORIZONTAL)
		hbox6 = wx.BoxSizer(wx.HORIZONTAL)
 
		#Define buttons and controls
		SearchBy = wx.StaticText(panel, -1, 'Search species by:')
		CommonName = wx.StaticText(panel, -1, 'English name:')
		self.CommonNameTXT = wx.TextCtrl(panel, -1, size = (200,-1))
		ScientificName = wx.StaticText(panel, -1, 'Scientific name:')
		self.ScientificNameTXT = wx.TextCtrl(panel, -1, size = (200,-1))
		Genus = wx.StaticText(panel, -1, 'Genus:')
		self.GenusTXT = wx.TextCtrl(panel, -1, size = (200,-1))
		Species = wx.StaticText(panel, -1, 'Species:')
		self.SpeciesTXT = wx.TextCtrl(panel, -1, size = (200,-1))
		searchBtn = wx.Button(panel, -1, 'Search')
		searchBtn.Bind(wx.EVT_BUTTON, self.OnSearch, searchBtn)
 
		#Merge sizers
		hbox1.Add(SearchBy, 0, wx.ALL, 5)
		hbox2.Add(CommonName, 0, wx.ALL, 5)
		hbox2.Add(self.CommonNameTXT, 0, wx.ALL, 5)
		hbox3.Add(ScientificName, 0, wx.ALL, 5)
		hbox3.Add(self.ScientificNameTXT, 0, wx.ALL, 5)
		hbox4.Add(Genus, 0, wx.ALL, 5)
		hbox4.Add(self.GenusTXT, 0, wx.ALL, 5)
		hbox5.Add(Species, 0, wx.ALL, 5)
		hbox5.Add(self.SpeciesTXT, 0, wx.ALL, 5)
		hbox6.Add(searchBtn, 0, wx.ALL, 5)
		vbox.Add(hbox1, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		vbox.Add(hbox2, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		vbox.Add(hbox3, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		vbox.Add(hbox4, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		vbox.Add(hbox5, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		vbox.Add(hbox6, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		panel.SetSizer(vbox)
		self.Centre()
		self.Show(True)
 
	def OnSearch(self, event):
		CommonName = self.CommonNameTXT.GetValue()
		if CommonName != '':
			ind = self.parent.EnglishNameList.index(CommonName)
			self.parent.SpeciesGrid.SetGridCursor(ind, 0)
			self.parent.SpeciesGrid.MakeCellVisible(ind,0)
			print ind

 
class SpeciesGrid(wx.Frame):
	def __init__(self, parent, id, title = 'Select species'):
		self.InitialSpecies = 4
		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)
 
	#Add species table widget
		self.SpeciesGrid = wx.grid.Grid(panel, -1, size=(826,550))
		self.SpeciesGrid.CreateGrid(10396,4)
		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
		searchBtn = wx.Button(panel, -1, 'Search')
		searchBtn.Bind(wx.EVT_BUTTON, self.OnSearch, searchBtn)
		useBtn = wx.Button(panel, -1, 'Use')
		useBtn.Bind(wx.EVT_BUTTON, self.OnUse, useBtn)
		rmvBtn = wx.Button(panel, -1, 'Remove')
		doneBtn = wx.Button(panel, -1, 'Done')
		doneBtn.Bind(wx.EVT_BUTTON, self.OnDone, doneBtn)
		SpeciesNumber = wx.StaticText(panel, -1, 'Actual number of seen species: ')
		SpeciesNumberTXT = wx.TextCtrl(panel, -1, style = wx.TE_READONLY)
		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)
		BtnSizer.Add(SpeciesNumber, 0, wx.ALL, 5)
		BtnSizer.Add(SpeciesNumberTXT, 0, wx.ALL, 5)
		vbox.Add(SpeciesTableSizer, 0, wx.ALIGN_CENTER | wx.ALL, 5)
		vbox.Add(BtnSizer, 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 OnUse(self, event):
		row = self.row+1
		def GetCurrentUser():
			sql = 'select substring_index(CURRENT_USER(),"@",1)'
			self.cursor.execute(sql)
			result = self.cursor.fetchall()
			textResult = str('')
			for record in result:
				for field in record:
					CurrentUser = str(field)
					return CurrentUser
		Username = GetCurrentUser()
 
		def GetOrder():
			sql = 'select OrderScientific from Species where idSpecies = %s'
			self.cursor.execute(sql, (row))
			result = self.cursor.fetchall()
			textResult = str('')
			for record in result:
				for field in record:
					Order = str(field)
					return Order
		Order = GetOrder()
 
		def GetFamily():
			sql = 'select Family from Species where idSpecies = %s'
			self.cursor.execute(sql, (row))
			result = self.cursor.fetchall()
			textResult = str('')
			for record in result:
				for field in record:
					Family = str(field)
					return Family
		Family = GetFamily()
 
		def GetGenus():
			sql = 'select Genus from Species where idSpecies = %s'
			self.cursor.execute(sql, (row))
			result = self.cursor.fetchall()
			textResult = str('')
			for record in result:
				for field in record:
					Genus = str(field)
					return Genus
		Genus = GetGenus()
 
		def GetSpecies():
			sql = 'select Species from Species where idSpecies = %s'
			self.cursor.execute(sql, (row))
			result = self.cursor.fetchall()
			textResult = str('')
			for record in result:
				for field in record:
					Species = str(field)
					return Species
		Latin = GetSpecies()
 
		def GetEnglishName():
			sql = 'select EnglishName from Species where idSpecies = %s'
			self.cursor.execute(sql, (row))
			result = self.cursor.fetchall()
			textResult = str('')
			for record in result:
				for field in record:
					EnglishName = str(field)
					return EnglishName
		EnglishName = GetEnglishName()
		sql = 'INSERT INTO LifeList(OrderScientific, Genus, Family, Species, EnglishName, Username) VALUES (%s, %s, %s, %s, %s, %s)'
		self.cursor.execute(sql, (Order, Genus, Family, Latin, EnglishName, Username))
		self.db.commit()
		def GetIndex():
			sql = 'select idSpecies from Species where (Genus, Species) in (select Genus,Species from LifeList)'
			self.cursor.execute(sql)
			result = self.cursor.fetchall()
			result2 = []
			for record in result:
				for field in record:
					Index = str(field)
					result2.append(Index)
			return result2
		Index = GetIndex()
		for i in range(0, len(Index)):
			for j in range(0,4):
				self.SpeciesGrid.SetCellBackgroundColour(int(Index[i])-1,j,wx.LIGHT_GREY)
 
	def OnDone(self, event):
		def CountSpecies():
			sql = 'select idSpecies from Species where (Genus, Species) in (select Genus,Species from LifeList)'
			self.cursor.execute(sql)
			result = self.cursor.fetchall()
			result2 = []
			for record in result:
				for field in record:
					Index = str(field)
					result2.append(Index)
			return int(len(result2))
		TotalSpecies = CountSpecies()
		AddedSpecies = TotalSpecies - self.InitialSpecies
		Message = 'You have added ' + str(AddedSpecies) + ' species ' + 'and your life list has ' + str(TotalSpecies) + ' species.'
		DoneDlg = wx.MessageDialog(None, Message, 'Info', wx.OK)
		DoneDlg.ShowModal()
		self.Destroy()
 
	def OnSearch(self, event):
		dlg = SearchDlg(self)
		dlg.ShowModal()
 
app = wx.App()
SpeciesGrid(None, -1)
app.MainLoop()

Could anyone help, please? This is a very important feature for my app ...
Thank you so much!

Dani

Solved! The key point was to return the focus to the grid. In this example, when you searched for a species, the focus is no longer on the grid but on the Search button. Bu adding

self.SpeciesGrid.SelectRow(self.row)

on the OSelectCell function (line 129), you return the focus to the grid and it works like a charm.

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.