Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing variables to Tkinter geometry method

Tags:

python

tkinter

I've been trying for a while now to find out if there is a way to pass variables to the geometry method in Tkinter.

I know the geometry method accepts a string:

root = Tk()
root.geometry("1200X1024")

I want to open the window based in the screen width.

root = Tk()
w = root.winfo_screenwidth()
h = root.winfo_screenheight()

What I've tried is this:

geometry = "%dX%d" % (w,h)
root.geometry(geometry)

or

root.geometry(("%dX%d" % (w,h)))

No matter how I concatenate the variables it gives this error:

TlcError: bad geometry specifier "1280X1024"

So is it possible to pass variables to the geometry method?


2 Answers

#works for python 3.4

screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
screen_resolution = str(screen_width)+'x'+str(screen_height)

window.geometry(screen_resolution)
like image 138
MAHENDRA Avatar answered Jun 07 '26 08:06

MAHENDRA


The Tk.geometry method is not very "smart" when it comes to interpreting the string you give it. Instead, it requires you to specify the window size exactly as:

<width>x<height>

Meaning, your uppercase X is confusing it. Simply change X to x and your code will work fine.