Tkinter canvas-画布参数、删除组件、添加垂直滚动条
蚁群算法(Ant Colony Optimization)
展示
import tkinter
if __name__ == '__main__':
win = tkinter.Tk()
win.title("我的世界")
win.geometry('800x600')
canvas = tkinter.Canvas(win,bg='#EBEBEB',width=800,height=1200,xscrollincrement = 1,
yscrollincrement = 1)
x0,y0 = 100,100
oval = canvas.create_oval(x0-5,y0-5,x0+5,y0+5,fill='#ff0000',outline="#000000",tags="node")
canvas.create_text(x0,y0-10,text='('+str(x0) +','+str(y0)+')',fill='black',tags="text")
p1 = (x0,y0)
x0, y0 = 120,170
oval = canvas.create_oval(x0 - 5, y0 - 5, x0 + 5, y0 + 5, fill='#ff0000', outline="#000000", tags="node")
canvas.create_text(x0, y0 - 10, text='(' + str(x0) + ',' + str(y0) + ')', fill='black',tags="text")
p2 = (x0, y0)
canvas.create_line(p1, p2, fill='#000000', tags="line")
canvas.pack(expand = tkinter.YES, fill = tkinter.BOTH)
win.mainloop()
from tkinter import *
import random
if __name__ == '__main__':
win = Tk()
win.title("我的世界")
win.geometry('800x600')
frame = Frame(win)
canvas = Canvas(frame,bg='#EBEBEB',width=800,height=800,scrollregion=(0,0,2000,1200))
pos = [[565.0,575.0],[25.0,185.0],[345.0,750.0],[945.0,685.0],[845.0,655.0],
[880.0,660.0],[25.0,230.0],[525.0,1000.0],[580.0,1175.0],[650.0,1130.0],
[1605.0,620.0],[1220.0,580.0],[1465.0,200.0],[1530.0, 5.0],[845.0,680.0],
[725.0,370.0],[145.0,665.0],[415.0,635.0],[510.0,875.0],[560.0,365.0],
[300.0,465.0],[520.0,585.0],[480.0,415.0],[835.0,625.0],[975.0,580.0],
[1215.0,245.0],[1320.0,315.0],[1250.0,400.0],[660.0,180.0],[410.0,250.0],
[420.0,555.0],[575.0,665.0],[1150.0,1160.0],[700.0,580.0],[685.0,595.0],
[685.0,610.0],[770.0,610.0],[795.0,645.0],[720.0,635.0],[760.0,650.0],
[475.0,960.0],[95.0,260.0],[875.0,920.0],[700.0,500.0],[555.0,815.0],
[830.0,485.0],[1170.0, 65.0],[830.0,610.0],[605.0,625.0],[595.0,360.0],
[1340.0,725.0],[1740.0,245.0]]
for x,y in pos:
canvas.create_oval(x - 5, y - 5, x + 5, y + 5, fill='#ff0000', outline="#000000", tags="node")
canvas.create_text(x, y - 10, text='(' + str(x) + ',' + str(y) + ')', fill='black', tags="text")
length = len(pos)
path = [x for x in range(length)]
random.shuffle(path)
for i in range(length-1):
canvas.create_line(pos[i], pos[i+1], fill='#000000', tags="line")
hbar = Scrollbar(frame,orient=HORIZONTAL)
hbar.pack(side=BOTTOM, fill=X)
hbar.config(command=canvas.xview)
vbar = Scrollbar(frame,orient=VERTICAL)
vbar.pack(side=RIGHT, fill=Y)
vbar.config(command=canvas.yview)
canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)
canvas.pack(expand=True, fill=BOTH)
frame.pack(expand=True, fill=BOTH)
win.mainloop()
多线程
import time
from tkinter import *
import random
import threading
pos = [[565.0,575.0],[25.0,185.0],[345.0,750.0],[945.0,685.0],[845.0,655.0],
[880.0,660.0],[25.0,230.0],[525.0,1000.0],[580.0,1175.0],[650.0,1130.0],
[1605.0,620.0],[1220.0,580.0],[1465.0,200.0],[1530.0, 5.0],[845.0,680.0],
[725.0,370.0],[145.0,665.0],[415.0,635.0],[510.0,875.0],[560.0,365.0],
[300.0,465.0],[520.0,585.0],[480.0,415.0],[835.0,625.0],[975.0,580.0],
[1215.0,245.0],[1320.0,315.0],[1250.0,400.0],[660.0,180.0],[410.0,250.0],
[420.0,555.0],[575.0,665.0],[1150.0,1160.0],[700.0,580.0],[685.0,595.0],
[685.0,610.0],[770.0,610.0],[795.0,645.0],[720.0,635.0],[760.0,650.0],
[475.0,960.0],[95.0,260.0],[875.0,920.0],[700.0,500.0],[555.0,815.0],
[830.0,485.0],[1170.0, 65.0],[830.0,610.0],[605.0,625.0],[595.0,360.0],
[1340.0,725.0],[1740.0,245.0]]
win = Tk()
win.title("我的世界")
win.geometry('800x600')
frame = Frame(win)
canvas = Canvas(frame,bg='#EBEBEB',width=800,height=800,scrollregion=(0,0,2000,1200))
for x,y in pos:
canvas.create_oval(x - 5, y - 5, x + 5, y + 5, fill='#ff0000', outline="#000000", tags="node")
canvas.create_text(x, y - 10, text='(' + str(x) + ',' + str(y) + ')', fill='black', tags="text")
hbar = Scrollbar(frame,orient=HORIZONTAL)
hbar.pack(side=BOTTOM, fill=X)
hbar.config(command=canvas.xview)
vbar = Scrollbar(frame,orient=VERTICAL)
vbar.pack(side=RIGHT, fill=Y)
vbar.config(command=canvas.yview)
canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)
canvas.pack(expand=True, fill=BOTH)
frame.pack(expand=True, fill=BOTH)
def run():
length = len(pos)
path = [x for x in range(length)]
random.shuffle(path)
time.sleep(1)
for i in range(length - 1):
canvas.create_line(pos[i], pos[i + 1], fill='#000000', tags="line", arrow=LAST, width=1)
time.sleep(1)
if __name__ == '__main__':
t = threading.Thread(target=run)
t.start()
win.mainloop()
|