以本地代码仓库为主,当要提交V1/V2作业时,再将代码在codeskulptor中打开,调试后提交。V3完全在本地开发。 在codeskulptor中调试时发现与本地冲突如下:
寻找知识源头
frame 添加Circle, Square, Triangle形状选择按钮,完成形状设置
def shape_circle():
def shape_triangle():
def shape_square():
frame.add_button("Circle", shape_circle)
frame.add_button("Triangle", shape_triangle)
frame.add_button("Square", shape_square)
通过鼠标位置,计算正方形、三角形的所有点坐标
def get_square_points(x, y, length):
def get_triangle_points(x, y, length):
def draw(canvas):
canvas.draw_circle(pos, 30, 2, "Black", "Red")
canvas.draw_polygon(get_triangle_points(pos, 60), 2, "Black", "Red")
canvas.draw_polygon(get_square_points(pos, 60), 2, "Black", "Red")
def mouse_click(pos):
frame.set_mouseclick_handler(mouse_click)
def color_rgb_reg(color):
return re.match(r'#[0-9a-f]{6}', color, re.IGNORECASE)
if (color in color_list) or color_rgb_reg(color):
cur_color = color
{"x":x, "y":y, "shape":shape, "color":color}
使用list存放所有点的信息,当鼠标点击时,在mouse_click处理函数中,向list.append(dictionary)
shape={"x":pos[0], "y":pos[1], "shape":cur_shape, "color":cur_color}
shape_list.append(shape)
绘制所有shape
def draw(canvas):
for shape in shape_list:
draw_shape(canvas,shape)
def draw_shape(canvas, shape):
if shape["shape"] == "Circle":
canvas.draw_circle([shape["x"], shape["y"]], 30, 2, "Black", shape["color"])
elif shape["shape"] == "Triangle":
canvas.draw_polygon(get_triangle_points(shape["x"], shape["y"], 60), 2, "Black", shape["color"])
elif shape["shape"] == "Square":
canvas.draw_polygon(get_square_points(shape["x"], shape["y"], 60), 2, "Black", shape["color"])
else:
canvas.draw_point([shape["x"], shape["y"]], shape["color"])
def set_interval(input_interval):
if play_mode:
show_message("Please don't Change Interval When Playing!")
return
global interval,message,timer
try:
interval = int(input_interval)
timer = simplegui.create_timer(interval, timer_handler)
except ValueError:
show_message("Please Input a Integer for Interval!")
interval_input.set_text("")
def play_stop():
global message,play_mode,play_index
if play_stop_btn.get_text() == "Play":
play_mode=True
play_stop_btn.set_text("Stop")
play_index = 0
timer.start()
elif play_stop_btn.get_text() == "Stop":
show_message("Play Stoped by User")
play_end()
play_index = -1
def timer_handler():
global play_index
if (play_index >= 0 and play_index < (len(shape_list) - 1)):
play_index += 1
else:
show_message("Play End")
play_end()
def get_picker_points(index):
column = index // COLOR_PICKER_ROW
row = index % COLOR_PICKER_ROW
pos = [COLOR_PICKER_WIDTH * column, COLOR_PICKER_WIDTH * row]
return get_square_points(pos[0], pos[1], COLOR_PICKER_WIDTH)
def get_color_picker_index(pos):
column = pos[0] // COLOR_PICKER_WIDTH
row = pos[1] // COLOR_PICKER_WIDTH
index = column * COLOR_PICKER_ROW + row
return index
def mouse_click(pos):
global cur_color
color_picker_index = get_color_picker_index(pos)
if color_picker_index < 0 or color_picker_index >= COLOR_PICKER_ROW * COLOR_PICKER_COLUMN :
shape={"x":pos[0], "y":pos[1], "shape":cur_shape, "color":cur_color}
shape_list.append(shape)
global cur_index,message,mouse_mode
cur_index=len(shape_list) - 1
messaage=""
mouse_mode="click"
else:
cur_color = color_list[color_picker_index]
确定使用tkFileDialog,file来实现此功能
先写一个测试程序,完成对文件的保存、关闭、写、读操作的学习
tkFileDialog.asksaveasfile(mode='w')
tkFileDialog.askopenfile(mode='r')
file.write(str)
file.readline(1024)
def save_draw():
save_file = tkFileDialog.asksaveasfile(mode='w')
for index in range(5):
save_file.write(str(index)+"\n")
save_file.flush()
save_file.close()
def open_draw():
global shape_list
end = False
open_file = tkFileDialog.askopenfile(mode='r')
while not end:
shape_str = open_file.readline(1024)
print(shape_str)
if shape_str == "":
end = True
else:
shape_list.append(shape_str)
open_file.close()
from Tkinter import *
root = Tk()
root.withdraw()
添加回放保存、与回放文件播放至最终代码中。
def save_draw():
save_file = tkFileDialog.asksaveasfile(mode='w')
save_file.write(str(shape_list))
save_file.flush()
save_file.close()
def open_draw():
end = False
shape_str = ""
open_file = tkFileDialog.askopenfile(mode='r')
while not end:
read_str = open_file.readline(1024)
print(shape_str)
if not read_str:
end = True
else:
shape_str += read_str
open_file.close()
print shape_str
shape_list = ast.literal_eval(shape_str)
play_stop()
Bug: