Is it possible to place widgets off window?

Question:

My question is simple – is it possible to place widgets out of bounds?
For example, I want a button to be not on tkinter.Tk, but it will be somewhere else like in the center of monitor.

Asked By: ColorfulYoshi

||

Answers:

tkinter.Tk are only can place within bounds of a parent widget. For this you can need to create transparent tkinter.Toplevel, then place in the needed location using method called geometry.

import tkinter as tk

source = tk.Tk()
top = tk.Toplevel(source)
top.attributes('-alpha', 0.0)
width = source.winfo_screenwidth()
height = source.winfo_screenheight()
buttonsize = 50  
x = (width - buttonsize) // 2 
y = (height - buttonsize) // 2  
top.geometry(f"{buttonsize}x{buttonsize}+{x}+{y}")
button = tk.Button(top, text="Click me!")
button.pack(fill=tk.BOTH, expand=True)

source.mainloop()
Answered By: vinoth raj
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.