tkinter keeping window on top all times on MacOS - python

I'm trying to create a screen "curtain" which blocks parts of the screen except for the mouse cursor's vicinity.
On windows, using root.wm_attributes("-topmost", "true") keeps the window on top, even if I focus on another app, perfectly. However, upon running the code on MacOS, if the focus for the window is lost, it will not keep itself on topmost.
What would be the MacOS equivalent to -topmost window manager attribute which will always keep the window on top, regardless of focus?
import tkinter as tk
class TransparentWindow(tk.Toplevel):
"""
This class is just a Toplevel window.
"""
def __init__(self, background="white", opacity=0.7):
super(TransparentWindow, self).__init__()
#self.master = master
self.configure(background=background)
self.overrideredirect(True)
self.wm_attributes("-alpha", opacity)
self.wm_attributes("-topmost", "true")
self.lift()
if __name__ == '__main__':
root = tk.Tk()
TransparentWindow()
root.mainloop()
Running this code in a High Sierra Virtual Machine resulted in the Toplevel not constantly being on top when another window is selected.

On Mac OS using overrideredirect(True) disables a lot of stuff like bind, Button presses and some events, honestly I don't know why exactly. (If anyone knows please comment). At least on my Mac I have this problem, I've read and seen that not all of Mac users have this problem.
So this is why root.wm_attributes("-topmost", "true") is not working. But don't worry I got a workaround.
From your code I can tell that you want a borderless window, here is how I do it with all bindings and event working still.
I first put overrideredirect(True) then in the next line
overrideredirect(False)
Also you don't need root.lift() in this case.
Ok try this code and see if the button press normally.
Sample
import tkinter as tk
root = tk.Tk()
root.overrideredirect(True)
# root.overrideredirect(False) # Uncomment and try again.
tk.Button(root, text="Borderless").pack()
root.wm_attributes("-topmost", "true")
root.wm_attributes("-alpha", 0.7)
root.wm_attributes("-topmost", "true")
# Doesn't matter if you use lift() or not with the use of root.overrideredirect(False) as well
root.lift()
root.mainloop()
I hope this helped you.
Here is your code which worked exactly you want (At least on my Mac).
import tkinter as tk
class TransparentWindow(tk.Toplevel):
"""
This class is just a Toplevel window.
"""
def __init__(self, background="white", opacity=0.7):
super(TransparentWindow, self).__init__()
#self.master = master
self.configure(background=background)
self.overrideredirect(True)
self.overrideredirect(False)
self.wm_attributes("-alpha", opacity)
self.wm_attributes("-topmost", "true")
# self.lift()
if __name__ == '__main__':
root = tk.Tk()
TransparentWindow()
root.mainloop()

Related

How to pause window to turn on another window?

I use tkinter and CTK:
I have created a page for login and I want to stop or use this page when the user is logged in, I want to show a new window and I want to resume the window when I want? How can I do that, I didn't know how to make it
I'll bite. Here's an example application that opens a second window when the user clicks a button on the main window, and disables interaction with the main window until the second window is closed.
Some configuration has been omitted for brevity, and I'm not using CTk here because I don't know how you've implemented it in your specific application - however, it should be easy enough to modify this example to work with CTk.
import tkinter as tk
from tkinter import ttk
class App(tk.Tk):
def __init__(self):
super().__init__()
self.open_button = ttk.Button(
self,
text='Open 2nd Window',
command=self.modal
)
self.open_button.pack()
def modal(self):
self.window = tk.Toplevel(self) # create new window
# bind a handler for when the user closes this window
self.window.protocol('WM_DELETE_WINDOW', self.on_close)
# disable interaction with the main (root) window
self.attributes('-disabled', True)
self.close_button = ttk.Button(
self.window,
text='Close Modal',
command=self.on_close
)
self.close_button.pack()
def on_close(self):
# re-enable interaction the root window
self.attributes('-disabled', False)
# close the modal window
self.window.destroy()
if __name__ == '__main__':
app = App()
app.mailoop() # run the app
In the future:
Provide code that shows you made a good-faith effort to solve the problem on your own
Don't post the same question multiple times within hours - if you need to make changes, edit the original question
If you mean you want to open up another window to do something before going back to the original window, you should consider using message box. Here is a link that goes over the types of messageboxes: https://docs.python.org/3/library/tkinter.messagebox.html.

