Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #6208 > unrolled thread

pyGTK identify a button

Started byTracubik <affdfsdfdsfsd@b.com>
First post2011-05-25 10:18 +0200
Last post2011-05-26 17:50 +0000
Articles 7 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  pyGTK identify a button Tracubik <affdfsdfdsfsd@b.com> - 2011-05-25 10:18 +0200
    Re: pyGTK identify a button Claudiu Nicolaie CISMARU <claudiu@virtuamagic.com> - 2011-05-25 11:44 +0300
      Re: pyGTK identify a button Tracubik <affdfsdfdsfsd@b.com> - 2011-05-25 10:51 +0200
        Re: pyGTK identify a button Claudiu Nicolaie CISMARU <claudiu@virtuamagic.com> - 2011-05-25 12:19 +0300
    Re: pyGTK identify a button Chris Angelico <rosuav@gmail.com> - 2011-05-25 18:58 +1000
    Re: pyGTK identify a button Cousin Stanley <cousinstanley@gmail.com> - 2011-05-25 16:22 +0000
    Re: pyGTK identify a button Alister Ware <alister.ware@ntlworld.com> - 2011-05-26 17:50 +0000

#6208 — pyGTK identify a button

FromTracubik <affdfsdfdsfsd@b.com>
Date2011-05-25 10:18 +0200
SubjectpyGTK identify a button
Message-ID<4ddcbb68$0$18241$4fafbaef@reader2.news.tin.it>
Hi all,
i'm trying to write a simple windows with two button in GTK, i need a 
way to identify wich button is pressed.
Consider that:

the two button are connected (when clicked) to infoButton(self, widget, 
data=None)

infoButton() is something like this

infoButton(self, widget, data=None):
	# discover wich button was pressed
	...
	# say hello to the button
	if button1pressed:
		print "Hi, button1!"
	else:
		print "Hi, button2!"

so, how can I know wich button was pressed without using data and 
without reading the label of the button (i could have buttons with the 
same label)

If data is needed, can someone pls tell me how to set it properly in 
glade 3.8

thanks
Nico

[toc] | [next] | [standalone]


#6210

FromClaudiu Nicolaie CISMARU <claudiu@virtuamagic.com>
Date2011-05-25 11:44 +0300
Message-ID<mailman.2059.1306313058.9059.python-list@python.org>
In reply to#6208

[Multipart message — attachments visible in raw view] — view raw

> the two button are connected (when clicked) to infoButton(self, 
> widget, 
> data=None)

From documentation:
handler_id = object.connect(name, func, func_data)

So:

