我在製作一個Rubik的幻燈片遊戲。基本上,4個方格可以移動的動作是右,左,上,下,順時針,逆時針。用tkinter在畫布上旋轉形狀(python 3)
我正在做的第一個按鈕/移動是順時針。按鈕使所有的方塊順時針平移一次,但是我怎樣才能使它每次按下時都能保持移位?
順時針方向移動是第一個功能,然後按順時針方向按鈕調用。
從Tkinter的進口* 從Tkinter的進口TTK
# --- Functions ---
def clockwise_move():
canvas.coords(square1, 500, 2, 250, 250)
canvas.coords(square2, 500, 490, 249, 250)
canvas.coords(square3, 2, 2, 249, 249)
canvas.coords(square4, 2, 490, 249, 250)
# --- Setup ---
main = Tk()
main.title("Rubik's Slide")
main.resizable(width=FALSE, height=FALSE)
main.geometry("700x550")
# --- Objects ---
frame = ttk.Frame(main)
button_frame = ttk.Frame(frame)
canvas = Canvas(frame, width=500, height=700)
# squares
square1 = canvas.create_rectangle(2, 2, 249, 249, fill="red")
square2 = canvas.create_rectangle(500, 2, 250, 250, fill="white")
square3 = canvas.create_rectangle(2, 490, 249, 250, fill="blue")
square4 = canvas.create_rectangle(500, 490, 250, 250, fill="black")
# buttons
right = ttk.Button(button_frame, text="Right", command=clockwise_move).grid(column=2, row=1)
left = ttk.Button(button_frame, text="Left").grid(column=2, row=2)
up = ttk.Button(button_frame, text="Up").grid(column=3, row=1)
down = ttk.Button(button_frame, text="Down").grid(column=3, row=2)
clockwise = ttk.Button(button_frame, text="Clockwise").grid(column=2, row=3)
counter_clockwise = ttk.Button(button_frame, text="Counterclock").grid(column=3, row=3)
start = ttk.Button(button_frame, text="Start").grid(column=2, row=4)
reset = ttk.Button(button_frame, text="Reset").grid(column=3, row=4)
# frame grid
frame.grid(column=1, row=1)
canvas.grid(column=1, row=1)
button_frame.grid(column=2, row=1)
# misc settings
for child in button_frame.winfo_children():
child.grid_configure(padx=10, pady=20)
main.mainloop() # end of GUI
它爲什麼保持旋轉?我怎麼才能使它只是旋轉一次,而不是再次,直到我再次按下按鈕? – frankehhh
另外,我會如何爲不同的方向呢?我沒有看到運動正在進行 – frankehhh
@frankehhh根據你的問題,我認爲你真的想在按下按鈕時保持旋轉。你可以簡單地刪除這條線'after_id = e.widget.after(1000,clockwise_move,e)',你也可以刪除這條線'clockwise.bind(「,stop)'。另外,你不需要函數'stop'和變量'after_id'。 –
nbro