Python GUI之tkinter详解
作者:繁星伴晚安 时间:2021-09-11 06:36:01
展示
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)
#删除tags = "text"
# canvas.delete("text")
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) # 放置水平滚动条在最下侧,占满X轴
hbar.config(command=canvas.xview)# 设置水平滚动条的函数与画布的X轴滚动条事件绑定
vbar = Scrollbar(frame,orient=VERTICAL) # 定义垂直滚动条
vbar.pack(side=RIGHT, fill=Y) # 放置垂直滚动条在最右侧,占满Y轴
vbar.config(command=canvas.yview)# 设置垂直滚动条的函数与画布的Y轴滚动条事件绑定
canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set) # 设置画布的X,Y轴滚动条函数与垂直滚动条绑定
canvas.pack(expand=True, fill=BOTH)
frame.pack(expand=True, fill=BOTH)
#删除tags = "text"
# canvas.delete("text")
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) # 放置水平滚动条在最下侧,占满X轴
hbar.config(command=canvas.xview)# 设置水平滚动条的函数与画布的X轴滚动条事件绑定
vbar = Scrollbar(frame,orient=VERTICAL) # 定义垂直滚动条
vbar.pack(side=RIGHT, fill=Y) # 放置垂直滚动条在最右侧,占满Y轴
vbar.config(command=canvas.yview)# 设置垂直滚动条的函数与画布的Y轴滚动条事件绑定
canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set) # 设置画布的X,Y轴滚动条函数与垂直滚动条绑定
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__':
#删除tags = "text"
# canvas.delete("text")
t = threading.Thread(target=run)
t.start()
win.mainloop()
暂定版本
个人知识还是不够,里面还有很多小bug,容我再思考思考
import time
from tkinter import *
import random
import sys
import threading
class TSP(object):
def __init__(self,root,cities:list[list]):
self.__root = root
self.__root.geometry('800x600')
self.__root.title("TSP蚁群算法(n:初始化 e:开始搜索 s:停止搜索 c:继续搜索 q:退出程序)")
self.__frame = Frame(root)
self.__canvas = Canvas(self.__frame,bg='#EBEBEB',width=800,height=800,scrollregion=(0,0,2000,1200))
hbar = Scrollbar(self.__frame, orient=HORIZONTAL) # 定义水平滚动条
hbar.pack(side=BOTTOM, fill=X) # 放置水平滚动条在最下侧,占满X轴
hbar.config(command=self.__canvas.xview) # 设置水平滚动条的函数与画布的X轴滚动条事件绑定
vbar = Scrollbar(self.__frame, orient=VERTICAL) # 定义垂直滚动条
vbar.pack(side=RIGHT, fill=Y) # 放置垂直滚动条在最右侧,占满Y轴
vbar.config(command=self.__canvas.yview) # 设置垂直滚动条的函数与画布的Y轴滚动条事件绑定
self.__canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set) # 设置画布的X,Y轴滚动条函数与垂直滚动条绑定
self.__canvas.pack(expand=True, fill=BOTH)
self.__frame.pack(expand=True, fill=BOTH)
self.setCity(cities)
self.__lock = threading.RLock() # 线程锁
self.__bindEvents()
self.__new()
def setCity(self,cities:list[list]):
self.__cities = cities
def sefPathsAndValues(self,paths:[list],values:list):
self.__paths = paths
self.__values = values
# 按键响应程序
def __bindEvents(self):
self.__root.bind("q", self.__quite) # 退出程序
self.__root.bind("n", self.__new) # 初始化
self.__root.bind("e", self.__search_path) # 开始搜索
self.__root.bind("s", self.__stop) # 停止搜索
self.__root.bind("c", self.__continue) # 继续搜索
#初始化
def __new(self,event=None):
self.__lock.acquire() #上锁
self.__running = False #标志位
self.__lock.release() #释放锁
for x, y in self.__cities:
self.__canvas.create_oval(x - 5, y - 5, x + 5, y + 5, fill='#ff0000', outline="#000000", tags="node")
self.__canvas.create_text(x, y - 10, text='(' + str(x) + ',' + str(y) + ')', fill='black', tags="text")
def __quite(self,event=None):
self.__lock.acquire() # 上锁
self.__running = False # 标志位
self.__lock.release() # 释放锁
self.__root.destroy()
print(u"\n程序已退出...")
sys.exit()
# 停止搜索
def __stop(self,event=None):
self.__lock.acquire() # 上锁
self.__running = False # 标志位
self.__lock.release() # 释放锁
def __line(self,path:list):
self.__canvas.delete("line")
i = 0
while self.__running:
if i == len(self.__cities) -1:
break
p1, p2 = self.__cities[path[i]], self.__cities[path[i + 1]]
self.__canvas.create_line(p1, p2, fill='#000000', tags="line",
arrow=LAST, width=1)
self.__canvas.update()
self.__canvas.after(500)
i = i+1
# 开始搜索
def __search_path(self,event=None):
self.__lock.acquire() # 上锁
self.__running = True # 标志位
self.__lock.release() # 释放锁
while self.__running:
x = random.randint(10,100)
label = Label(self.__canvas,text="最佳路径总距离:"+str(x)).place(x=10,y=30)
self.__canvas.update()
path = [x for x in range(len(self.__cities))]
random.shuffle(path)
self.__line(path)
def __continue(self,event=None):
self.__lock.acquire() # 上锁
self.__running = True # 标志位
self.__lock.release() # 释放锁
def mainloop(self):
self.__root.mainloop()
if __name__ == '__main__':
tsp = TSP(Tk(),cities)
tsp.mainloop()
来源:https://blog.csdn.net/weixin_48180029/article/details/120638034
标签:Python,GUI,tkinter
0
投稿
猜你喜欢
Swift 3.0在集合类数据结构上的一些新变化总结
2023-10-19 02:35:47
朴素贝叶斯分类算法原理与Python实现与使用方法案例
2022-03-11 10:59:04
带你深入了解Access数据库的4种安全方式
2008-11-28 14:34:00
Django celery实现异步任务操作,并在后台运行(守护进程)
2021-11-12 16:26:21
Perl学习笔记之文件操作
2022-04-01 21:48:18
一文深入了解Python中的继承知识点
2023-01-16 19:57:16
如何查看SQLSERVER中某个查询用了多少TempDB空间
2024-01-16 06:40:21
pytorch使用voc分割数据集训练FCN流程讲解
2023-08-29 20:30:37
MySQL中使用replace、regexp进行正则表达式替换的用法分析
2024-01-15 13:34:02
对pandas replace函数的使用方法小结
2022-07-04 15:20:24
实现 Python 脚本生成命令行
2022-08-26 01:05:56
解决python ogr shp字段写入中文乱码的问题
2023-07-05 00:40:17
Javascript技术栈中的四种依赖注入小结
2024-04-18 10:54:08
php实现将数组或对象写入到文件的方法小结【三种方法】
2023-11-19 05:08:04
perl比较两个文件字符串的实例代码
2023-02-15 15:40:18
python time时间库详解
2023-10-09 03:20:57
python单元测试unittest实例详解
2023-10-16 08:26:49
javascript实现页面的实时时钟显示示例
2024-04-10 10:49:07
解决MSSQL2005远程连接sql2000非默认端口数据库的问题
2024-01-28 09:41:10
Python使用Selenium、PhantomJS爬取动态渲染页面
2023-12-20 22:24:55