Luke Redwine
Luke Redwine

Reputation: 83

Random Tkinter window placement not working

I am trying to make a Tkinter program, that when executed, places the window at a random point of the screen each time. For the x-axis, I use a range between 100 and 1280, and for the Y-axis I use a range between 100 and 800.

import tkinter
import random

randx = random.randint(100,1280)
randy = random.randint(100,800)

win = tkinter.Tk()
win.geometry("500x300+randx()+randy()")
win.title('button')

win.mainloop()

When executed, it gives me this error: "_tkinter.TclError: bad geometry specifier "500x300+randx()+randy()"

I'm unsure how the placement system works. if I just plug in other single numbers instead of "randx()" or "randy()", it works, but then it's not random each time. what am I missing here?

Upvotes: 0

Views: 345

Answers (1)

Henry
Henry

Reputation: 3942

Firstly randx and randy are not functions, so you don't need the parentheses after them.
To put randx and randy into the string, you need to use string formatting like below:

win.geometry("500x300+{}+{}".format(randx, randy))

To learn more about string formatting, have a look at this.

Upvotes: 2

Related Questions