繁体   English   中英

tkinter-移动形状

[英]tkinter - move shapes

如何在Python中移动眼睛(当精神向左转时,眼睛将留在右侧)。 当我定义左移动时,我尝试了c.move(eye,-10,0),我也尝试过为眼睛隐藏状态,并创建了一个状态为normal的left_eye,但它不起作用。 谢谢。

from tkinter import *
HEIGHT = 500
WIDTH = 800
window = Tk()
window.title('Fetita arunca mingea')
c = Canvas (window, width = WIDTH, height = HEIGHT, bg = 'grey')
c.pack()
school= c.create_rectangle (200, 50,600, 150, fill = 'orange')
school_window1 = c.create_rectangle (250,80, 300, 100, fill= 'yellow')
school_window2 = c.create_rectangle (360,80, 420, 100, fill= 'yellow')
school_window2 = c.create_rectangle (500,80, 550, 100, fill= 'yellow')
body =c.create_polygon (0, 450, 250, 450, 100, 240,fill ='blue')
head = c.create_oval (50, 250, 150, 150, fill = 'ivory')
eye =c.create_oval (120 ,180, 125, 185,fill ='blue')
#eye_left =c.create_oval (75 ,180, 80, 185,fill ='yellow')
eye_left = c.move(eye,-10,0)
mouth = c.create_line (145, 220, 120, 220,fill = 'red')
#mouth_left = c.create_line (55, 220, 80, 220,fill = 'blue')
hand =c.create_line (110, 300, 200, 220, fill = 'black')
GIRL_step = 10
def move_girl(event):
   if event.keysym == 'Up':
        c.move (body, 0, - GIRL_step)
        c.move (head, 0, - GIRL_step)
        c.move (eye, 0, - GIRL_step)
        c.move (mouth, 0, - GIRL_step)
        c.move (hand, 0, - GIRL_step)
    elif event.keysym== 'Down':
        c.move (body, 0, GIRL_step)
        c.move (head, 0, GIRL_step)
        c.move (eye, 0, GIRL_step)
        c.move (mouth, 0, GIRL_step)
        c.move (hand, 0, GIRL_step)
    elif event.keysym== 'Left':
        c.move(eye,-10, 0)
        c.move (body, - GIRL_step, 0)
        c.move (head, - GIRL_step, 0)
        c.move (eye, - GIRL_step, 0)
        c.move (mouth, _GIRL_step, 0)
        c.move (hand, - GIRL_step, 0)
    elif event.keysym== 'Right':
        c.move (body, GIRL_step, 0)
        c.move (head, GIRL_step, 0)
        c.move (eye, GIRL_step, 0)
        c.move (mouth, GIRL_step, 0)
        c.move (hand, GIRL_step, 0)
c.bind_all ('<Key>', move_girl )

您也可以使用for()使事情变得更容易

    for body_part in [body, head, eye, mouth, hand]:
        c.move (body_part, - GIRL_step, 0)

和消除冗余代码的功能

from tkinter import *
HEIGHT = 500
WIDTH = 800
window = Tk()
window.title('Fetita arunca mingea')
c = Canvas (window, width = WIDTH, height = HEIGHT, bg = 'grey')
c.pack()
school= c.create_rectangle (200, 50,600, 150, fill = 'orange')
school_window1 = c.create_rectangle (250,80, 300, 100, fill= 'yellow')
school_window2 = c.create_rectangle (360,80, 420, 100, fill= 'yellow')
school_window2 = c.create_rectangle (500,80, 550, 100, fill= 'yellow')
body =c.create_polygon (0, 450, 250, 450, 100, 240,fill ='blue')
head = c.create_oval (50, 250, 150, 150, fill = 'ivory')
eye =c.create_oval (120 ,180, 125, 185,fill ='blue')
#eye_left =c.create_oval (75 ,180, 80, 185,fill ='yellow')
eye_left = c.move(eye,-10,0)
mouth = c.create_line (145, 220, 120, 220,fill = 'red')
#mouth_left = c.create_line (55, 220, 80, 220,fill = 'blue')
hand =c.create_line (110, 300, 200, 220, fill = 'black')
GIRL_step = 10

def move_common(move_x, move_y):
    for body_part in [body, head, eye, mouth, hand]:
        c.move (body_part, move_x, move_y)

def move_girl(event):
    if event.keysym == 'Up':
        move_common(0, -GIRL_step)
    elif event.keysym== 'Down':
        move_common(0, GIRL_ste)
    elif event.keysym== 'Left':
        c.move(eye,-10, 0)
        move_common(-GIRL_step, 0)
    elif event.keysym== 'Right':
        move_common(GIRL_step, 0)
c.bind_all ('<Key>', move_girl )

window.mainloop()

暂无
暂无

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

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