How can I open a Toplevel Window on a random location on my screen?

37 Views Asked by At

I am currently building a Tkinter application which opens a new Toplevel window. Now I am trying to figure out how to randmoize the position of the Toplevel (which I set to overrideredirect(True)).

This code works like I want it to but because of the overrideredirect the windows appear on top of each other, blocking the access to the root window.

import tkinter as tk
from tkinter import *

from PIL import Image, ImageTk

window = tk.Tk()
window.geometry("500x500")
window.title("ApplicationTest")
window.overrideredirect(True)

photo = Image.open("C:\ImageClose\close.png")

resized_image = photo.resize((61, 27))
new_image = ImageTk.PhotoImage(resized_image)

def open():
    w2 = Toplevel()
    w2.title("ApplicationTest")
    w2.geometry("500x500")
    w2.overrideredirect(True)
    cl2Btn = Button(w2, command=open, image=new_image, borderwidth=0, highlightthickness=0, background="black", activebackground="black", padx="0", pady="0")
    cl2Btn.pack(side=tk.TOP, anchor=tk.NE)
    w2label = Label(window, text="Hello", font=('Cascadia Code', 15), width="500", height="500", bg="black",
                     fg="green")
    w2label.pack()



closeBtn = tk.Button(window, command=open, image=new_image, borderwidth=0, highlightthickness=0, background="black", activebackground="black", padx="0", pady="0")

closeBtn.pack(side=tk.TOP, anchor=tk.NE)

label = tk.Label(window, text="Hello", font=('Cascadia Code', 15), width="500", height="500", bg="black", fg="green")
label.pack()



window.mainloop()


0

There are 0 best solutions below