Tkinter filedialog breaks entry widgets - python

tl;dr: When the application calls tkinter.filedialog, entry fields do not properly focus.
Long explanation:
When initializing a tkinter application, the entry fields are enabled by default. Their state is tk.ENABLED, they can be focused on by scrolling through fields with tab, and, most importantly, they can be clicked on to select the field.
For some reason, this behavior is broken by calling tkinter.filedialog. If a method of tkinter.filedialog is called, such as askdirectory or askopenfile(), the entry field will still have the tk.ENABLED state, and the background will be properly styled, but clicking on the entry field will not insert the cursor or select the field. Typing, of course, does not register.
This can be worked around by toggling to a different window and toggling back. However, the file dialog windows (properly) return the user directly back to the main window, and so users are always presented with a main window that appears to be locked up.
See this example:
import tkinter as tk
from tkinter import filedialog
BR8K = True
root = tk.Tk()
if BR8K:
filedialog.askdirectory()
entry = tk.Entry(root, takefocus=True, highlightthickness=2)
entry.grid(sticky="WE")
root.mainloop()
Here, the code behaves properly if BR8K is False, and incorrectly if BR8K is True.
(Note: In a production environment, this would be object oriented. The issue persists in an object oriented environment.)

This is a known issues resulting from a dialog window being called prior to the mainloop() being reached for the first time.
The simplest way to fix this is to add update_idletask() before the dialog.
Try this:
import tkinter as tk
from tkinter import filedialog
BR8K = True
root = tk.Tk()
# By adding this you avoid the focus breaking issue of calling dialog before the mainloop() has had its first loop.
root.update_idletasks()
if BR8K:
filedialog.askdirectory()
entry = tk.Entry(root, takefocus=True, highlightthickness=2)
entry.grid(sticky="WE")
root.mainloop()

Related

Is there a way to make a custom askopenfilenames dialog window or at least edit its title bar, close button, etc in tkinter?

I'm making an app in tkinter which uses the ttk.Scale widget to show the process of an mp3 song.
I have a function that I want to add buttons with the names of which (the buttons) should be relied on filenames. Therefore I've made this example:
from tkinter import Tk, Button
from tkinter.filedialog import askopenfilenames
from tkinter.ttk import Scale
from threading import Timer
root = Tk()
slider = Scale(root, from_=0, to=100, orient='horizontal')
slider.pack()
# slider is continuously set to a bigger number so that it keeps going
def update_slider(num):
slider.set(num)
num += 1
root.after(50, update_slider, num)
update_slider(num=0)
# this function creates buttons based on the files opened
def add_buttons():
# the 'X' button of this particular window slows down execution of update_slider function
files = askopenfilenames(title='Add Buttons')
for i in list(files):
Button(root, text=i).pack()
button = Button(root, text='Browse', command=lambda: Timer(0.1, add_buttons).start())
button.pack()
root.mainloop()
The problem I'm facing is that when I open the askopenfilenames dialog box or when I press its 'X' button, my slider which is running continuously in the background gets stuck, and as a result doesn't show the process correctly.
Here is a picture where I hold down the 'X' button and the ttk.Scale stops moving:
I've tried using threading to run the add_buttons function but the behavior of the program remains the same.
Can I edit the askopenfilenames dialog box with something similar like overrideredirect(True) so that I can make my own title bar and 'X' button and the events generated not to slow down my Scale?
Replying to:
I cannot reproduce the issue in Linux, the scale keeps moving no matter what I do with the filedialog window. So this may be an OS specific issue.
I'm aware that this problem doesn't appear on Linux. I faced the same problem with the root's close button and other Toplevels' close button, but I fixed it by replacing the title bar using overrideredirect(True).
Is there anything similar I can do with this askopenfilenames window?

Why can I not type in my Tkinter entry field [duplicate]

tl;dr: When the application calls tkinter.filedialog, entry fields do not properly focus.
Long explanation:
When initializing a tkinter application, the entry fields are enabled by default. Their state is tk.ENABLED, they can be focused on by scrolling through fields with tab, and, most importantly, they can be clicked on to select the field.
For some reason, this behavior is broken by calling tkinter.filedialog. If a method of tkinter.filedialog is called, such as askdirectory or askopenfile(), the entry field will still have the tk.ENABLED state, and the background will be properly styled, but clicking on the entry field will not insert the cursor or select the field. Typing, of course, does not register.
This can be worked around by toggling to a different window and toggling back. However, the file dialog windows (properly) return the user directly back to the main window, and so users are always presented with a main window that appears to be locked up.
See this example:
import tkinter as tk
from tkinter import filedialog
BR8K = True
root = tk.Tk()
if BR8K:
filedialog.askdirectory()
entry = tk.Entry(root, takefocus=True, highlightthickness=2)
entry.grid(sticky="WE")
root.mainloop()
Here, the code behaves properly if BR8K is False, and incorrectly if BR8K is True.
(Note: In a production environment, this would be object oriented. The issue persists in an object oriented environment.)
This is a known issues resulting from a dialog window being called prior to the mainloop() being reached for the first time.
The simplest way to fix this is to add update_idletask() before the dialog.
Try this:
import tkinter as tk
from tkinter import filedialog
BR8K = True
root = tk.Tk()
# By adding this you avoid the focus breaking issue of calling dialog before the mainloop() has had its first loop.
root.update_idletasks()
if BR8K:
filedialog.askdirectory()
entry = tk.Entry(root, takefocus=True, highlightthickness=2)
entry.grid(sticky="WE")
root.mainloop()

