I have a toplevel window that keeps appear underneath the root window, which is pretty annoying. Any idea how to make sure that the toplevel gets the focus when the function is called?
def setup(self):
self.setup_step = 1
setup_window = Toplevel(self)
setup_window.config(bg = "#000000", height = "600", width = "850")
setup_window.title("RoastMaster Setup")
setup_window.geometry("800x600")
You can try by making the toplevel window transient.
You might try raising it and giving it focus. It could be that you window manager wants the window with focus to always be on top.
Related
Hey guys so I am trying to make a timer app for studying which pops-up when break time is triggered. I am using this code to bring the window up, but the issue I am facing is if I minimize the window this code does not work anymore and the app does not pop-up. I tried to find a way to just remove the minimize button option from the window but didn't come across anything.
def raise_above_all(window):
window.attributes('-topmost', 1)
window.attributes('-topmost', 0)
You can use window.deiconify() to make it un-minimize, and use your code to make it go to front. Like this:
window.deiconify()
window.attributes('-topmost', 1)
window.attributes('-topmost', 0)
Don't go for disabling the window's minimize button. Just use window.state('zoomed') along with window.attributes('-topmost', True). This way even if the window was minimized, still it will be raised to the topmost level.
Also, define the window's maxsize prior, so that the window does not become fullscreen due to zoomed.
Here is a simple yet complete demonstration:
(To raise the window "window2", you would need to Enter 1 in the entry box of "window1")
from tkinter import *
window1 = Tk()
window1.geometry('1200x600')
window1.minsize(1200, 600)
def raise_above_all(window):
window.state('zoomed') #Needed in case the window2 is minimized
window.attributes('-topmost', True)
def create_new_window2(window):
window.maxsize(300, 300) #define the maxsize so that the window2 does not get fullscreen when 'zoomed'
window.mainloop()
def submit():
if x.get()==1:
raise_above_all(window2)
else:
print("Enter 1")
x = IntVar()
Entry(window1, textvariable=x).pack()
Button(window1, text="Enter", command=submit).pack()
window2 = Tk()
create_new_window2(window2)
window1.mainloop()
Alternatively, you can just remove the entire window's title bar using window.overrideredirect(True)
it seems that I was pending to continue the bombardment of questions. It this is short, Is it possible to disable the movement of the Tkinter window without deleting the top bar of this?
It would give a minimal and reproducible code, but if it did it would only be two lines, it would be useless.
Bind a event for your window,and set the window .geometry()
But now you can not revise the window size by dragging the window's border(But it can maximize the window.).
Here is an example of the code:
import tkinter
def GetWindowPos():
global X,Y
X = win.winfo_geometry().split("+")[1]
Y = win.winfo_geometry().split("+")[2]
win.bind_all('<Configure>', HoldOn)
def HoldOn(event):
win.geometry("+{}+{}".format(X,Y))
win = tkinter.Tk()
win.geometry("400x400+{}+{}".format(12,12))
tkinter.Label(win,text="Halo!").grid()
win.after(100,GetWindowPos)
win.mainloop()
I have found a method, but as you might know to achieve something, we have to lose something!
You can use:
root.overrideredirect(True) # turns off title bar
by which you wont be able to move the tkinter window and also Tkinter application won't be displayed in taskbar, but you will also lose the title bar.
but if you wish to have the title bar,
then you can create one by this link.
Or use below to make a new title bar and also be able to move it(from this answer)
def move_window(event):
root.geometry('+{0}+{1}'.format(event.x_root, event.y_root)
# bind title bar motion to the move window function
title_bar.bind('<B1-Motion>', move_window)
But still your Tkinter application won't show up in taskbar, here's a solution(from this answer):
root = tkinter.Tk()
top = tkinter.Toplevel(root)
root.attributes("-alpha",0.0) # to make root invisible
#toplevel follows root taskbar events (minimize, restore)
def onRootIconify(event): top.withdraw()
root.bind("<Unmap>", onRootIconify)
def onRootDeiconify(event): top.deiconify()
root.bind("<Map>", onRootDeiconify)
You can add a toplevel window under the root object, make toplevel invisible and then handle the icon events of top level to hide or show the root window on taskbar.
I am having a problem with getting tkSimpleDialog to take focus over my fullscreen window GUI. I have a GUI that I am trying to use a dialog window to use as an admin password to close the GUI (like a kiosk mode app) using root.quit(). The problems with this are that the dialog does not come in front of the parent windows if the parent windows are fullscreen. Additionally, I would like to make the tkSimpleDialog go fullscreen as well.
Here is the code for how the dialog box is being called/created using the tkSimpleDialog.py in my program:
def check_admin_password():
# use askstring here to verify password.
pass_attempt = simpledialog.askstring("Verifying access","Please enter Admin password", parent=window, show="*")
if pass_attempt == "password":
root.quit() # used whatever your instance of Tk() is here in place of root.
admin_minimize_button = Button(window, text = "Administrator", command = check_admin_password, width=35, height=12)
admin_minimize_button.grid(row=4, column=0)
I am using the following code for the parent window and I believe there is something with the overrideredirect(True) that is affecting the focus of my dialog window:
qwindow = Toplevel(root) #renames Toplevel "popup" window frame to variable "qwindow"
qwindow.title('Mission Queue') #creates title on popup window
w, h = qwindow.winfo_screenwidth(), qwindow.winfo_screenheight() #aquires dimensions from display size
qwindow.geometry("%dx%d+0+0" % (w, h)) #sets window size to aquired dimensions
qwindow.overrideredirect(True) #removes top bar and exit button from parent window frame
I have tried editing the tkSimpleDialog.py file and adding an overrideredirect(True) line however that is not working. If more information is needed, let me know. Any advice would be much appreciated. Thanks in advance.
Due to my inability to figure out a solution using tkSimpleDialog, I attempted a different approach creating my own "dialog". Unfortunately, I was still unable to get the dialog to have the entry line take focus with the screen window being set to wm_attributes('-fullscreen', 'True') and overrideredirect(True). While it would have been better to have both work; for my purposes, it will work fine to just be fullscreen in my application without overrideredirect(True). For anyone with a similar issue or wanting to see more documentation on my progress, heres the link to my other forum post: (Tkinter Entry Widget with Overrideredirect and Fullscreen).
Thanks for the help!
Say I have some simple code, like this:
from Tkinter import *
root = Tk()
app = Toplevel(root)
app.mainloop()
This opens two windows: the Toplevel(root) window and the Tk() window.
Is it possible to avoid the Tk() window (root) from opening? If so, how? I only want the toplevel. I want this to happen because I am making a program that will have multiple windows opening, which are all Toplevel's of the root.
Thanks!
The withdraw() method removes the window from the screen.
The iconify() method minimizes the window, or turns it into an icon.
The deiconify() method will redraw the window, and/or activate it.
If you choose withdraw(), make sure you've considered a new way to exit the program before testing.
e.g.
from Tkinter import * # tkinter in Python 3
root = Tk()
root.withdraw()
top = Toplevel(root)
top.protocol("WM_DELETE_WINDOW", root.destroy)
but = Button(top, text='deiconify')
but['command'] = root.deiconify
but.pack()
root.mainloop()
The protocol() method can be used to register a function that will be called when the
Toplevel window's close button is pressed. In this case we can use destroy() to exit.
I have a (second) tkinter window, which, when opened, does not get the focus, but rather the first window remains focused (although the second window appears in front of the other).
It contains a textbox which I want to be able to type in, but I have to double-click it in order to type.
How do I focus the textbox when opening the window?
My tries:
textbox.focus_set(),
window.grab_set(),
window.focus_set()
None of them did what I wanted to do.
EDIT:
Instead, .focus_set() raises an error when (and only when) closing the main window: can't invoke "focus" command: application has been destroyed
This is my current code (tkWin is the main window, tkcWinis the second window):
def click(self, field):
import _tkinter
if field != None:
try:
self.tkcWin = Tk()#creating window
self.tkcWin.focus()
self.tkcWin.title(field)
self.tkcWin.geometry('300x100')
self.mainframe = Frame(master=self.tkcWin,background="#60BF98")
self.mainframe.place(x=0, y=0, width=300, height=300)
self.textb = Text(master=self.mainframe)
self.textb.place(x=0, y=50)
self.textb.bind("<Return>",lambda a: self.setM(field))
self.textb.bind("<Return>",lambda a: self.tkcWin.destroy(),True)
self.tkcWin.grab_set()
self.tkWin.wait_window(self.tkcWin)
self.textb.focus_set()
hwnd = self.tkcWin.winfo_id()
ctypes.windll.user32.SetFocus(hwnd)
self.tkcWin.mainloop()
except _tkinter.TclError:
self.tkcWin.destroy()
It turns out that you can simply call the secondary window's deiconify() method and then the widget's focus_set() method:
toplevel.deiconify()
text.focus_set()
Here's the original work-around for Windows (no longer recommended):
Start by adding import ctypes at the top.
Go ahead and focus your widget like you have with: text.focus_set()
Get the hwnd of the second window: top_hwnd = toplevel.winfo_id()
And finally activate the second window with: ctypes.windll.user32.SetFocus(top_hwnd)