Python Forum
tkinter - global variable not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter - global variable not working
#1
Hello!

I am using Tkinter and doing GUI so that there is a 'save', 'save as', and 'open' button, along with a text box.
I have got everything working except the 'save' button. I Have tried to set the name from filedialog.askopenfilename(), to a global variable to use in my 'save' function.

I have written my save function so that if the file name already exists, save over that, if not do the save_as function. It works fine when I need to save to an existing file, but when I enter straight into the text box and hit 'save' (NOT save as), it does not run the save_as function, but instead tells me that 'filename' is not defined ?

Is it saying that there is no filename because I didn't open anything ?
Is there a different want to do this that I haven't thought about ?

I cant see what I am doing wrong ? Please help!

def open_file():
    global filename
    filename = filedialog.askopenfilename()
    ask_file= open(filename)
    text.insert(INSERT, ask_file.read())
    return filename

def save_as_file():
    save_file = filedialog.asksaveasfile( mode = "w", defaultextension = '.txt')
    save_f = text.get(0.0, END)
    try:
        save_file.write(save_f.rstrip())
    except:
        showerror(title = "Save Error")

def save_file():
    if os.path.exists(filename):
        save_text = text.get(0.0, END)
        file_save = open(filename, 'w')
        file_save.write(save_text)
        file_save.close()
    else:
        save_as_file()
Reply


Messages In This Thread
tkinter - global variable not working - by albry - Jan-26-2019, 03:08 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pass a variable between tkinter and toplevel windows janeik 10 2,334 Jan-24-2024, 06:44 AM
Last Post: Liliana
  Using Tkinter inside function not working Ensaimadeta 5 5,048 Dec-03-2023, 01:50 PM
Last Post: deanhystad
  tkinter toggle buttons not working Nu2Python 26 6,994 Jan-23-2022, 06:49 PM
Last Post: Nu2Python
  [Tkinter] Making entry global in tkinter with multiprocessing luckyingermany 2 2,327 Jan-21-2022, 03:46 PM
Last Post: deanhystad
  [Tkinter] Update variable using tkinter entry methon drSlump 6 5,205 Oct-15-2021, 08:01 AM
Last Post: drSlump
  [Tkinter] tkinter.Menu – How to make text-variable? Sir 3 5,643 Mar-10-2021, 04:21 PM
Last Post: Sir
  [Tkinter] tkinter global variable chalao_adda 6 11,047 Nov-14-2020, 05:37 AM
Last Post: chalao_adda
  python file(.py) not working with my tkinter project DeanAseraf1 9 7,124 Mar-22-2020, 10:58 PM
Last Post: ifigazsi
  Tkinter scaling windows conten to or with its size not working Detzi 5 4,455 Jan-12-2020, 12:42 PM
Last Post: Detzi
  [Tkinter] Mouse click event not working on multiple tkinter window evrydaywannabe 2 3,760 Dec-16-2019, 04:47 AM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020