button1.connect(when is pressed, your_function, 1)
button2.connect(when is pressed, your_function, 2)
(This code is conception, I don't really know how is done in GTK).

On the callback:
infoButton(self, widget, data=None):
        # discover wich button was pressed
        ...
        # say hello to the button
        if data == 1:
                print "Hi, button1!"
        else:
                print "Hi, button2!"

-- 
  Claudiu Nicolaie CISMARU
  GNU GPG Key: http://claudiu.targujiu.net/key.gpg
  T: +40 755 135455
  E: claudiu@virtuamagic.com, claudiu.cismaru@gmail.com

[toc] | [prev] | [next] | [standalone]


#6212

FromTracubik <affdfsdfdsfsd@b.com>
Date2011-05-25 10:51 +0200
Message-ID<4ddcc326$0$18248$4fafbaef@reader2.news.tin.it>
In reply to#6210
On 25/05/2011 10:44, Claudiu Nicolaie CISMARU wrote:
>> the two button are connected (when clicked) to infoButton(self,
>> widget,
>> data=None)
>
>  From documentation:
> handler_id = object.connect(name, func, func_data)
>
> So:
>
> button1.connect(when is pressed, your_function, 1)
> button2.connect(when is pressed, your_function, 2)
> (This code is conception, I don't really know how is done in GTK).

thanks but, as i've sayed before, i can't use func_data 'cause i don't 
know how to set it on glade3.8, that is the program i use to create the 
GUI.
Anyway, i think this is the only way to identify the button :-/

[toc] | [prev] | [next] | [standalone]


#6215

FromClaudiu Nicolaie CISMARU <claudiu@virtuamagic.com>
Date2011-05-25 12:19 +0300
Message-ID<mailman.2063.1306315145.9059.python-list@python.org>
In reply to#6212

[Multipart message — attachments visible in raw view] — view raw

> thanks but, as i've sayed before, i can't use func_data 'cause i don't 
> know how to set it on glade3.8, that is the program i use to create 
> the 
> GUI.
> Anyway, i think this is the only way to identify the button :-/

Hack into the generated source!

-- 
  Claudiu Nicolaie CISMARU
  GNU GPG Key: http://claudiu.targujiu.net/key.gpg
  T: +40 755 135455
  E: claudiu@virtuamagic.com, claudiu.cismaru@gmail.com

[toc] | [prev] | [next] | [standalone]


#6214

FromChris Angelico <rosuav@gmail.com>
Date2011-05-25 18:58 +1000
Message-ID<mailman.2062.1306313899.9059.python-list@python.org>
In reply to#6208
On Wed, May 25, 2011 at 6:18 PM, Tracubik <affdfsdfdsfsd@b.com> wrote:
> Hi all,
> i'm trying to write a simple windows with two button in GTK, i need a way to
> identify wich button is pressed.
> Consider that:
>
> the two button are connected (when clicked) to infoButton(self, widget,
> data=None)

I'm not terribly familiar with GTK, but I believe the 'widget'
parameter is the button that was clicked on. Whatever means you have
for distinguishing them (saving another reference to each object in a
named variable, etc), you should be able to do with that parameter.

Chris Angelico

[toc] | [prev] | [next] | [standalone]


#6251

FromCousin Stanley <cousinstanley@gmail.com>
Date2011-05-25 16:22 +0000
Message-ID<irjacb$d8b$1@dont-email.me>
In reply to#6208
Tracubik wrote:

> Hi all,
> i'm trying to write a simple windows with two button in GTK, 
> i need a  way to identify wich button is pressed.
> ....

#!/usr/bin/env python

import gtk

def console_display( button , args ) :

    a0 , a1 , a2 = args

    print '    %s .... %s %s ' % ( a0 , a1 , a2 )


window = gtk.Window()

window.set_title( "gtk.buttons.01" )
  
window.set_size_request( 300 , -1 )

window.set_position( gtk.WIN_POS_CENTER )

window.connect( "destroy" , gtk.main_quit )


# Create VBox and add in Window

vbox = gtk.VBox()

window.add( vbox )


# Create buttons

dict_buttons = {
    12   :  [ 'OneTwo' , 'Buckle ' , 'My Shoe' ] ,
    34   :  [ 'TreFor' , 'Shut   ' , 'The Door' ] ,
    56   :  [ 'FivSix' , 'Pick   ' , 'Up Sticks' ] ,
    78   :  [ 'SvnAte' , 'Lay    ' , 'Them Straight' ] ,
    910  :  [ 'NinTen' , 'Big    ' , 'Fat Hen' ] }

list_keys = dict_buttons.keys()

list_keys.sort()

for this_button in list_keys :
    
    this_name = dict_buttons[ this_button ][ 0 ]
    
    b = gtk.Button( this_name )

    b.set_name( this_name )

    b.connect( "clicked" , console_display , dict_buttons[ this_button ] )

    vbox.pack_start( b )


window.show_all()

gtk.main()


-- 
Stanley C. Kitching
Human Being
Phoenix, Arizona

[toc] | [prev] | [next] | [standalone]


#6331

FromAlister Ware <alister.ware@ntlworld.com>
Date2011-05-26 17:50 +0000
Message-ID<RpwDp.24712$%o4.23556@newsfe16.ams2>
In reply to#6208
On Wed, 25 May 2011 10:18:48 +0200, Tracubik wrote:

> Hi all,
> i'm trying to write a simple windows with two button in GTK, i need a
> way to identify wich button is pressed. Consider that:
> 
> the two button are connected (when clicked) to infoButton(self, widget,
> data=None)
> 
> infoButton() is something like this
> 
> infoButton(self, widget, data=None):
> 	# discover wich button was pressed
> 	...
> 	# say hello to the button
> 	if button1pressed:
> 		print "Hi, button1!"
> 	else:
> 		print "Hi, button2!"
> 
> so, how can I know wich button was pressed without using data and
> without reading the label of the button (i could have buttons with the
> same label)
> 
> If data is needed, can someone pls tell me how to set it properly in
> glade 3.8
> 
> thanks
> Nico

This looks similar to a question I posted about 2 weeks ago

In gtk 2.1.6 (i think) & earlier you could use widget.get_name() to 
return the id of the widget.
this no-longer works since 2.1.7
the pygtk forum suggests this is because widget id's are not necessarily 
unique & caused problems elsewhere, to be honest most of the explanation 
went above my head.
I did find a work around & that was to use gtk.buildable.get_name(widget)
but is is probably cleaner to use a discrete callback for each button.


-- 
Disco is to music what Etch-A-Sketch is to art.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web