0

My program uses tkinter.asksaveasfilename function to save a text into another location but when user opens save as dialog, tkinter's empty square window also pops-up. My program works on console so I don't need the tkinter window.

I searched the web and stackoverflow but only found root.withdraw funtion to hide tkinter window but when I use it before asksaveasfilename function, save as dialog never pops up. If I use root.withdraw after asksaveasfilename function, square empty tkinter window closes after user closes the save as dialog.

Is there a way to hide tkinter window when asksaveasfilename function is active?

Yılmaz Alpaslan
  • 327
  • 3
  • 16
  • Can you provide the code that uses `root.withdraw()` before the `asksaveasfilename`? I think something is wrong with your code. – TheLizzard Jun 01 '21 at 17:35
  • @TheLizzard I tried copying the save as part from my code into a fresh .py file and runned it, interestingly it worked. But with same part of code, it's not working with the whole code. If I give link to my whole code, can you examine it? https://github.com/Yilmaz4/Encrypt-n-Decrypt/blob/python/Encrypt.py The part is starting from line 116 and finishing in 133. – Yılmaz Alpaslan Jun 02 '21 at 04:25
  • Just after line `117` add `root.withdraw()`. I can't find a reason for why it wouldn't work – TheLizzard Jun 02 '21 at 09:12
  • @TheLizzard I moved save as lines between 116 and 133 to the beginning of the code and it worked, I moved them inside the first while loop, it worked again, it continued working till I put these lines into the if statement in line 27 and save as dialog doesn't popped up. After this line, save as dialog never popped up in below lines. Very weird. Do you think it's a Python bug or what? – Yılmaz Alpaslan Jun 02 '21 at 18:29
  • Right now there are too many variables to keep track of. Please look at how to define and use python functions. Also your code can be simplified greatly eg: `if exit == True: exit = True; break` can be simplified into: `if exit: break`. I don't get what most of the code is doing. – TheLizzard Jun 02 '21 at 19:18
  • @TheLizzard I will try to remove most of variables and test. Also my code is a encryption program. You can learn more from my github page. Also yes you're right. I can simplyfie my code like this. Have a nice day. – Yılmaz Alpaslan Jun 03 '21 at 16:50

1 Answers1

1

Does this work for you?:

from tkinter.filedialog import asksaveasfilename
import tkinter as tk

# Create the window
root = tk.Tk()
# Hide the window
root.withdraw()

# Now you are free to popup any dialog that you need
print(asksaveasfilename())

# Destroy the window
root.destroy()

It works for me (Windows 10). I think that it should work for Linux and MacOS too.

TheLizzard
  • 7,248
  • 2
  • 11
  • 31
  • Interestingly, this works for me. I tried the same thing in my code but it didn't worked. I mean I am simply creating the root, withdrawing it, then opening save as dialog after that destroying root. But save as dialog never pops up. I'll write save as dialog part again. If it works, then problem is with my code. – Yılmaz Alpaslan Jun 02 '21 at 04:14
  • The problem with this solution is that it hides the file dialog from the task bar. If other windows are in front of the file dialog, you have to minimize each of those windows one by one, or look for the file dialog in the Task View rather than task bar. – root Feb 27 '22 at 11:12
  • @root I didn't check for that when I was writing this answer. Just now I tested it on Ubuntu and the file dialog is in the task bar. Did you test it on Windows? – TheLizzard Feb 28 '22 at 16:39
  • @TheLizzard Yes, on Windows it's not in the taskbar. Here's a solution without this problem: https://stackoverflow.com/a/68424608/5231110 – root Feb 28 '22 at 17:09