new tkinter window loses focus after mainloop ends - python

I'm using the following code to create a window after destroying another.
from tkinter import *
tk=Tk()
def destroy():
tk.destroy()
tk.after(2000,destroy)
tk.mainloop()
tk=Tk()
tk.mainloop()
The window is created alright, but it loses focus. I tried lift() and focus() methods with no result.

You can use <tk.Tk>.focus_force() to force the window to be focused.
So your modified code will look like this:
from tkinter import *
tk = Tk()
def destroy():
tk.destroy()
tk.after(2000, destroy)
tk.mainloop()
tk = Tk()
tk.focus_force()
tk.mainloop()
Although it is much better to reuse the window. Destroy all of the widgets on the window and reuse it instead of destroying and recreating it. Creating a window takes a lot of resources.
Also something else: the variable tk is usually used for something else so please don't use it for tkinter windows. Usually people use root or window for windows.

Force the input focus to the widget using focus_force(). Just update you last part.
tk=Tk()
tk.focus_force()
tk.mainloop()
Also, You can make a Toplevel() which is basically just creating a new window over the root window.
from tkinter import *
tkk=Tk()
tkk.withdraw()
tk=Toplevel()
def destroy():
tk.destroy()
tkk.deiconify()
tk.after(2000,destroy)
tk.focus_force()
tkk.mainloop()

Related

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.

How to make Toplevel() widget appear above main root window?

I have made a Toplevel widget but when it pops up it always appears below my root window. Is there an easy way I can make it come to the top most level when it pops up?
you can use the .lift() method on a Toplevel widget:
import tkinter
root = tkinter.Tk()
root.title("root")
top = tkinter.Toplevel(root)
top.title("top")
top.lift(root)
root.mainloop()
according to this documentation you should be able to just use top.lift() to raise above all other windows but it didn't seem to work for me.
Edit: calling top.lift() without arguments does work when called during the mainloop, although since this question was specifically when starting the program that isn't very useful.
try attributes
import tkinter
root = tkinter.Tk()
root.title("root")
top = tkinter.Toplevel(root)
top.attributes('-topmost', 'true')
top.title("top")
root.mainloop()

How to move the entire window to a place on the screen (Tkinter, Python3)

The title says it all. How to move the entire window to a place on the screen using tkinter. This should be moving the root frame.
Use the geometry method of the root (or any Toplevel) window. For example:
import tkinter as tk
root = tk.Tk()
root.geometry("+200+400") # places the window at 200,400 on the screen
use this:
from tkinter import Tk
main=Tk()
main.geometry('+100+200')
main.mainloop()
or do it with function :
def change_position(root_variable,x,y):
root_variable.geometry('+{}+{}'.format(x,y))
and use :change_position(main,500,400)
edit: added dot for format

Categories

Resources