tkinter - 移动形状

时间:2016-04-28 17:48:34

标签: python tkinter drawing move

你如何用Python移动眼睛(当精神转向时,眼睛仍保持在右侧)。当我定义左侧运动时,我尝试使用c.move(eye,-10,0),我也尝试了隐藏状态的状态并创建了一个状态正常的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 )

1 个答案:

答案 0 :(得分:2)

你也可以使用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()