Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I'm working on a very simple app for Ubuntu. I've asked a question on stackoverflow, and it seems that the issue I am having is caused by signals, not by the scope of variables, as I originally thought. The problem I am having is that when TextBox emits a signal through activate the whole code works without a glitch. But when I change the signal to insert-at-click it returns NameErrors in every non-TextBox-linked function. Now, It is highly possible I am doing something completely wrong here, but is it at least probable that signals could affect global variable assignments?

The code in its current form giving me NameErrors:

def on_servername_insertatcursor(self, widget):
global output  
output = StringIO.StringIO()         
servername = widget.get_text()
output.write("USHARE_NAME="+servername+'\n')

def on_netif_changed(self, widget):
netif = widget.get_active_text()
global output
output.write("USHARE_IFACE="+netif+'\n')

def on_port_insertatcursor(self, widget):
global output
port = widget.get_text()
output.write("USHARE_PORT="+port+'\n')

def on_telprt_insertatcursor(self, widget):
global output
telprt = widget.get_text()
output.write("USHARE_TELNET_PORT="+telprt+'\n')

def on_dirs_insertatcursor(self, widget):
global output
dirs = widget.get_text()
output.write("USHARE_DIR="+dirs+'\n')

def on_iconv_toggled(self, widget):
global output
iconv = widget.get_active()
if iconv == True:
    output.write("USHARE_OVERRIDE_ICONV_ERR="+"True"+'\n')
else:
    output.write("USHARE_OVERRIDE_ICONV_ERR="+"False"+'\n')

def on_webif_toggled(self, widget):
global output
webif = widget.get_active()
if webif == True:
    output.write("USHARE_ENABLE_WEB="+"yes"+'\n')
else:
    output.write("USHARE_ENABLE_WEB="+"no"+'\n')

def on_telif_toggled(self, widget):
global output
telif = widget.get_active()
if telif == True:
    output.write("USHARE_ENABLE_TELNET="+"yes"+'\n')
else:
    output.write("USHARE_ENABLE_TELNET="+"no"+'\n')

def on_xbox_toggled(self, widget):
global output
xbox = widget.get_active()
if xbox == True:
    output.write("USHARE_ENABLE_XBOX="+"yes"+'\n')
else:
    output.write("USHARE_ENABLE_XBOX="+"no"+'\n')

def on_dlna_toggled(self, widget):
global output
dlna = widget.get_active()
if dlna == True:
    output.write("USHARE_ENABLE_DLNA="+"yes"+'\n')
else:
    output.write("USHARE_ENABLE_DLNA="+"no"+'\n')

def on_commit_clicked(self, widget):
commit = output.getvalue()
logfile = open('/home/boywithaxe/Desktop/ushare.conf','w')
logfile.write(commit)

def on_endprogram_clicked(self, widget):
sys.exit(0)
share|improve this question
    
Thank you! Good luck with your coding. –  izx Jun 24 '12 at 9:31
1  
@izx: no need to thank me, I need to start being more clear. :) –  boywithaxe Jun 24 '12 at 9:40
    
Is the current code in the stack exchange what you are currently using and having an issue with? –  mhall119 Jun 24 '12 at 20:25
    
@mhall119: yes, that's the most up to date version of the code. I've also added it here for convenience. –  boywithaxe Jun 24 '12 at 21:40
    
@mhall119: but replacing insertatcursor with activate runs the code smoothly. –  boywithaxe Jun 24 '12 at 21:42
add comment

1 Answer

up vote 2 down vote accepted

You're misunderstanding the meaning of the global statement in Python. Here's what the Python documentation says:

The global statement ... means that the listed identifiers are to be interpreted as globals. It would be impossible to assign to a global variable without global, although free variables may refer to globals without being declared global.

So, basically, you only need to use global blah when you need to assign to a global variable called blah. The variable, however, should already exist in the global scope. If you're just accessing a global variable or its methods (as apposied to assigning to it), you don't need to declare it as global.

So what you need is something like

output = None

def on_servername_insertatcursor(self, widget):
    global output  
    output = StringIO.StringIO()         
    servername = widget.get_text()
    output.write("USHARE_NAME="+servername+'\n')

def on_netif_changed(self, widget):
    netif = widget.get_active_text()
    output.write("USHARE_IFACE="+netif+'\n')

...

However, the whole approach with using a global variable and making the whole thing dependent on the order of function calls (on_servername_insertatcursor MUST be called first, otherwise the other functions will fail) is not a good coding practice.

UPDATE: Also, it occurred to me that the functions you're showing are in fact methods of a class, is that right? In this case, you can make output a member of the class and access it as self.output:

class MyApp(gtk.Window):

    output = None

    def __init__(...):
        ...
        self.output = StringIO.StringIO()

    def on_servername_insertatcursor(self, widget):    
        servername = widget.get_text()
        self.output.write("USHARE_NAME="+servername+'\n')

    def on_netif_changed(self, widget):
        netif = widget.get_active_text()
        self.output.write("USHARE_IFACE="+netif+'\n')

Still, the output of your app is dependent on the order user clicks the buttons, which is not cool. For example if user clicks the "dlna" checkbox twice, there will be 2 lines in the config, which I presume is not what you want.

share|improve this answer
    
Fair point. And I think I just realised what the issue is. Having set a global at def on_servername_activate (I'm using activate right now, as insertatcursor doesn't work. I might be using a wrong signal) it must be assigned there and then, so that when it goes to def on_netif_changed it exists already. I think that's what's causing the problem. Thank you for pointing that out. But now, is there a way of not making the code dependent on the order in which data is input? –  boywithaxe Jun 24 '12 at 22:26
    
Can you confirm you've seen and tried the code from the UPDATE section? There's no need to use global at all. Regarding the "way of not making the code dependent on the order in which data is input" - can you ask a new question on SO, I'll be happy to answer. It –  Sergey Jun 24 '12 at 22:33
    
... may be difficult to do that here in the comments –  Sergey Jun 24 '12 at 22:33
    
Yes I have seen the code and reflected it in mine, that is actually the most graceful way out of this ness I couls have hoped for, Thank you! I will post another question on SO regarding the order of data input –  boywithaxe Jun 24 '12 at 22:40
    
here's the question on SO –  boywithaxe Jun 24 '12 at 22:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.