[gtk dialog] How to change value of external 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
fabrice_035
Posts: 765
Joined: Mon 28 Apr 2014, 17:54
Location: Bretagne / France

[gtk dialog] How to change value of external variable?

#1 Post by fabrice_035 »

Hi,

It is possible to change the value of external variable ?

In this example, i try change AD=1 to AD=-1

And make the negative button work.

Thanks.

Code: Select all

#!/bin/bash

GTKDIALOG=gtkdialog

export AD
AD=1

MAIN_DIALOG='
<window title="compute" decorated="true" icon_name="gtk-media-record" >
	<vbox>
		<hbox>

		<button can-focus="no" relief="2">
		<input file stock="gtk-add"></input>
		<action function="refresh">'RESULT'</action>
		</button>
		
		
		<button can-focus="no" relief="2">
		<input file stock="gtk-remove"></input>
		
		</button>

				<entry>
				<variable>RESULT</variable>
				<input>echo $(( (RESULT + AD) ))</input>
			    </entry>
			</hbox>
	</vbox>
</window>
'
export MAIN_DIALOG

gtkdialog  --program=MAIN_DIALOG 

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

Re: [gtk dialog] addition subtraction

#2 Post by MochiMoppel »

fabrice_035 wrote:Hi,

It is possible to change the value of external variable ?
Well, yes...sort of, but I don't see how these would help you, at least in your chosen approach.

The following example tries to stay as close as possible to your code and uses 2 text widgets to keep your internal variables:

Code: Select all

#!/bin/bash
GTKDIALOG=gtkdialog 

MAIN_DIALOG=' 
<window title="compute" decorated="true" icon_name="gtk-media-record" > 
<vbox> 
	<text visible="false"><variable>ADD</variable><input>echo 1</input></text>
	<text visible="false"><variable>SUB</variable><input>echo 1</input></text>
	<hbox>
		<button can-focus="no" relief="2"> 
			<input file stock="gtk-add"></input> 
			<action>refresh:ADD</action> 
			<action>clear:SUB</action> 
			<action>refresh:RESULT</action> 
		</button> 
		<button can-focus="no" relief="2"> 
			<input file stock="gtk-remove"></input> 
			<action>refresh:SUB</action> 
			<action>clear:ADD</action> 
			<action>refresh:RESULT</action> 
		</button> 
		<entry> 
			<variable>RESULT</variable> 
			<input>echo $(( RESULT + ADD - SUB ))</input> 
		</entry> 
	</hbox> 
</vbox> 
 </window> 
 ' 
export MAIN_DIALOG 

gtkdialog  --program=MAIN_DIALOG

User avatar
fabrice_035
Posts: 765
Joined: Mon 28 Apr 2014, 17:54
Location: Bretagne / France

#3 Post by fabrice_035 »

Hi MochiMoppel, nice way :P you help me
So I imagine change external var is not possible in gtk ?
Thanks.

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

#4 Post by MochiMoppel »

fabrice_035 wrote:So I imagine change external var is not possible in gtk ?
I guess it's not possible anywhere in Linux. You can't change a parent shell variable from a subshell. You may use and change the variable in a subshell, e.g. in a gtkdialog <action> statement, but you can't make such changed value usable for other actions or widgets. So for all practical purposes let's say: It's not possible.

I'm glad you like the solution. You can make it even simpler. Only 1 variable:

Code: Select all

#!/bin/bash
MAIN_DIALOG=' 
<window title="compute" decorated="true" icon_name="gtk-media-record" > 
<vbox> 
	<text visible="false"><variable>SUB</variable><input>echo 2</input></text>
	<hbox>
		<button can-focus="no" relief="2"> 
			<input file stock="gtk-add"></input> 
			<action>clear:SUB</action> 
			<action>refresh:RESULT</action> 
		</button> 
		<button can-focus="no" relief="2"> 
			<input file stock="gtk-remove"></input> 
			<action>refresh:SUB</action> 
			<action>refresh:RESULT</action> 
		</button> 
		<entry> 
			<variable>RESULT</variable>
			<default>100</default> 
			<input>echo $(( RESULT + 1 - SUB ))</input> 
		</entry> 
	</hbox> 
</vbox> 
 </window> 
 ' 
export MAIN_DIALOG 

gtkdialog  --program=MAIN_DIALOG

Post Reply