The state of a Tkinter window is not tracked correctly when maximized

I'm currently trying to resize a Tkinter window to a specific size. It is working great except when the window is maximized using the maximize button in the top toolbar. Here is a simple code that I've done for example.
When the window is maximized using the button, the print output is "normal" while it should be "zoomed". The window's size is then locked until the maximize button is pressed again.
I am working on Red Hat Enterprise Linux if it can help. Has anyone already had this problem before?
from tkinter import *
class Main:
def __init__(self, root):
self.root = root
Button(self.root, command=self.resize, text="Resize").pack()
def resize(self):
print(self.root.state())
self.root.attributes("-zoomed", False)
self.root.geometry("800x300")
root = Tk()
x = Main(root)
root.mainloop()

How to fully close tkinter programm?

How would I go about writing a command that fully closes the currently open tkinter programm. There are already a ton of threads regarding this, but I have been unable to find a solution that reliably closes all open tkinter windows. In my scenario, I am adding menus to each window with the option to close the current window, as well as close the whole programm. While closing the current window always works, closing the whole programm only works sporadically.
Things I have tried:
using root.quit() frequently leads to the "Python not responding" windows error message.
using root.destroy() will close the root window, and all toplevel windows, if I have not clicked on anything in them. Once I do use a toplevel window, it will not be ended by the root.destroy() command anymore.
using mytoplevel.destroy() will reliably close the currently active toplevel window.
I have written a simplified version of the code I am using, however, the windows don't do anything here, so the destroy command will always work.
import tkinter as tk
class MainWindow(tk.Frame):
def __init__(self, master = None):
b = tk.Button(master, text="Type_1", command=self.window_type_1)
b.pack()
self.load_menu(root)
tk.Frame.__init__(self, master)
def close(self, parent):
parent.destroy()
def window_type_1(self):
top = tk.Toplevel()
top.minsize(width = 600, height = 500)
self.load_menu(top)
def load_menu(self, parent):
menubar = tk.Menu(parent, tearoff=False)
parent.config(menu=menubar)
start_menu = tk.Menu(menubar, tearoff=False)
if parent == root:
start_menu.add_command(label="close", command=lambda: self.close(parent))
menubar.add_cascade(label="File", menu=start_menu)
else:
start_menu.add_command(label="Window close", command=lambda: self.close(parent))
start_menu.add_command(label="Programm close", command=lambda: self.close(root))
menubar.add_cascade(label="File", menu=start_menu)
root = tk.Tk()
mainwindow = MainWindow(master = root)
mainwindow.mainloop()
Calling destroy on the root window is generally the right solution, and is specifically the right solution in your example program. Destroying the root window will cause mainloop to exit, so unless you have more code after the call to mainloop and/or more code running in a separate thread, the program must exit.

Using Python, how do you call a tkinter GUI from another GUI?

