import time
import tkinter
from threading import Thread
root = tkinter.Tk()
root.title("My table1")
width, height = 300, 600
width_max, height_max = root.maxsize()
s_center = '%dx%d+%d+%d' % (width, height, (width_max - width) / 2, (height_max - height) / 2)
print(s_center)
root.geometry(s_center)
root.resizable(width=False, height=False)
c = tkinter.Canvas(root, width=width, height=400, bg='yellow')
c.place(x=0, y=0)
def draw_line():
c.create_line(10, 0, 10, 400, fill='red', width=2)
c.create_line(20, 0, 20, 400, fill='red', dash=(2, 1))
tkinter.Button(root, text='绘制线条', command=draw_line).place(x=10, y=420)
def draw_lines_blue():
for y in range(20, 400, 20):
c.create_line(0, y, width, y, fill='blue')
def draw_lines_red():
for x in range(20, 300, 20):
c.create_line(x, 0, x, 400, fill='red')
tkinter.Button(root, text="绘制蓝色线条", command=draw_lines_blue).place(x=95, y=420)
tkinter.Button(root, text="绘制红色线条", command=draw_lines_red).place(x=200, y=420)
c.create_rectangle(160, 160, 250, 350, fill="white", outline='Tan', width=1)
c.create_oval(10, 10, 150, 150, fill='SlateGray', outline='red', width=1)
c.create_oval(160, 160, 250, 350, fill='SlateGray', outline='red', width=1)
def draw_oval_line():
c.create_line(10, 0, 10, 400, fill='DarkTurquoise')
c.create_line(0, 10, 300, 10, fill='DarkTurquoise')
c.create_line(150, 0, 150, 400, fill='DarkTurquoise')
c.create_line(0, 150, 300, 150, fill='DarkTurquoise')
tkinter.Button(root, text='绘制4条切线', command=draw_oval_line).place(x=10, y=470)
def del_all():
c.delete(tkinter.ALL)
tkinter.Button(root, text='删除画布对象', command=del_all).place(x=95, y=470)
c.create_polygon(150, 150, 100, 200, 100, 250, 200, 250, 200, 200
, fill='red')
c.create_text(180, 40, text='文本内容', font=('宋体', 16)
, anchor=tkinter.W, fill='red')
c.create_arc(10, 250, 120, 400)
c.create_arc(10, 250, 120, 400, extent=359)
c.create_arc(10, 250, 120, 400, start=-90)
def fun():
Thread(target=run_arc).start()
def run_arc():
for i in range(-1, -360, -1):
t = c.create_arc(10, 250, 120, 400, start=i, fill='white')
time.sleep(0.05)
c.delete(t)
tkinter.Button(root, text='弧形走动效果', command=fun).place(x=200, y=470)
root.mainloop()
import time
import tkinter
from threading import Thread
root = tkinter.Tk()
root.title("My table1")
width, height = 300, 600
width_max, height_max = root.maxsize()
s_center = '%dx%d+%d+%d' % (width, height, (width_max - width) / 2, (height_max - height) / 2)
print(s_center)
root.geometry(s_center)
root.resizable(width=False, height=False)
c = tkinter.Canvas(root, width=width, height=400, bg='yellow')
c.place(x=0, y=0)
rect = c.create_rectangle(50, 50, 160, 160, fill='white', outline='blue',
width=2)
print('画布上的新建对象', rect)
c.create_line(0, 50, 300, 50, fill='red', width=2)
c.create_line(0, 160, 300, 160, fill='red', width=2)
c.create_line(50, 0, 50, 400, fill='red', width=2)
c.create_line(160, 0, 160, 400, fill='red', width=2)
def fun():
Thread(target=move_rect).start()
def move_rect():
for i in range(0, 25):
c.move(rect, 10, 0)
time.sleep(0.2)
def coords_rect():
c.coords(rect, 10, 30, 150, 250)
def del_rect():
c.delete(tkinter.ALL)
def config_rect():
c.itemconfig(rect, fill='Tan', outline='red', width=4)
var = tkinter.StringVar()
var.set('移动方法')
tkinter.Button(root, textvariable=var, command=fun).place(x=10, y=420)
root.mainloop()