How to duplicate Tkinter pop-up

So the code initially begins with a pop up asking you if you are ready, then once you select yes, a pop-up appears and if you try and close it, it will duplicate itself. Right now, it cannot duplicate itself.
Sorry if code is bad and riddled with mistakes (I'm just a beginner.) Thanks for helping.
from tkinter import *
from tkinter import messagebox
def a():
window2 = Tk()
offset = 300 + 1*10
window2.geometry('250x50+'+str(offset)+'+'+str(offset))
window2.title('')
window2.resizable(False, False)
la = Label(window2,text = 'ccmeockeoowpeokv.').pack()
button = Button(window2, text = 'OK', command = a()).pack()
def begining():
window = Tk()
window.eval('tk::PlaceWindow %s center' %window.winfo_toplevel())
window.withdraw()
if messagebox.askyesno("heh", "Ready?.") == True:
a()
window.deiconify()
window.destroy()
window.quit()
begining()
Do NOT ever insert Tk() if there are the absolute root of application, because it's just gonna make a new App, and NOT a new Window
The only method is using Toplevel()
Here, i explain you a bit
What is Toplevel()?
The Toplevel() widget is used to create and display the toplevel windows which are directly managed by the window manager
What is the function of Toplevel()?
The function of Toplevel() is to create a new window, without using Tk(). The Tk() and Toplevel() is almost same, but Toplevel() is to create a new window, without even create new application, if you watch some tutorial, the Tk() function is used to be creating a new application
What is the difference between Toplevel() and Tk()?
Tk() is the absolute root of the application, it is the first widget that needs to be instantiated and the GUI will shut down when it is destroyed. Toplevel() is a window in the application, closing the window will destroy all children widgets placed on that window but will not shut down the program
I suggest you to read more of the docs, or watch some tkinter tutorials on Youtube
Happy coding!

Python 3 tkinter: focus_force on messagebox

I'm running python 3 code in background which should show a popup window in some situations. I'm using tkinter for this:
import tkinter as tk
from tkinter import messagebox
def popup(message, title=None):
root = tk.Tk()
root.withdraw()
root.wm_attributes("-topmost", 1)
messagebox.showinfo(title, message, parent=root)
root.destroy()
popup('foo')
The ok-button in this infobox should get the focus automatically when popping up. Sadly I'm not able to do this. I tried root.focus(), but it does not help. Any ideas how to solve that? TIA
BTW: The code should be platform independent (Linux and Windows).
Edit:
Maybe I missunderstood the focus keyword and I should clarify my question:
root = tk.Tk()
root.focus_force()
root.wait_window()
When calling the code above the root window is active, even if I worked in e.g. the browser before. Is this also possible for messagebox.showinfo? Adding root.focus_force() in the popup function does not help.
Is this even possible? Or is it necessary to create my own root window? I really like the appearance of the messagebox with the icon.
Edit 2:
Here is a video: https://filebin.net/no195o9rjy3qq5c4/focus.mp4
The editor is the active window, even after the popup was shown.
In Linux I it works as expected.
You can use the default argument in the messagebox function.
default constant
Which button to make default: ABORT, RETRY, IGNORE, OK, CANCEL, YES, or NO (the constants are defined in the tkMessageBox module).
So, here is an example to highlight the "ok" button.
import tkinter as tk
from tkinter import messagebox
def popup(message, title=None):
root = tk.Tk()
root.withdraw()
messagebox.showinfo(title, message, parent=root, default = "ok")
root.destroy()
popup('foo')
Hope this helps!

Python: How to close the previous Main (top) tkinter windows

I want my script should work in such a way that, close all the previous windows in tkinter.
If I minimise the existing pop up window and again I will run same script, it will again pop up new window. How can I close the already existing window, when i run script again?
Simple code:
import Tkinter as tk
root = tk.Tk()
root.mainloop()
Maybe if you check all variable in locals and see if you have tk type in it?
import tkinter as tk
root = tk.Tk()
tk_type = type(root)
print(tk_type)
var=0
for var in locals().items():
if type(var[1]) == tk_type:
print(var, 'to be deleted')
root.mainloop()
something like this where you can put the test at the begining of the class to see if it exists other TK window object.
I'm sure it should exist a better solution though.

Categories

Resources