在栅格上移动圆时,坐标不会像图片那样更新

2024-09-28 01:30:25 发布

您现在位置:Python中文网/ 问答频道 /正文

该程序旨在创建可移动的圆,并显示正在移动的圆的坐标。问题是,当使用关键点向上或向下移动圆时,Xco值会增加10,尽管它基于的坐标不变

    from tkinter import *
    from tkinter import ttk
    
    root = Tk()
    
    canvas_width = 1000
    canvas_height = 1000
    w = canvas_width // 2
    h = canvas_height // 2
    canvas = Canvas(root, width=canvas_width, height=canvas_height)
    canvas.grid()
    
    r1 = canvas.create_oval(800, 800,  900,  900)
    r2 = canvas.create_oval(400, 400,  500,  500)
    r3 = canvas.create_oval(400, 400,  500,  500)
    t = canvas.create_text(20, 10, text="Hello")
    actionBtn = Button(root, text="Enter", width=10, height=1, command=quit).place(x=900, y=100) 
     
    def keypress(event): 
        
        gong = canvas.coords(r1)
        if all(i >= 0 for i in gong) and all(i <= 1000 for i in gong):
            print(canvas.coords(r1))
            
            x, y = 0, 0
            if event.char == "a" and gong[0] >= 10:
                x = -10
            if event.char == "d" and gong[2] <= 990:
                x = 10
            if event.char == "w" and gong[1] >= 10:
                y = -10
            if event.char == "s" and gong[3] <= 990:
                y = 10
            canvas.move(r1, x, y)
            Xco = str((gong[0]+gong[2])//2)
            structurelbl= Label(root, text= "X: "+Xco, width=10, height=1).place(x=900, y=100)
           # canvas.move(t, x, y)
                   
    root.bind("<Key>", keypress)
    root.mainloop()

Tags: andtextfromeventifcreaterootwidth

热门问题