使用tkinter(python 3)在画布中旋转形状

时间:2016-03-01 23:30:07

标签: python-3.x tkinter

我正在制作一个魔方幻灯片游戏。基本上,4个方格可以进行的移动是向右,向左,向上,向下,顺时针,逆时针。

我正在做的第一个按钮/移动是顺时针方向。按钮使所有方块顺时针方向移动一次,但我如何制作它以便每按一次它们就可以保持移位?

顺时针移动是第一个功能,然后由顺时针按钮调用。

来自tkinter import *     来自tkinter import 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

3 个答案:

答案 0 :(得分:1)

如果我理解你想要获得什么,这是我的解决方案。

我删除了除.之外的所有按钮,以简化程序。我也简化了坐标。

我的程序的想法是,对于“顺时针”转弯,你总是有相同的4个坐标,根据顺时针方向传递到下一个方格,并根据计数器clockwise(即一个变量)它总是递增以做一些算术以索引正确的坐标)。运算符n基本上将索引元组的每个项分配给四个参数。通过使用*函数的组合来监听<Button-1><Leave>事件,可以模拟按下按钮时执行某些操作的想法,该函数会调度对函数的调用

after

无论如何,通过查看代码,您可以更好地了解正在发生的事情。如果没有,请在网上查看您尚未理解的概念;)

当您停止按下按钮时可能会有轻微的延迟,因为事件已经安排好了......

答案 1 :(得分:1)

需要更多的思考。如何告诉程序要移动哪一行,并向右或向左移动。列也是如此。另一种方法是将方形Id和该方块的颜色存储在字典中并推进颜色。这只会移动行,但上下相似。

from Tkinter import *
import ttk
from functools import partial

def move_button(row, direction):
    increment = -1  ## move left
    if direction == "R":
        increment = +1
    this_row=rows_dict[row]
    ## get current leftmost color
    square_instance, color=this_row[0]
    location=colors_list.index(color)
    location += increment  ## next color
    for each_square_list in rows_dict[row]: ## list=square_id and color
        if location >= len(colors_list) or location < 0:
            location = 0
        next_color=colors_list[location]
        print "Setting color to", next_color

        ## update the canvas and dictionary with the new color
        canvas.itemconfig(each_square_list[0], fill=next_color)
        each_square_list[1]=next_color
        location += 1  ## for 2nd/right square
    print rows_dict

main = Tk()

main.title("Rubik's Slide")
main.resizable(width=FALSE, height=FALSE)
main.geometry("750x550")

# --- Objects ---

frame = ttk.Frame(main)
button_frame = ttk.Frame(frame)
canvas = Canvas(frame, width=500, height=700)

# store color of each square in a dictionary
# key = row--> list of lists, each square=[ square instance, color]     
rows_dict={}
colors_list=["white", "red", "blue", "green", "yellow", "orange"]
## 2X2 square
square = canvas.create_rectangle(2, 2, 249, 249, fill="red")
rows_dict[0]=[[square, "red"]]
square = canvas.create_rectangle(500, 2, 250, 250, fill="blue")
rows_dict[0].append([square, "blue"])
square = canvas.create_rectangle(2, 490, 249, 250, fill="blue")
rows_dict[1]=[[square, "blue"]]
square = canvas.create_rectangle(500, 490, 250, 250, fill="green")
rows_dict[1].append([square, "green"])
print "rows_dict", rows_dict

# buttons
ttk.Button(button_frame, text="Top row right", command=partial(move_button, 0, "R")).grid(column=2, row=0)
ttk.Button(button_frame, text="Top row left", command=partial(move_button, 0, "L")).grid(column=3, row=0)
ttk.Button(button_frame, text="Bottom row right", command=partial(move_button, 1, "R")).grid(column=2, row=1)
ttk.Button(button_frame, text="Bottom row left", command=partial(move_button, 1, "L")).grid(column=3, row=1)
Button(button_frame, text="Exit", command=main.quit, bg="orange").grid(row=5, column=2, columnspan=2)
# frame grid
frame.grid(column=1, row=1)
canvas.grid(column=1, row=1)
button_frame.grid(column=2, row=0, rowspan=2)

# misc settings
for child in button_frame.winfo_children():
    child.grid_configure(padx=10, pady=20)

main.mainloop()

答案 2 :(得分:1)

这是你想要做的吗?

import sys
if sys.version[0] < '3':
    from Tkinter import *
else:
    from tkinter import *

left_corners = [(0,0), (200,0), (200,200), (0,200)]
colors = ['red', 'green', 'yellow', 'blue']

def rotate_clockwise():
    last_color = colors.pop()
    colors.insert(0, last_color)
    update()

def rotate_counterclockwise():
    first_color = colors.pop(0)
    colors.append(first_color)
    update()

def update():
    for square, color in zip(squares, colors):
        canvas.itemconfig(square, fill=color)

root = Tk()

canvas = Canvas(root, width=400, height=400)
canvas.grid()

squares = []
for left_corner, color in zip(left_corners, colors):
    x1, y1 = left_corner
    x2, y2 = x1+200, y1+200
    square = canvas.create_rectangle(x1, y1, x2, y2, fill=color)
    squares.append(square)

Button(root, text="Rotate 90 degrees clockwise",
       command=rotate_clockwise).grid()
Button(root, text="Rotate 90 degrees counter-clockwise",
       command=rotate_counterclockwise).grid()

root.title("Rotating test")
root.mainloop()