tkinter.TclError: bad geometry specifier (bug?)

I’ve been searching forums for over an hr and can’t find anything. I’m following a tutorial for a snake game and I keep getting the bad geometry error. No spaces between numbers, im using x, not *, it’s lowercase… it’s driving me crazy!


from tkinter import *
import random

GAME_WIDTH = 700
GAME_HEIGHT = 700
SPEED = 50
SPACE_SIZE = 50
BODY_PARTS = 3
SNAKE_COLOR = "#00FF00"
FOOD_COLOR = "#FF0000"
BACKGROUND_COLOR = "#000000"


window = Tk()
window.title("Snake game")
window.resizable(False, False)

score = 0
direction = 'down'

label = Label(window, text="Score:{}".format(score), font=('consolas', 40))
label.pack

canvas = Canvas(window, bg=BACKGROUND_COLOR, height=GAME_HEIGHT, width=GAME_WIDTH)
canvas.pack

window.update()

window_width = window.winfo_width()
window_height = window.winfo_height()
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()

x = int(screen_width/2) - (window_width/2)
y = int(screen_height/2) - (window_height/2)

window.geometry(f"{window_width}x{window_height}+{x}+{y}")

window.mainloop()

I’m getting this error message and I can’t find what I’m doing wrong

Traceback (most recent call last):
File “/Users/macbook/PycharmProjects/PracticeMac/main.py”, line 56, in
window.geometry(f"{window_width}x{window_height}+{x}+{y}")
File “/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/tkinter/init.py”, line 2100, in wm_geometry
return self.tk.call(‘wm’, ‘geometry’, self._w, newGeometry)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
_tkinter.TclError: bad geometry specifier “200x200+740.0+425.0”

1 Like

Decimals aren’t permitted. You’re intifying the screen dimensions, but not the window dimensions.

I’d write it this way:

x = (screen_width - window_width) // 2
y = (screen_height - window_height) // 2

but other methods would work too.

when i run the code with either varaint, it opens the game window for 0.1 seconds and then closes itself, what could be the problem? Also, even after writing as you said it still gives me the same error.

1 Like

i have the same problem did you find the solution?

Hello,

I gave it a shot however not being familiar with this game. But I did follow the traceback and it referred to the geometry as the source of the error. Instead of using variables in place of the values,
I used actual numerical values as arguments to the function. As in:

##window.geometry(f"{window_width}x{window_height}+{x}+{y}")
##window.geometry('GAME_WIDTHxGAME_HEIGHT')
window.geometry('700x700')

You’ll notice that I have commented out the upper two that use variable names. Maybe with
variable names (text), the x multiplier gets interpreted as part of the variable name. Perhaps
this is why if numbers are used explicitly, there is a clear distinction between strings and numbers, and no geometry error is generated. I suspect that if the asterisk (*) could be used as a multiplier,
instead of the x text, this would not be an issue.

Give it a try.

The original code had:

x = int(screen_width/2) - (window_width/2)
y = int(screen_height/2) - (window_height/2)

The problem is that / returns a float, e.g. 740.0, but the geometry manager expects ints, e.g. 740.

In each of those calculations, the result of the first division is converted to an int, but the result of the second division is not, so the final sum is a float.

If you change those lines to:

x = int(screen_width/2) - int(window_width/2)
y = int(screen_height/2) - int(window_height/2)

it no longer complains.

Alternatively, you can use //, which returns an int:

x = screen_width // 2 - window_width // 2
y = screen_height // 2 - window_height // 2
1 Like

Why is there such a big difference in window size for the different implementations types?

  1. Via variables
  2. Via explicit integers
x = screen_width // 2 - window_width // 2
y = screen_height // 2 - window_height // 2 
##y = 350  # Uncomment to overwrite value and try same value as integer numerical way
print('x', x)
print('y', y)
print('window_width', window_width)
print('window_height',window_height)

window.geometry(f"{window_width}x{window_height}+{x}+{y}") # Via variable way
#window.geometry('GAME_WIDTHxGAME_HEIGHT')
#window.geometry('700x350') # Via the explicit integer numerical way

They’re being given different sizes.

This:

window.geometry(f"{window_width}x{window_height}+{x}+{y}")

is giving the size as 200x200 with the position +860+440 (calculated to centre the window on the screen), whereas this:

window.geometry('700x350')

is giving the size as 700x350 without a position.

1 Like

Ok, got it.

Thank you.