GtkDialog - How to change the value of a variable?

For discussions about programming, programming questions/advice, and projects that don't really have anything to do with Puppy.
Post Reply
Message
Author
User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

GtkDialog - How to change the value of a variable?

#1 Post by MochiMoppel »

I'm stuck with a simple task: to toggle a button label. I have a variable defined outside of the dialog

Code: Select all

TEXT="go"
I now want a togglebutton to change its label whenever it's pushed. Something like this:

Code: Select all

	<togglebutton>
		<label>'"$TEXT"'</label>
		<variable>BTN</variable>
		<action>if true let TEXT="stop"</action>
		<action>if false let TEXT="go"</action>
		<action>refresh:BTN</action>
	</togglebutton>
This doesn't work. Is it somehow possible to change the value of TEXT and let the button refresh accordingly?

User avatar
zigbert
Posts: 6621
Joined: Wed 29 Mar 2006, 18:13
Location: Valåmoen, Norway
Contact:

#2 Post by zigbert »

The 'refresh' command does only manage the <input> tag, so the text can only be refreshed for <entry>, <combo...>, <edit>, <text>, <tree> and <table>.

The way I get around this is to build a svg-icon for a <button> widget, and rebuild it with new label when refreshing.


Sigmund

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#3 Post by don570 »

-to ZIGBERT's post...

I made a simple example of SVG buttons (YES and NO)
in my manual

http://murga-linux.com/puppy/viewtopic.php?t=89045

___________________________________________

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#4 Post by MochiMoppel »

@ zigbert
Thanks for your prompt reply. Though this is quite a limitation, there is also something wrong with (the syntax?) of my code. The variabel doesn't change. Let's try another example with the <entry> tag. Outside of the dialog I've defined a variable TEXT="home". My code would look like this:

Code: Select all

	<togglebutton>
		<input file stock="gtk-'"$TEXT"'"></input>
		<variable>BTN</variable>
		<action>if true let TEXT="yes"</action>
		<action>if false let TEXT="no"</action>
		<action>refresh:BTN</action>
	</togglebutton>
Doesn't work either. The button shows the home icon, but doesn't change. No wonder: After I've left the dialog the variable hasn't changed to "yes" or "no". Where is my mistake? Changing the value of the variable is my primary problem, having the dialog pick up the value would be the next.

@don570
Your manual is the first thing I look for when I need a good example. Thanks for work :D Actually I studied your togglebutton example before posting, but I don't want to use icons, and if I must use them, I would like to stick to gtk stock icons. Does it really have to be svg icons?

User avatar
zigbert
Posts: 6621
Joined: Wed 29 Mar 2006, 18:13
Location: Valåmoen, Norway
Contact:

#5 Post by zigbert »

The mistake is the call for gtk stock icons. - You should refresh a file. don570 shows the solution.
When you first get used to svg, it is rather simple and powerful.


Sigmund

User avatar
don570
Posts: 5528
Joined: Wed 10 Mar 2010, 19:58
Location: Ontario

#6 Post by don570 »

To avoid using an SVG image button I tried a togglebutton instead.

http://murga-linux.com/puppy/viewtopic. ... 690#756690

Image

________________________________________________

In my manual this is example that is closest ---->
The button changes color and label text changes. The signal that is
emitted changes the entry box text.

Image
_____________________________________

User avatar
MochiMoppel
Posts: 2084
Joined: Wed 26 Jan 2011, 09:06
Location: Japan

#7 Post by MochiMoppel »

zigbert wrote:The mistake is the call for gtk stock icons.
Hmmm...I'm afraid I failed to discribe my problem properly - though I tried to put it into the thread title. This thread was intended to clarify variable values and their scope, not togglebuttons and their limitations. I used togglebuttons only as an example. If this was a bad example, I'm sorry.
But I think I figured it out myself more or less: The variables in the main script that builds the dialog are accessible during the creation process. Once gtkdialog runs the MAIN_DIALOG (or whatever it was called) variable, this is done in a subshell (?), and this subshell has no access to the variables of other shells, not even the starting shell (unless the variables had been exported). But even in case of exported variables they would be read-only (?) and therefore can't be changed. So my real mistake was to assume that I can change the variable TEXT (containing the value "home") from within the dialog. My second mistake seems to be the value assignment in the <action> tags. Given that the TEXT variables declared in the main script and the variable in the dialog have nothing in common except the name, following assignment would fail:

Code: Select all

<action>if true let TEXT="yes";xmessage $TEXT</action> 
This also fails:

Code: Select all

<action>TEXT="yes"</action>
<action>xmessage $TEXT</action>
However this works:

Code: Select all

<action>TEXT="yes";xmessage $TEXT</action>
So we have exported read-only variables that can be used anywhere in the dialog, read-only variables declared with the <variable> tags that also can be used anywhere in the dialog, and finally read/write variables declared in the <action> tags that are local to the tag where they are declared. A bit confusing, but correct so far?

don570 wrote:In my manual this is example that is closest
I didn't find this example in your manual.
don570 wrote:To avoid using an SVG image button I tried a togglebutton instead.
Thanks, I've seen this example and it's a bit funny. The irony here: The togglebutton is the only button that does NOT toggle. The image doesn't change, the label doesn't change, but this would be essential for a togglebutton since the user needs a clue what the button will trigger.
I've played with your example and came up with a version that changes button image and label. I simply use 2 buttons (is that cheating?), which for the user appear as one. This also avoids the need to juggle symlinks and all other problems associated with using 2 sets of resources for only 1 button:
Image
Attachments
Two_toggled_buttons.gz
Remove the fake gz extension
(2.96 KiB) Downloaded 236 times
Last edited by MochiMoppel on Tue 25 Feb 2014, 02:16, edited 1 time in total.

User avatar
Moose On The Loose
Posts: 965
Joined: Thu 24 Feb 2011, 14:54

#8 Post by Moose On The Loose »

MochiMoppel wrote:

Code: Select all

<action>TEXT="yes"</action>
<action>xmessage $TEXT</action>
However this works:

Code: Select all

<action>TEXT="yes";xmessage $TEXT</action>
I have found a reliable way to think about this sort of thing. In all cases where gtkdialog doesn't know the item used directly, I think of <action> as meaning "fire up a subshell and pass this string as a command to it". The subshell closes at the end of the command.

When the <action> starts with an item that gtkdialog knows then it does the work internally.

This breaks <action> into two very different things. The differences in behavior are kept from being confused this way. Variables created in the subshell only live until the end of the subshell. This explains why "TEXT" goes away between <action> lines.

Post Reply