You are not logged in.
Pages: 1
Hey all, im trying to learn PyQt, im trying to make a calculator. So far i have the display working but I cant get a callback working when I click the buttons. I want to be able to click the button and then add its value to the display, if someone could help me setup the callback I can manage the rest (I hope).
Heres what ive got so far, http://zariel.pastey.net/111585
Thanks.
/e Doh, can a mod move this to the Programming section please?
Last edited by Zariel (2009-04-03 18:04:10)
Offline
Done
Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy
Offline
Something like http://arch.har-ikkje.net/apps/archisthebest/ ?
Evil #archlinux@libera.chat channel op and general support dude.
. files on github, Screenshots, Random pics and the rest
Offline
Not really, i need to get the button that was pressed as an argument, so ie i want to do something like this.
def p(self):
print self.str
class button:
str = "hej"
# When we click the button it executes the p function like this.
a = button()
p(a)
Last edited by Zariel (2009-04-04 11:27:32)
Offline
That is what my button does It calls the close function of the app.
Edit: take a look here: http://zetcode.com/tutorials/pyqt4/
Last edited by Mr.Elendig (2009-04-04 16:51:25)
Evil #archlinux@libera.chat channel op and general support dude.
. files on github, Screenshots, Random pics and the rest
Offline
No I don't think it does, if each button has the field .fish, and when I press the button I want it to print that field how would I do that? I can connect it to function which don't take any args but for what I want to do I need an arg passed to the function.
Offline
I think what you are looking for is partial function application (a.k.a "currying").
Here is an example code I wrote for you (and for practice). Hope it helps:
#! /usr/bin/python
# -*- coding: utf-8 -*-
import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *
class PartialFunctionApplicationButtons(QDialog):
""" I use what is called as "partial function application" to identify calling button.
Learnt it from this book: http://www.qtrac.eu/pyqtbook.html
"""
def __init__(self):
QDialog.__init__(self)
# Works with any label array, uncomment the one you like:
#labels = range(10)
labels = ['one', 'two', 'three', '*' , '+']
layout = QVBoxLayout()
self.text = QLineEdit()
layout.addWidget(self.text)
# The buttons
self.button = []
self.button_callback = []
for w in range(len(labels)):
self.button.append(QPushButton(unicode(labels[w]))) # create button widget
layout.addWidget(self.button[w]) # add button to layout
self.button_callback.append(lambda who=labels[w]: self.genericButton(who)) # create a special function for each button, that calls a generic function with the button as parameter name
self.connect(self.button[w], SIGNAL("clicked()"), self.button_callback[w]) # This is the actual connection. Specify emitter, singal and slot=function to call. Note SIGNAL is QtCore.SIGNAL, the way I imported QtCore perhaps makes it unclear.
self.setLayout(layout)
def genericButton(self, who):
"""" Generic function called by the button callback functions, with their label as argument """
self.text.setText(unicode(who))
if __name__ == "__main__":
app = QApplication(sys.argv)
window = PartialFunctionApplicationButtons()
window.show()
sys.exit(app.exec_())
Good luck!
Last edited by fede (2009-04-06 05:06:46)
Offline
Thanks, forgot about using lambda, though it seems much less functional than the lua version.
Offline
Pages: 1