Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 使用tkinter(python 3)在画布中旋转形状_Python 3.x_Tkinter - Fatal编程技术网

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

Python 3.x 使用tkinter(python 3)在画布中旋转形状,python-3.x,tkinter,Python 3.x,Tkinter,我正在做一个魔方幻灯片游戏。基本上,4个方块可以移动的方向是右、左、上、下、顺时针、逆时针 我正在做的第一个按钮/动作是顺时针的。按钮使所有的方块顺时针移动一次,但我如何使它们每次按下时都能保持移动呢 顺时针移动是第一个功能,然后由顺时针按钮调用 从tkinter进口* 从tkinter导入ttk # --- Functions --- def clockwise_move(): canvas.coords(square1, 500, 2, 250, 250) canvas.c

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

我正在做的第一个按钮/动作是顺时针的。按钮使所有的方块顺时针移动一次,但我如何使它们每次按下时都能保持移动呢

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

从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

如果我正确理解了你想要得到什么,下面是我的解决方案

为了简化程序,我删除了所有按钮,除了顺时针方向的
按钮。我还简化了坐标

我的程序的思想是,对于“顺时针”旋转,你总是有相同的4个坐标,它们根据顺时针方向和逆时针方向传递到下一个正方形(即,一个变量总是递增,以进行一些算术运算,以便索引正确的坐标)。操作符
*
基本上将索引元组的每个项分配给四个参数。通过收听
事件,结合
after
函数,可以模拟在按下按钮时执行操作的想法,该函数安排对函数的调用

from tkinter import *
from tkinter import ttk


after_id = None

def clockwise_move(e):
    global after_id, c, n
    n += 1

    canvas.coords(square1, *c[n % len(c)])
    canvas.coords(square2, *c[(n + 1) % len(c)])
    canvas.coords(square3, *c[(n + 2) % len(c)])
    canvas.coords(square4, *c[(n + 3) % len(c)])

    after_id = e.widget.after(1000, clockwise_move, e)

def stop(e):
    e.widget.after_cancel(after_id)


main = Tk()

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

c = [(0, 0, 250, 250),
     (250, 0, 500, 250),
     (250, 250, 500, 500),
     (0, 250, 250, 500)]

n = len(c)

square1 = canvas.create_rectangle(*c[n % len(c)], fill="red")
square2 = canvas.create_rectangle(*c[(n + 1) % len(c)], fill="yellow")
square3 = canvas.create_rectangle(*c[(n + 2) % len(c)], fill="blue")
square4 = canvas.create_rectangle(*c[(n + 3) % len(c)], fill="green")

clockwise = ttk.Button(button_frame, text="Clockwise")
clockwise.grid(column=2, row=3)
clockwise.bind("<Button-1>", clockwise_move)
clockwise.bind("<Leave>", stop)

frame.pack()
canvas.grid(column=1, row=1)
button_frame.grid(column=2, row=1)

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

main.mainloop() 
从tkinter导入*
从tkinter导入ttk
在\u id=无之后
def顺时针移动(e):
全局后置id,c,n
n+=1
canvas.coords(平方1,*c[n%len(c)])
画布坐标(平方2,*c[(n+1)%len(c)])
画布坐标(平方3,*c[(n+2)%len(c)])
画布坐标(平方4,*c[(n+3)%len(c)])
after\u id=e.widget.after(1000,顺时针移动,e)
def停止(e):
e、 widget.after\u cancel(在\u id之后)
main=Tk()
帧=ttk帧(主)
按钮\帧=ttk.帧(帧)
画布=画布(框架,宽度=500,高度=500)
c=[(0,0,250,250),
(250, 0, 500, 250),
(250, 250, 500, 500),
(0, 250, 250, 500)]
n=len(c)
square1=画布。创建矩形(*c[n%len(c)],fill=“red”)
square2=画布。创建矩形(*c[(n+1)%len(c)],fill=“黄色”)
square3=画布。创建矩形(*c[(n+2)%len(c)],fill=“blue”)
square4=画布。创建矩形(*c[(n+3)%len(c)],fill=“绿色”)
顺时针=ttk.按钮(按钮\框架,文本=“顺时针”)
顺时针方向。网格(列=2,行=3)
顺时针绑定(“,顺时针移动)
顺时针方向绑定(“,停止)
frame.pack()
canvas.grid(列=1,行=1)
按钮框架网格(列=2,行=1)
对于按钮框中的子项。winfo子项()
配置子网格(padx=10,pady=20)
main.mainloop()
无论如何,通过查看代码,您将更好地了解正在发生的事情。如果没有,请在web上查看您尚未完全理解的概念;)


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

