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)
You're misunderstanding the meaning of the
global
statement in Python. Here's what the Python documentation says:So, basically, you only need to use
global blah
when you need to assign to a global variable calledblah
. 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 asglobal
.So what you need is something like
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 asself.output
: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.