First off - Hello.
I’m rather new to python (bout 2 months into it) and discovered wxPy a couple of days ago. We had a small introduction course of Python at school
and I couldn’t get enough, so I started learning a bit more on my own. I find that the best way to learn languages is to make up small projects for yourself,
so I decided to create a little number game, which I think most of us played in high school. The layout is very basic at this point and the game is
at its early stages but I’ve already run into a bit of a problem spot: The code is supposed to retrieve a cell’s value and coordinates when it’s clicked on
and then check if the next cell that is clicked on is
a) a neighbour cell
b) has an identical value or a sum of 10
This all works surprisingly fine and wxPy is a delight to work with. However, if I click outside of the game, it throws no error or anything, but the program
stops responding (“… has stopped working”) and windows asks me to Close the Program.
import wx
import wx.grid as gridlibpair =
class MyForm(wx.Frame):
def init(self):
“”"
Constructor
“”"
wx.Frame.init(self, parent=None, title=“Getting the Row/Col”)
panel = wx.Panel(self)
myGrid = gridlib.Grid(panel)
myGrid.CreateGrid(3, 9)
self.myGrid = myGrid
self.myGrid.GetGridWindow().Bind(wx.EVT_LEFT_DOWN, self.onMouseDown)
self.myGrid.Bind(wx.grid.EVT_GRID_RANGE_SELECT, self.whenRangeSelect)
myGrid.AutoSize()
myGrid.SetCellHighlightPenWidth(0)
myGrid.EnableEditing(False)
myGrid.EnableDragGridSize(False)
myGrid.DisableDragColSize()
myGrid.DisableDragRowSize()
line = 0
cell = 0
labels = [nr for nr in range(1,20)]
labels.remove(10)
for label in labels:
for digit in str(label):
if cell < 9:
myGrid.SetCellValue(line, cell, digit)
cell += 1
else:
cell = 0
line += 1
myGrid.SetCellValue(line, cell, digit)
cell = 1myGrid.SetRowLabelSize(0) myGrid.SetColLabelSize(0) sizer = wx.BoxSizer(wx.VERTICAL) sizer.Add(myGrid, 1, wx.EXPAND) panel.SetSizer(sizer) def onMouseDown(self, event): """ Recognises which cell was clicked on and retrieves the value of clicked cell. """ global pair x, y = self.myGrid.CalcUnscrolledPosition(event.GetX(), event.GetY()) row, col = self.myGrid.XYToCell(x, y) val = [int(self.myGrid.GetCellValue(row, col)), row, col] if len(pair) == 0: pair.append(val) print(pair, len(pair)) else: pair.append(val) if (pair[0][1] == pair[1][1] and (pair[0][2] + 1 == pair[1][2] or pair[0][2] -1 == pair[1][2])) or (pair[0][2] == pair[1][2] and (pair[0][1] +1 == pair[1][1] or pair[0][1] -1 == pair[1][1])): if pair[0][0] + pair[1][0] == 10 or pair[0][0] == pair[1][0]: print(pair, "Correct!") pair = [] else: print(pair, "No dice") pair = [] else: print("Numbers aren't neighbours!") pair = [] def whenRangeSelect(self, event): #self.myGrid.ClearSelection() if not self.clearingSelection: self.myGrid.clearingSelection = True self.myGrid.ClearSelection() self.myGrid.clearingSelection = False
if name == “main”:
app = wx.App(False)
frame = MyForm().Show()
app.MainLoop()
Since I do not know which part exactly is the reason for this issue, I posted the whole code I have so far. I guess the conflict is that with the left mouse click event, it tries
to GetX and GetY of the grid and there happens to be none. Would a try/except error handling correct this for me? How would you brighter minds go about this?
Also, I’ve searched but found no conclusive answer this far - is there a way to disable RangeSelect? The part that I have in my code right now is what I found from here on wxPython-Users,
but it doesn’t do the trick for me.
Could also use some comments on the code if some things could be done more efficiently perhaps. Still a rookie.
All help appreciated!
Andrew