简体   繁体   English

为什么我的Tkinter窗口不会移动超过1个像素?

[英]Why won't my tkinter window move further than 1 pixel?

I am trying to make a tkinter window bounce around the screen like a ball in Pong, but it will only move 1 pixel in each direction and back again. 我正在尝试使Tkinter窗口像Pong中的球一样在屏幕上反弹,但它只会在每个方向上移动1像素,然后再次移回。 What have I done wrong? 我做错了什么?

main.py: main.py:

import tkinter
from time import sleep

window = tkinter.Tk()

screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()

window.title('VIRUS.EXE')

width, height = 300, 150
x, y, = 0, 0

x_vel = 1
y_vel = 1

while True:

    sleep(0.1)

    if x + width >= screen_width or x <= 0:
        x_vel = -x_vel

    if y + width >= screen_height or y <= 0:
        y_vel = -y_vel

    x += x_vel
    y += y_vel

    window_string = str(width) + 'x' + str(height) + '+' + str(x) + '+' + str(y)

    window.geometry(window_string)

    window.update()

window.mainloop()

Try with + instead of - +代替-

if x + width >= screen_width or x <= 0:
    x_vel = +x_vel

if y + width >= screen_height or y <= 0:
    y_vel = +y_vel

Edit: this may point you in the right direction: 编辑:这可能会指出正确的方向:

if x <= screen_width or x <= 0:
    x_vel = +x_vel
    x += x_vel

if y <= (screen_height -200)or y <= 0:
    y_vel = +y_vel
    y += y_vel

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM