Greetings,
my name is Vojtěch Polášek and I am a blind student from czech Republic.
I am creating an application in Pyside with Python 3.2. I would like to
create a sudoku game accessible for blind and visually impaired people.
I have created a main window, added a widget which contains 81 buttons
in a grid layout. Now my idea is, that after pressing a button, that
button would disappear and would be replaced by some other controll
which would allow you to change the value. I chose QSpinBox for now.
This step works well, although I had to implement my own eventfilter,
because QT in Linux uses arrows to browse available elements on the
screen. Never mind.
Now this is the way in which I replace a button with a spinbox. This
function is connected to the QPushButton.clicked signal:
def editValue(self):
print ('editing')
senderpos =
self.grid.getItemPosition(self.grid.indexOf(self.sender()))
self.grid.addWidget(self.spinbox, senderpos[0], senderpos[1])
print ('added')
self.spinbox.setValue(int(self.sender().text()))
print ('value set')
self.spinbox.show()
print('showed')
self.spinbox.setFocus()
print('focused')
self.spinbox.editingFinished.connect(self.storeValue)
print('connected')
Don't mind those print statements, they are there for debugging
purposes, because I haven't found a way of debugging Pyside with PDB.
The problem is, that this signal is also called after I press a button
and this function is executed. I don't manipulate with spinbox in any
manner. Only thing that is set is its accepted range of input.
What could be the problem?
I am attaching the source code.
Thank you very much,
Vojtěch Polášek
#!/usr/bin/env python
import sys, time
from PySide import QtGui, QtCore
class MainWindow(QtGui.QMainWindow):
def __init__(self):
super (MainWindow, self).__init__()
self.setCentralWidget(SudokuBoard())
self.show()
class SudokuBoard(QtGui.QWidget):
def __init__(self):
super (SudokuBoard, self).__init__()
self.initUI()
def initUI(self):
self.grid = QtGui.QGridLayout()
self.buttons = []
for i in range(0, 82):
self.buttons.append(QtGui.QPushButton(str(2)))
self.grid.addWidget(self.buttons[i], i/10, i%10)
self.buttons[i].installEventFilter(self)
self.buttons[i].clicked.connect(self.editValue)
self.setLayout (self.grid)
self.spinbox = QtGui.QSpinBox()
self.spinbox.setRange (1, 9)
self.spinbox.setKeyboardTracking(False)
self.buttons[0].setFocus()
self.setGeometry (500, 500, 500, 600)
self.setWindowTitle ("test")
self.show()
# def keyPressEvent(self, e):
#if e.key() == QtCore.Qt.Key_Up: self.move("U"); print ('pressed')
def eventFilter(self, object, event):
if event.type() == QtCore.QEvent.KeyPress:
if event.key() == QtCore.Qt.Key_Up: self.move("U"); return True
if event.key() == QtCore.Qt.Key_Down: self.move("D"); return True
if event.key() == QtCore.Qt.Key_Left: self.move("L"); return True
if event.key() == QtCore.Qt.Key_Right: self.move("R"); return True
else: return False
# else: return QtGui.QWidget.eventFilter(self, object, event)
else: return False
def move(self, direction):
for b in self.buttons:
if b.hasFocus(): tmp = self.buttons.index(b)
if direction == "U": tmp -= 9
elif direction == "D": tmp += 9
elif direction == "L": tmp -= 1
elif direction == "R": tmp += 1
if (tmp >= 0) and (tmp <= 81):
self.buttons[tmp].setFocus()
return 0
else: return -1
def editValue(self):
print ('editing')
senderpos = self.grid.getItemPosition(self.grid.indexOf(self.sender()))
self.grid.addWidget(self.spinbox, senderpos[0], senderpos[1])
print ('added')
self.spinbox.setValue(int(self.sender().text()))
print ('value set')
self.spinbox.show()
print('showed')
self.spinbox.setFocus()
print('focused')
self.spinbox.editingFinished.connect(self.storeValue) #doing this because spinbox emits editingfinished signal when creating, duno why
print('connected')
def storeValue(self):
print ('stroing')
spinboxpos = self.grid.getItemPosition(self.grid.indexOf(self.sender()))
print (self.grid.itemAtPosition(spinboxpos[0], spinboxpos[1]))
def main():
app = QtGui.QApplication(sys.argv)
w = MainWindow()
sys.exit (app.exec_())
main()
_______________________________________________
PySide mailing list
[email protected]
http://lists.qt-project.org/mailman/listinfo/pyside