Tkinter not working the first time it runs with withdraw() - python

I am trying to make a Tkinter script to select files through Windows File Explorer. I don't need the Tkinter window to show, just the File Explorer interface.
import tkinter
from tkinter import filedialog
import os
window = tkinter.Tk()
#window.geometry("1x1")
window.withdraw()
def open_files():
files = filedialog.askopenfiles(mode='r')
global filenames
filenames = [os.path.abspath(file.name) for file in files]
window.destroy() # analysis:ignore #says "window" is undefined becasue of "del window" below
window.after(0, open_files)
window.mainloop()
del window
The first time I run this in Spyder, if window.withdraw() is not commented out, the console just shows runfile(*my_file_name*) and the code does... something... in the background, but nothing seems to actually happen. Nothing changes on-screen, but I cannot type in the console so I know the code is running.
If I open a new console tab and run the code with window.withdraw() commented out, everything works, and the Tkinter GUI is visible. If I then run this code again in the same tab, with window.withdraw() not commented out, then the code works as intended, with only the File Explorer window opening up, and the Tkinter GUI staying hidden. This is the case even if I click the "Remove all variables" button in Spyder, so as far as I understand the code is not saving any variables that allow it to run properly after the first time.
My question is, why does this code work the 2nd, 3rd, 4th, etc. time I run it, but not the first time?

I kept playing around, and changed -alpha to alpha and got this error:
TclError: wrong # args: should be "wm attributes window ?-alpha ?double?? ?-transparentcolor ?color?? ?-disabled ?bool?? ?-fullscreen ?bool?? ?-toolwindow ?bool?? ?-topmost ?bool??"
So I ended up changing window.attributes('-alpha',0) to window.attributes('-topmost',True, '-alpha',0), and this works! It brings up File Explorer on the first run without showing the Tkinter window. Thank you #Thingamabobs for your help.
My final code is:
import tkinter
from tkinter import filedialog
import os
window = tkinter.Tk()
window.attributes('-topmost',True, '-alpha',0)
filenames = [os.path.abspath(file.name) for file in filedialog.askopenfiles(mode='r')]
window.destroy()
del window

Related

python tkinter main window

I was trying to open a code with pycharm and the following lines are the begining . but it doesn't open any window . what should I do ?
import tkinter
mainwindow=tkinter.Tk()
mainwindow.title("Calculator")
mainwindow.geometry('480x240')
buttonOne= tkinter.Button(mainwindow,text='1')
it runs and instantly closes without opening any window
In order to make sure the window doesn't close you need the mainloop function.
import tkinter
mainwindow=tkinter.Tk()
mainwindow.title("Calculator")
mainwindow.geometry('480x240')
buttonOne= tkinter.Button(mainwindow,text='1')
tkinter.mainloop()

Tkinter TopLevel not showing when it's supposed to

I have a very simple python code: a tkitner button that process some images in the background. I wanted to open a tkinter toplevel to show the user that it was doing something, but for my surprise is not working as I thought it would. The command on the tk.Button is the next method:
def processing(self):
"""Starts the images processing"""
# Open a Tk.Toplevel
aux_topLevel = Splash(self.window) # a simple Tk.Toplevel class, that works perfectly
self._process_images() # starts processing the images
# I wanted to kill here the topLevel created before
aux_topLevel.destroy()
My surprise: the window is displayed once the processing images is done (tried it out adding prints and time.sleep), however, i couldn't display the TopLevel when I wanted to.
Is there anything am I doing wrong? Any help is appreciated. Thank you.
Consider the following example and try to run it.
What you'd think should happen is that the new Toplevel window should open, some event happens for a period of time and then the window is destroyed.
What actually happens is the window is opened, but never displayed, the task occurs and then the window is destroyed.
from tkinter import *
import time
def processing():
new = Toplevel(root)
new.geometry("200x150")
lbl = Label(new,text="--")
lbl.grid()
for i in range(50):
time.sleep(0.1)
#Un-comment the line below to fix
#root.update()
print(i)
lbl['text'] = "{}".format(i)
new.destroy()
root = Tk()
root.geometry('200x100')
btnGo = Button(root,text="Go",command=processing)
btnGo.grid()
root.mainloop()
If you un-comment out the root.update() line and re-run the code, the window will be displayed.
There are better ways to deal with tasks that takes a while to process, such as threading.

How to cal tcl commands inside my python script using tkinter?

I am running my performance test on python and I want to call some Tcl commands using Tkinter inside my python script.
Can somebody please help me to write the code,
import Tkinter
root = Tkinter.Tk()
root.tk.eval('puts {printed by tcl}')
I tried simply above example, here when I do root=tkinter.tk() it opens up a window, I just want to execute my command and get the result
The code you have tried will not show any window until you put the root.mainloop(),but you can try something like this,
import tkinter
root = tkinter.Tk()
root.withdraw()
root.tk.eval('puts {printed by tcl}')
root.destroy()
root.mainloop()
here withdraw() will remove the window from the screen without destroying it and then you can perform your tasks and then destroy it at the end of code.

Python Tkinter Window not closing

So I was writing a short code to test something when I noticed this interesting behaviour.
import tkinter
from tkinter import *
master=tkinter.Tk()
master.geometry("800x850+0+0")
master.configure(background="lightblue")
def d():
master.destroy()
button=Button(master, text="asdf", command=d).pack()
master.mainloop()
The button closes the window as expected, but when I click on the red button on the top left button (from the actual window, not tkinter), the program gets stuck and doesn't respond.
However, when I change the code to remove the text in the button as follows:
import tkinter
from tkinter import *
master=tkinter.Tk()
master.geometry("800x850+0+0")
master.configure(background="lightblue")
def d():
master.destroy()
button=Button(master, command=d).pack()
master.mainloop()
It now works perfectly fine. Both the tkinter button in the window and the red button from the actual window close the window as expected.
Why does this happen?
I am using python 3.5 on Mac, in case this matters.
I tried it out on some of my friends computers and they didn't have this issue, so it appears that it was just a hardware specific problem.

askopenfilename window won't close

To allow my users to choose a file I'm using tkinter's askopenfilename. The function works fine, but after opening a file or pressing cancel the file open window goes empty and just stays open. The rest of the program moves forward correctly but even once the program ends the window remains open. And I'm not getting any errors. Is there anyone else who has experienced this behavior and has a solution? Alternatively, do you know any other easy methods of implementing a GUI file open prompt?
here's a pertinent example:
from Tkinter import Tk
from tkFileDialog import askopenfilename
Tk().withdraw()
filename = askopenfilename()
print(filename)

Categories

Resources