I created a couple of GUIs using tkinter. But now I am interested in combining them into one caller GUI. So the caller GUI would have buttons that, when clicked, would open the other GUIs. However, I cannot get it to work. I've done the imports correctly (I think), edited the main functions in the subGUIs, and added the command=GUI.main in my buttons. I get it to load but I get errors about missing files...but when I run a GUI by itself it works fine.
In my research, I read that there can only be one mainloop in a Tkinter program. Basically, I cannot use a Tkinter GUI to call another Tkinter GUI. Do you know what I can do different, for instance, can I create the caller GUI using wxPython and have it call all other GUIs that use Tkinter?
Thank you!
You can't "call" another GUI. If this other GUI creates its own root window and calls mainloop(), your only reasonable option is to spawn a new process. That's a simple solution that requires little work. The two GUIs will be completely independent of each other.
If you have control over the code in both GUIs and you want them to work together, you can make the base class of your GUI a frame rather than a root window, and then you can create as many windows as you want with as many GUIs as you want.
For example, let's start with a simple GUI. Copy the following and put it in a file named GUI1.py:
import tkinter as tk
class GUI(tk.Frame):
def __init__(self, window):
tk.Frame.__init__(self)
label = tk.Label(self, text="Hello from %s" % __file__)
label.pack(padx=20, pady=20)
if __name__ == "__main__":
root = tk.Tk()
gui = GUI(root)
gui.pack(fill="both", expand=True)
tk.mainloop()
You can run that GUI normally with something like python GUI1.py.
Now, make an exact copy of that file and name it GUI2.py. You can also run it in the same manner: python GUI2.py
If you want to make a single program that has both, you can create a third file that looks like this:
import tkinter as tk
import GUI1
import GUI2
# the first gui owns the root window
win1 = tk.Tk()
gui1 = GUI1.GUI(win1)
gui1.pack(fill="both", expand=True)
# the second GUI is in a Toplevel
win2 = tk.Toplevel(win1)
gui2 = GUI2.GUI(win2)
gui2.pack(fill="both", expand=True)
tk.mainloop()
Depending on your OS and window manager, one window might be right on top of the other, so you might need to move it to see both.
Thank you for the ideas. At first, your code wouldn't print the text on the toplevel window. So I edited it a little and it worked! Thank you. GUI1 and GUI2 look like:
import tkinter as tk
def GUI1(Frame):
label = tk.Label(Frame, text="Hello from %s" % __file__)
label.pack(padx=20, pady=20)
return
if __name__ == "__main__":
root = tk.Tk()
GUI1(root)
root.mainloop()
And then the caller looks like this:
from tkinter import *
import GUI1
import GUI2
def call_GUI1():
win1 = Toplevel(root)
GUI1.GUI1(win1)
return
def call_GUI2():
win2 = Toplevel(root)
GUI2.GUI2(win2)
return
# the first gui owns the root window
if __name__ == "__main__":
root = Tk()
root.title('Caller GUI')
root.minsize(720, 600)
button_1 = Button(root, text='Call GUI1', width='20', height='20', command=call_GUI1)
button_1.pack()
button_2 = Button(root, text='Call GUI2', width='20', height='20', command=call_GUI2)
button_2.pack()
root.mainloop()

Automatically Activate Main Window in TkInter

Is it possible to automatically activate the main window of a tkinter app? I am using Yosemite on a Mac. When the window comes up, the title bar is grayed out, and I have to click on the window before it will respond to events. The Tk manual says that event generate, "Generates a window event and arranges for it to be processed just as if it had come from the window system." I tried generating a <Button-1> event, but it had no effect. The manual goes on to say, "Certain events, such as key events, require that the window has focus to receive the event properly." I tried focus_force, but it didn't work either.
Is it possible to do what I want? Is this a Mac peculiarity? In the code below, the text changes as the mouse cursor enters and leaves the label, but the app is unresponsive until you click on the window.
import tkinter as tk
root = tk.Tk()
def visit(event):
kilroy['text'] = 'Kilroy was here.'
def gone(event):
kilroy['text'] = 'Kilroy has left the building'
def startup():
root.focus_force()
root.event_generate('<Button-1>')
frame = tk.Frame(root, width=500,height=100)
kilroy = tk.Label(frame, text="Kilroy hasn't been here.", width = 50)
kilroy.grid(row=0,column=0)
frame.grid(row=0,column=0)
kilroy.grid_propagate(0)
frame.grid_propagate(0)
kilroy.bind('<Enter>', visit)
kilroy.bind('<Leave>', gone)
root.after(100,startup)
root.mainloop()

Categories

Resources