python Tkinter的图片刷新实例
作者:alansss 发布时间:2023-10-31 04:32:24
标签:python,Tkinter,图片,刷新
调用python自带的GUI制作库
一开始想用Tkinter制作GUI的,网上说是python自带的,结果输入:
import tkinter
后,显示:
_ImportError: No module named tkinter_
以为是没有安装,还利用apt-get install 命令安装了一堆东西,安装完了发现还是没有用。(⊙﹏⊙)b
后来看到如果是用的python2.7的话,需要输入
import Tkinter
然后就可以用了。
显示连续刷新的图片
开始用的TK的Label功能来显示图片,需要等到调用mainloop()后才会显示图片,没找到可以刷新图像的方法;后来采用Canvas,才找到了可以不用等到mainloop()就可以显示图片的方法,代码如下:
from Tkinter import *
from PIL import Image, ImageTk
import time
import os
import cv2
num=0
tk=Tk()
canvas=Canvas(tk,width=500,height=500,bg = 'white')
while num<7:
num +=1
filename = str(num) + '.jpg'
if os.path.isfile(filename):
img1 = cv2.imread(filename)
im1 = Image.fromarray(cv2.cvtColor(img1,cv2.COLOR_BGR2RGB))
img = ImageTk.PhotoImage(image = im1)
#img = ImageTk.PhotoImage(file = filename)
itext = canvas.create_image((250,150),image = img)
canvas.pack()
tk.update()
tk.after(1000)
tk.mainloop()
再后来发现用Label也可以实现图片的刷新,关键在于是否加了:
tk.updata()
使用Label的程序如下,其中的.grid()用于设置显示的位置:
#coding=utf-8
import Tkinter as tk
from PIL import Image, ImageTk
import cv2
import os
import time
def btnHelloClicked():
labelHello.config(text = "Hello Tkinter!")
def resize(w,h,w_box,h_box,im):
f1 = 1.0*w_box/w
f2 = 1.0*h_box/h
factor = min([f1, f2])
width = int(w*factor)
height = int(h*factor)
return im.resize((width,height),Image.ANTIALIAS)
top = tk.Tk()
#-------------- image 1 --------------
for N in range(1,10):
filename = str(N) + '.jpg'
if os.path.isfile(filename):
#top = tk.Toplevel()#tk.Tk()
top.title("test the net.")
#string
labelHello = tk.Label(top,text = str(N),height = 5,width = 20,fg = "blue")
labelHello.grid(row = 0,column = 1)
img1 = cv2.imread(filename)
im1 = Image.fromarray(cv2.cvtColor(img1,cv2.COLOR_BGR2RGB))
#---resize the image to w_box*h_box
w_box = 500
h_box = 450
w,h = im1.size
im_resized1 = resize(w,h,w_box,h_box,im1)
bm1 = ImageTk.PhotoImage(image = im_resized1)
label1 = tk.Label(top,image = bm1,width = w_box,height = h_box)
label1.grid(row = 1,column = 0)
top.update()
top.after(1000)
top.mainloop()
其中尝试了将 tk.Tk()放在循环内部,但是运行到第二个循环的时候,会报错:
_tkinter.TclError: image "pyimage2" doesn't exist
需要将tk.Tk替换为tk.Toplevel(),但是每个循环都会新出现两个面板。
来源:https://blog.csdn.net/alansss/article/details/85011293
0
投稿
猜你喜欢
- 视频读取视频读取,主要利用VideoCapture类下的方法打开视频并获取视频中的帧,具体示例如下:#include<iostream
- 本文实例讲述了Python实现的最近最少使用算法。分享给大家供大家参考。具体如下:# lrucache.py -- a simple LRU
- 前言去年在做golangserver的时候,内部比较头疼的就是在线服务发布的时候,大量用户的请求在发布时候会被重连,在那时候也想了n多的方法
- 本文实例为大家分享了python dlib人脸识别的具体代码,供大家参考,具体内容如下import matplotlib.pyplot as
- Django 是 Python 编程语言驱动的一个开源模型-视图-控制器(MVC)风格的 Web 应用程序框架。它是Python API开发
- 主要就是了解一下pytorch中的使用layernorm这种归一化之后的数据变化,以及数据使用relu,prelu,leakyrelu之后的
- pymysql写入中文直接写入中文会出现错误。根据网上的方法,设置my.ini不成功sql = "INSERT INTO comp
- 本来想着做一个将图片识别为文字的小功能,本想到Google上面第一页全是各种收费平台的广告。这些平台提供的基本都是让我们通过调用相关的三方接
- PIL:使用python自带图像处理库读取出来的图片格式numpy:使用python-opencv库读取出来的图片格式tensor:pyto
- UserAgent = Trim(Lcase(Request.Serve
- i前端:nput_test.html<!DOCTYPE html><html><head lang="
- 为什么ASP.NET Core采用Main方法?需要记住的最重要的一点是,ASP.NET Core Web 应用程序最初作为控制台应用程序启
- 很多人不知道SQL语句在SQL SERVER中是如何执行的,他们担心自己所写的SQL语句会被SQL SERVER误解。比如:select *
- 目录什么是 PyMySQL?PyMySQL 安装数据库连接实例:创建数据库表数据库插入操作数据库查询操作数据库更新操作删除操作执行事务错误处
- 一步步教你怎么用Python画雪人,进一步熟悉Python的基础画图操作,废话不多说,上代码。希望您给个关注给个赞,也算对我们的支持了。cl
- javascript/js的ajax的GET请求代码如下所示:<script type="text/javascript&q
- 一般常规的我们保存数据为dict类型时,系统会自动帮我们排序;但有时我们想按照输入顺序的key:value保存到dict中,而不想要改变顺序
- 利用三元运算符实现登录注册效果切换,供大家参考,具体内容如下直接上代码wxml:<view class='top'&g
- 目录process模块1、在python中启动一个子进程2、给子进程传递参数3、同时开多个子进程4、join的用法5、多进程之间的数据是否隔
- 第一:mysql服务的启动和停止net stop mysqlnet start mysql第二:登陆mysql –u用户名 [–h主机名或者