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 9145863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:37:08+00:00 2026-06-17T10:37:08+00:00

I am working with GTK in Python. I noticed that it is easy to

  • 0

I am working with GTK in Python. I noticed that it is easy to SAVE AS but for some reason I cannot just SAVE a file. I thought I’d set it up to check if it was saved at all and then SAVE but for some reason it restarts the function over and over w/o saving the value of filename. The thing that is killing me is this feels like a beginner mistake. somehow I have forgotten to keep the value after the function has been run. I hope this makes sense.

def SaveFile(filename):
    chooser = gtk.FileChooserDialog("Save File...", None,
                                    gtk.FILE_CHOOSER_ACTION_SAVE,
                                    (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, 
                                     gtk.STOCK_SAVE, gtk.RESPONSE_OK))

    filename = chooser.get_filename()
    if filename == None:
        response = chooser.run()
        if response == gtk.RESPONSE_OK:
            filename = chooser.get_filename()
            chooser.destroy()
            wbuffer = textview.get_buffer()
            text = wbuffer.get_text(wbuffer.get_start_iter(), wbuffer.get_end_iter())
            openfile = open(filename,"w")
            openfile.write(text)
            openfile.close()
            print filename, "this is the first part"
            return filename
        else:
            chooser.destroy()
    elif filename != None:
        wbuffer = textview.get_buffer()
        text = wbuffer.get_text(wbuffer.get_start_iter(), wbuffer.get_end_iter())
        openfile = open(filename,"w")
        openfile.write(text)
        openfile.close()
        print filename, "made it this far"
        return filename
    else:
        chooser.destroy()
        return filename
  • 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-17T10:37:09+00:00Added an answer on June 17, 2026 at 10:37 am

    As far as I can tell, you don’t persist your filename in any way between the calls. You call filename = chooser.get_filename() on a newly created FileChooseDialog – this will always return None.

    As I don’t know more about the architecture of your program, I can just propose using a global variable to persist the filename, e.g.:

    filename = None
    
    def SaveFile(filename):
        global filename
        if filename == None:
            chooser = gtk.FileChooserDialog("Save File...", None,
                                        gtk.FILE_CHOOSER_ACTION_SAVE,
                                        (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, 
                                         gtk.STOCK_SAVE, gtk.RESPONSE_OK))
            response = chooser.run()
            if response == gtk.RESPONSE_OK:
                filename = chooser.get_filename()
                chooser.destroy()
                wbuffer = textview.get_buffer()
                text = wbuffer.get_text(wbuffer.get_start_iter(), wbuffer.get_end_iter())
                openfile = open(filename,"w")
                openfile.write(text)
                openfile.close()
                print filename, "this is the first part"
                return filename
            else:
                chooser.destroy()
        elif filename != None:
            wbuffer = textview.get_buffer()
            text = wbuffer.get_text(wbuffer.get_start_iter(), wbuffer.get_end_iter())
            openfile = open(filename,"w")
            openfile.write(text)
            openfile.close()
            print filename, "made it this far"
            return filename
        else:
            chooser.destroy()
            return filename
    

    But remember, altering global variables is always dangerous, maybe you can think of a better approach. If you put your method into a class, you could make filename an attribute of this class, e.g., self._filename.

    Another remark: Don’t ever duplicate code. In the if-branch and in the elif-branch, you call

            wbuffer = textview.get_buffer()
            text = wbuffer.get_text(wbuffer.get_start_iter(), wbuffer.get_end_iter())
            openfile = open(filename,"w")
            openfile.write(text)
            openfile.close()
    

    Put this into a separate method. I promise you’ll forget to change the second occurrence of this code snippet if you ever come to change your file-writing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a Python/GTK project I've been working on for a while, and some
I have some working code that I altered but I can't seem to get
I'm working with Python and threads by some time, but I still have a
Working with an undisclosed API, I found a function that can set the number
I'working on this nice example that shows a webcam output in a GTK widget
I'm currently working on a small script that needs to use gtk.StatusIcon() . For
I am working with python and gtk. There is a checker thread, which looks
GTK's toggled signal fires when a radio button is selected, but before that, it
I'm working on my first Python and GTK script. I'm trying to do a
I'm working with a library (TreeView in Gtk to be specific) that allows you

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.