需要更多的思考。如何告诉程序要移动哪一行,并将其向右或向左移动。列也是如此。另一种方法是将正方形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()
从Tkinter导入*
导入ttk
从functools导入部分
def move_按钮(行,方向):
增量=-1##向左移动
如果方向==“R”:
增量=+1
此行=行记录[行]
##获取当前最左边的颜色
正方形\实例,颜色=此\行[0]
位置=颜色\u列表索引(颜色)
位置+=增量##下一种颜色
对于行中的每个方格列表,dict[行]:##列表=方格id和颜色
如果位置>=len(颜色列表)或位置<0:
位置=0
下一个颜色=颜色列表[位置]
打印“设置颜色”,下一个颜色
##使用新颜色更新画布和字典
canvas.itemconfig(每个正方形列表[0],填充=下一个颜色)
每个正方形列表[1]=下一个颜色
位置+=1##用于第二/右侧正方形
按顺序打印行
main=Tk()
主要标题(“魔方幻灯片”)
main.可调整大小(宽度=FALSE,高度=FALSE)
主要几何图形(“750x550”)
#---对象---
帧=ttk帧(主)
按钮\帧=ttk.帧(帧)
画布=画布(框架,宽度=500,高度=700)
#在字典中存储每个方块的颜色
#键=行-->列表列表,每个方块=[方块实例,颜色]
行_dict={}
颜色列表=[“白色”、“红色”、“蓝色”、“绿色”、“黄色”、“橙色”]
##2X2平方米
正方形=画布。创建矩形(2,2,249,249,fill=“红色”)
行[0]=[[方形,“红色”]]
正方形=画布。创建一个矩形(500,2250500,fill=“蓝色”)
行目录[0]。追加([方形,“蓝色”])
正方形=画布。创建_矩形(249249250,fill=“蓝色”)
行[1]=[[方形,“蓝色”]]
正方形=画布。创建_矩形(5004905250250,fill=“绿色”)
行目录[1]。追加([square,“green”])
打印“行记录”,行记录
#钮扣
ttk.按钮(按钮框,text=“顶行右”,命令=partial(移动按钮,0,“R”)).grid(列=2,行=0)
ttk.Button(按钮框,text=“左上行”,命令=部分(移动按钮,0,“L”))).grid(列=3,行=0)
ttk.Button(按钮框,text=“底部行右”,命令=部分(移动按钮,1,“R”)).grid(列=2,行=1)
ttk.Button(按钮框,text=“左下排”,命令=partial(移动按钮,1,“L”))).grid(列=3,行=1)
按钮(Button_frame,text=“Exit”,command=main.quit,bg=“orange”).grid(行=5,列=2,列span=2)
#框架网格
frame.grid(列=1,行=1)
canvas.grid(列=1,行=1)
button_frame.grid(列=2,行=0,行跨度=2)
#杂项设置
对于按钮框中的子项。winfo子项()
配置子网格(padx=10,pady=20)
main.mainloop()

这就是你想要做的吗

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()
导入系统 如果系统版本[0]<“3”: 从Tkinter进口* 其他: 从tkinter进口* 左角=[(0,0)、(200,0)、(200200)、(0200)] 颜色=[“红色”、“绿色”、“黄色”、“蓝色”] 判定元件