You are not logged in.

#1 2009-04-03 17:42:43

Zariel
Member
Registered: 2008-10-07
Posts: 446

Basic PyQt help

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

#2 2009-04-04 10:14:14

.:B:.
Forum Fellow
Registered: 2006-11-26
Posts: 5,819
Website

Re: Basic PyQt help

Done wink


Got Leenucks? :: Arch: Power in simplicity :: Get Counted! Registered Linux User #392717 :: Blog thingy

Offline

#3 2009-04-04 10:59:20

Mr.Elendig
#archlinux@freenode channel op
From: The intertubes
Registered: 2004-11-07
Posts: 4,092

Re: Basic PyQt help


Evil #archlinux@libera.chat channel op and general support dude.
. files on github, Screenshots, Random pics and the rest

Offline

#4 2009-04-04 11:27:21

Zariel
Member
Registered: 2008-10-07
Posts: 446

Re: Basic PyQt help

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

#5 2009-04-04 16:45:16

Mr.Elendig
#archlinux@freenode channel op
From: The intertubes
Registered: 2004-11-07
Posts: 4,092

Re: Basic PyQt help

That is what my button does tongue 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

#6 2009-04-04 18:09:24

Zariel
Member
Registered: 2008-10-07
Posts: 446

Re: Basic PyQt help

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

#7 2009-04-06 04:56:59

fede
Member
Registered: 2007-06-29
Posts: 126

Re: Basic PyQt help

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

#8 2009-04-06 14:16:55

Zariel
Member
Registered: 2008-10-07
Posts: 446

Re: Basic PyQt help

Thanks, forgot about using lambda, though it seems much less functional than the lua version.

Offline

Board footer

Powered by FluxBB