Label Change on Button Click

More
23 Mar 2016 21:53 #72072 by squan09
I am just getting started with GladeVCP and am trying to make a button that will change a label in axis. Here is my code so far:
#!/usr/bin/env python

import gtk
import hal_glib
import hal

class HandlerClass:

	def on_button1_clicked(self, button, data=None):
		self.label1.set_text("clicked")
		print("clicked")

	def __init__(self, halcomp,builder,useropts):
		self.builder = gtk.Builder()
		self.halcomp = halcomp
		self.builder.add_from_file('button.glade')
		self.builder.connect_signals(self)
		self.label1 = self.builder.get_object('label1')
		self.window = self.builder.get_object('window1')

def get_handlers(halcomp,builder,useropts):
	return [HandlerClass(halcomp,builder,useropts)]

When I run this I "clicked" is printed to the terminal, but the label does not change. If I add self.window.show() to the init then the window will show the button and label and the label will change there. Any idea why it would change the one label and not the other in Axis?

Please Log in or Create an account to join the conversation.

More
24 Mar 2016 00:52 #72076 by cmorley
You need a reference to the object before you can manipulate it.
try adding :
self.label1 = self.builder..get_object( "label1" )

to the __init__ function

I assume the label you wish to modify is called label1 in glade.

Chris M

Please Log in or Create an account to join the conversation.

More
24 Mar 2016 01:18 #72078 by squan09
Thanks, but I already have that on the second line from the bottom of the init function. Also, the label does change, but only when opened in a separate window outside of the the Axis GUI.

Please Log in or Create an account to join the conversation.

More
25 Mar 2016 04:14 #72123 by cmorley
sorry i didn't scroll the code...
This code is not useful...
self.window = self.builder.get_object('window1')
window1 gets replaced when the panel is re-parented into axis.
I can't see how that would affect the window based on the code you have shown.
Can you post the glade file too.

Chris M

Please Log in or Create an account to join the conversation.

More
28 Mar 2016 21:02 #72269 by squan09
So here is the glade file. I removed self.window = self.builder.get_object('window1') and had the same results.

Could this have anything to do with calling self.builder = gtk.Builder(), rather than self.builder = builder. From looking at others code that is only difference I can find. If I do change it to self.builder = builder though, the button inside of the Axis GUI does not function. Can you explain or do you know of a reference that explains the difference between gtk.Builder() and builder.

File Attachment:

File Name: button.glade
File Size:2 KB
Attachments:

Please Log in or Create an account to join the conversation.

Moderators: HansU
Time to create page: 0.074 seconds
Powered by Kunena Forum