简体   繁体   English

我正在制作蛇游戏并不断收到此错误 _tkinter.TclError: bad geometry specifier "704x772+288.0+14.0" 我该怎么办?

[英]I'm making a snake game and keep getting this error _tkinter.TclError: bad geometry specifier "704x772+288.0+14.0" what should I do?

I'm coding a snake game in python and I'm at the point where the food should show up on the screen but I am getting this error:我正在 python 中编写一个蛇游戏,我正处于食物应该出现在屏幕上的位置,但我收到了这个错误:

return self.tk.call('wm', 'geometry', self._w, newGeometry) _tkinter.TclError: bad geometry specifier "704x772+288.0+14.0" return self.tk.call('wm', 'geometry', self._w, newGeometry) _tkinter.TclError: bad geometry specifier "704x772+288.0+14.0"

import random
GAME_WIDTH = 700
GAME_HEIGHT = 700
SPEED = 50
SPACE_SIZE = 50
BODY_PARTS = 3
SNAKE_COLOUR = "#00FF00"
FOOD_COLOUR = "#FF0000"
BACKGROUND_COLOUR = "#000000"
class snake:
    pass
class food:
    def __init__(self):
        x = random.randint(0, (GAME_WIDTH /SPACE_SIZE-1)) * SPACE_SIZE
        y = random.randint(0, (GAME_HEIGHT / SPACE_SIZE - 1)) * SPACE_SIZE
        self.coordinates = [x,y]
        canvas.create_oval(x,y, x + SPACE_SIZE, y + SPACE_SIZE, fill=FOOD_COLOUR, tag="food")
def next_turn():
    pass
def change_direction(new_direction):
    pass
def check_collisions():
    pass
def game_over():
    pass
from tkinter import *
window = Tk()
window.title("Snake Game")
score = 0
direction = 'down'
label = Label(window, text="Score:{}".format(score), font=('consolas', 40))
label.pack()
canvas = Canvas(window, bg=BACKGROUND_COLOUR, height=GAME_HEIGHT, width=GAME_WIDTH)
canvas.pack()
window.update()
window_width: int = 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}")
snake = Snake()
food = Food()
window.mainloop()

Replace window_width/2 with something that produces an int, eg window_width//2 .window_width/2替换为产生 int 的东西,例如window_width//2 Do it likewise for window_height/2 .window_height/2做同样的事情。

This will remove the offending .0 from the geometry.这将从几何图形中删除有问题的.0

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Tkinter 错误(tkinter.TclError:错误的几何说明符“500 * 500) - Tkinter error(tkinter.TclError: bad geometry specifier "500*500) 获取 _tkinter.TclError:错误的几何说明符“400*400” - Getting _tkinter.TclError: bad geometry specifier "400*400" 我收到一条错误消息:_tkinter.TclError:错误的事件类型或键符“ButtonLeft” - I'm getting an error message: _tkinter.TclError: bad event type or keysym "ButtonLeft" 为什么我得到 tkinter.TclError: bitmap not defined 错误? - Why do I get tkinter.TclError: bitmap not defined error? 我在销毁按钮时收到错误 _tkinter.TclError: bad window path name “.!button” - I get the error _tkinter.TclError: bad window path name “.!button” when I destroy the button 我该如何解决错误_tkinter.TclError: couldn't open "sample.png": no such file or directory - How should I solve the error _tkinter.TclError: couldn't open "sample.png": no such file or directory 这是什么“_tkinter.TclError: bad option”。 谁能告诉我我做错了什么并告诉我如何解决这个问题? - What is this “_tkinter.TclError: bad option”. can anybody tell me what I've done wrong and tell me how to fix this? 当我在 python 中运行我的刽子手游戏时出现此错误:_tkinter.TclError:命令名称“.!canvas”无效 - i get this error when I am running my hangman game in python: _tkinter.TclError: invalid command name ".!canvas" 当我尝试运行它时,Tkinter 给了我一个 _tkinter.TclError: bad event type or keysym "button" - Tkinter is giving me a _tkinter.TclError: bad event type or keysym "button" when i try to run it 获取_tkinter.TclError:tkinter中的未知选项“-relief”错误 - getting _tkinter.TclError: unknown option "-relief" error in tkinter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM