Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8125515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:50:41+00:00 2026-06-06T06:50:41+00:00

I’m trying to use a global variable. I’ve declared it as global to begin

  • 0

I’m trying to use a global variable. I’ve declared it as global to begin with and the declare it at such at each mention, but I get a NameError after the first function completes. Here’s the code, and I think I’ve gone stare crazy but I can’t seem to find the problem.

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)

What’s amazing is that when insertatcursor (coming from Gtk.TextBuffer.insert_at_cursor() ) is replace with activate, the code works perfectly, except I don’t want to have the user have to press enter after every data input.

EDIT. The Traceback is as follows

Traceback (most recent call last):
File "/home/boywithaxe/Developer/Quickly/broadcast/broadcast/BroadcastWindow.py", line     58, in on_netif_changed
output.write("USHARE_IFACE="+netif+'\n')
NameError: global name 'output' is not defined

Having made the changes suggested by @jdi (Thank you btw, I see the logic behind that), the Traceback I get is as follows:

Traceback (most recent call last):
File "/home/boywithaxe/Developer/Quickly/broadcast/broadcast/BroadcastWindow.py", line 55, in on_netif_changed
OUTPUT.write("USHARE_IFACE="+netif+'\n')
NameError: global name 'OUTPUT' is not defined
Traceback (most recent call last):
File "/home/boywithaxe/Developer/Quickly/broadcast/broadcast/BroadcastWindow.py", line 72, in on_iconv_toggled
OUTPUT.write("USHARE_OVERRIDE_ICONV_ERR="+"True"+'\n')
NameError: global name 'OUTPUT' is not defined
Traceback (most recent call last):
File "/home/boywithaxe/Developer/Quickly/broadcast/broadcast/BroadcastWindow.py", line 79, in on_webif_toggled
OUTPUT.write("USHARE_ENABLE_WEB="+"yes"+'\n')
NameError: global name 'OUTPUT' is not defined
Traceback (most recent call last):
File "/home/boywithaxe/Developer/Quickly/broadcast/broadcast/BroadcastWindow.py", line 86, in on_telif_toggled
OUTPUT.write("USHARE_ENABLE_TELNET="+"yes"+'\n')
NameError: global name 'OUTPUT' is not defined
Traceback (most recent call last):
File "/home/boywithaxe/Developer/Quickly/broadcast/broadcast/BroadcastWindow.py", line 93, in on_xbox_toggled
OUTPUT.write("USHARE_ENABLE_XBOX="+"yes"+'\n')
NameError: global name 'OUTPUT' is not defined
Traceback (most recent call last):
File "/home/boywithaxe/Developer/Quickly/broadcast/broadcast/BroadcastWindow.py", line 100, in on_dlna_toggled
OUTPUT.write("USHARE_ENABLE_DLNA="+"yes"+'\n')
NameError: global name 'OUTPUT' is not defined
Traceback (most recent call last):
File "/home/boywithaxe/Developer/Quickly/broadcast/broadcast/BroadcastWindow.py", line 105, in on_commit_clicked
commit = OUTPUT.getvalue()
NameError: global name 'OUTPUT' is not defined
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T06:50:43+00:00Added an answer on June 6, 2026 at 6:50 am

    Your code example doesn’t require a global. Just remove it. The only time you need to use the global keyword in a function is if you are going to assign to it.

    OUTPUT = StringIO.StringIO()         
    
    def on_servername_insertatcursor(self, widget):
        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')
    
    def on_port_insertatcursor(self, widget):
        port = widget.get_text()
        OUTPUT.write("USHARE_PORT="+port+'\n')
    
    def on_telprt_insertatcursor(self, widget):
        telprt = widget.get_text()
        OUTPUT.write("USHARE_TELNET_PORT="+telprt+'\n')
    
    def on_dirs_insertatcursor(self, widget):
        dirs = widget.get_text()
        OUTPUT.write("USHARE_DIR="+dirs+'\n')
    
    def on_iconv_toggled(self, widget):
        iconv = widget.get_active()
        if iconv == True:
            OUTPUT.write("USHARE_OVERRIDE_ICONV_ERR="+"True"+'\n')
        else:
            OUTPUT.write("USHARE_OVERRIDE_ICONV_ERR="+"False"+'\n')
    

    Even if you wanted to be able to reset your StringIO object in a function, it still wouldn’t require an assignment or global keyword:

    def reset_output(self):
        OUTPUT.seek(0)
        OUTPUT.truncate()
    

    Proof that it works

    import StringIO
    
    OUTPUT = StringIO.StringIO()         
    
    def foo():
        OUTPUT.write('foo')
    
    def bar():
        OUTPUT.write('bar')
    
    def print_output():
        print OUTPUT.getvalue()
    
    def reset_output():
        OUTPUT.seek(0)
        OUTPUT.truncate()
    
    if __name__ == "__main__":
        foo()
        bar()
        print_output()
        reset_output()
        print_output()
    

    Output

    $ python test.py 
    foobar
    
    $
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.