Python tkinter的简单使用,在绘布上播放GIF和图片


文章目录

  • Python tkinter的简单使用,在绘布上播放GIF和图片
  • 前言
  • 一、tkinter 的简单组件以及pack(),grid方法、place方法。
    • 按钮的使用Button, Label的使用。
      • 导入语句
      • 声明Button和Label
    • 在tkinter的绘布上放映图片和GIF的核心代码。
  • 二、核心代码
    • 1.主窗体
      • 其他代码文件我只放一个做示例。
      • 主函数
  • 总结

前言

本文章代码是具有具有python基础上写着来学习的,本文的代码可能只涉及到下面目录所提及的部分内容,若有需要的话,可以查看,并且指出一起学习进步。

提示:本文章是在学习过程中留下的一些笔记。本文章的一些核心代码是在网上参考别人的,我是做了一下改动。


以下是本篇文章正文内容

一、tkinter 的简单组件以及pack(),grid方法、place方法。

按钮的使用Button, Label的使用。

导入语句

from tkinter import *

声明Button和Label

btn1 = Button(self.root, text="R", width=15, font='宋体 12 bold', relief='raised', bg='white', fg='blue',command=choose_R.show_R)
btn1.place(x=200, y=20)
label2 = Label(root1,text = str,wraplength= 180)
label2.pack()

在tkinter的绘布上放映图片和GIF的核心代码。

这个放映是在弹出新窗口上播放的GIF。主要是win = Toplevel();
要是在原界面上播放也差不多。本文章就不展示源代码文件

代码

 def show_R(self):win = Toplevel()canvas = Canvas(win, width=1380, height=800, bg='White')canvas.pack(expand=True)# 最大帧数numIdx = 28             frames = [PhotoImage(file='D:/Python_code/tk/final/gif/R_fianl.gif',format='gif -index %i' % (i)) for i in range(numIdx)]# 填充帧内容到framesdef update(idx):    # 定时器函数frame = frames[idx]idx += 1         # 下一帧的序号:canvas.create_image(15, 15, anchor=NW, image=frame)# label.configure(image = frame) # 显示label方法当前帧的图片# 0.1秒(500毫秒)之后继续执行定时器函数(update)if self.isplaying:self.root.after(self.time, update, idx % numIdx)else:self.stop_idx = idxwin.after(0, update(0))

二、核心代码

1.主窗体

from tkinter import *
from tkinter import messagebox
from show_R import R
from tkinter.ttk import Separator
from show_lw import lw
from show_sw import sw
from show_beq import beq
from show_j import Jumpclass test_menu:def __init__(self, root, time):self.title = ' 大勇'self.root = rootself.time = timeself.isplaying = Falseself.stop_idx = 0self.show_aboutself.minimumself.show_testself.text_exitdef get_time(self):return self.timedef minimum(self):self.root.iconify()# 让窗口最小化def text_exit(self):# 弹出式窗口页面ret = messagebox.askquestion("EXIT", '确定要离开吗?')if ret == 'yes':self.root.destroy()def show_about(self):# 菜单内的那个关于root1 = Toplevel()root1.title("关于")screen_width = self.root.winfo_screenwidth()screen_height = self.root.winfo_screenheight()x= 300y =240w = (screen_width - x) / 2h = (screen_height -y) /2root1.geometry('%dx%d+%d+%d'%(x, y, w, h))label1 = Label(root1, text='关于模拟数据通路', fg='black', bg='white')label1.pack()sep = Separator(root1,orient = HORIZONTAL)sep.pack(fill = X,pady = 5)str ="此程序只能做到初步模拟数据通路的流动在指令模拟方面只是绘制了部分指令,R型的指令并没有细分,只是画了很初步的流法,流动的效果也不是很好,一些指令线路的流动也没有画出来,流动的指令只是主要部分的流动,实际情况会比此程序演示的流动还要复杂许多本程序只是参照课本"label2 = Label(root1,text = str,wraplength= 180)label2.pack()label3 = Label(root1, text='制作时间:2021年6月1日',fg='black', bg='white')label3.pack()root1.mainloop()def show_test(self):def rate( current_time):ret =messagebox.askokcancel('更改速度','确定更改速度或者取消?')if ret == True:self.time -= current_timechoose_R.update_time(self.time)choose_lw.update_time(self.time)choose_sw.update_time(self.time)choose_beq.update_time(self.time)def show_popupmenu(event):popmenu.post(event.x_root, event.y_root)# 获取窗口的大小screen_width = self.root.winfo_screenwidth()screen_height = self.root.winfo_screenheight()self.root.title("模拟数据通路")# root.iconbitmap('mystar.ico')# 下面为设置窗口的大小,x为宽,y为高,w,h为离左边的位置x = 720y = 480w = (screen_width - x) / 2h = (screen_height -y) /2self.root.geometry('%dx%d+%d+%d'%(x, y, w, h))# 声明各个类的对象time = self.get_time()choose_R= R(self.isplaying,self.root,time) choose_lw= lw(self.isplaying,self.root,time)  choose_sw = sw(self.isplaying,self.root,time)choose_beq = beq(self.isplaying,self.root,time)choose_j = Jump(self.isplaying,self.root,time)btn1 = Button(self.root, text="R", width=15, font='宋体 12 bold', relief='raised', bg='white', fg='blue',command=choose_R.show_R)btn2 = Button(self.root, text="lw", width=15, font='宋体 12 bold', relief='raised', bg='white', fg='blue',command=choose_lw.show_lw)btn3 = Button(self.root, text="sw", width=15, font='宋体 12 bold', relief='raised', bg='white', fg='blue',command=choose_sw.show_sw)btn4 = Button(self.root, text='beq', width=15, font='宋体 12 bold', relief='raised', bg='white', fg='blue',command=choose_beq.show_beq)btn5 = Button(self.root,text ='jump',width = 15,font = '宋体 12 bold',relief ='raised',bg= 'white',fg = 'blue',command = choose_j.show_jump)menubar = Menu(self.root)# 建立菜单类对象,并将此菜单类别命名为Filefilemenu = Menu(menubar)menubar.add_cascade(label='菜单', font='宋体 36 bold', menu =filemenu)# 在FIle菜单中建立子菜单列表findmenu = Menu(filemenu,tearoff=False)findmenu.add_command(label = '1.0x',command = lambda: rate(0))findmenu.add_command(label = '1.25x',command = lambda:rate(100))findmenu.add_command(label = '1.5x',command = lambda:rate(100))findmenu.add_command(label = '2.0x',command = lambda:rate(200))findmenu.add_command(label = '0.75x',command = lambda:rate(-200))findmenu.add_command(label = '0.5x',command = lambda:rate(-400))filemenu.add_cascade(label='速度 ', menu = findmenu,command=rate)filemenu.add_command(label='关于', command=self.show_about)filemenu.add_command(label='退出', command=self.text_exit)# 显示菜单对象self.root.config(menu=menubar)popmenu = Menu(self.root, tearoff=False)findmenu1 = Menu(popmenu,tearoff=False)# 建立弹出式菜单对象findmenu1.add_command(label = '1.0x',command = lambda: rate(0))findmenu1.add_command(label = '1.25x',command = lambda:rate(100))findmenu1.add_command(label = '1.5x',command = lambda:rate(100))findmenu1.add_command(label = '2.0x',command = lambda:rate(200))findmenu1.add_command(label = '0.75x',command = lambda:rate(-200))findmenu1.add_command(label = '0.5x',command = lambda:rate(-400))popmenu.add_cascade(label = '速度',menu = findmenu1)popmenu.add_command(label='最小化',command=self.minimum)popmenu.add_command(label='退出',command=self.text_exit)self.root.bind('<Button-3>', show_popupmenu)btn1.place(x=200, y=20)btn2.place(x=200, y=80)btn3.place(x=200, y=140)btn4.place(x=200, y=200)btn5.place(x= 200,y=260)# 程序图标self.root.iconbitmap('D:/Python_code/tk/final/gif/1.ico')self.root.mainloop()

其他代码文件我只放一个做示例。

from tkinter import *
import time
class R:def __init__(self, isplaying,root,time):self.isplaying = isplayingself.root = rootself.time = timedef update_time(self,current_time):if current_time != self.time:self.time = current_timedef show_R(self):"""下面为在画布上展示R型指令的设计"""win = Toplevel()canvas = Canvas(win, width=1380, height=800, bg='White')canvas.pack(expand=True)# 最大帧数numIdx = 28             frames = [PhotoImage(file='D:/Python_code/tk/final/gif/R_fianl.gif',format='gif -index %i' % (i)) for i in range(numIdx)]# 填充帧内容到framesdef slow_speed():# 减速if(self.time <= 1500):self.time += 100def add_speed():# 加速if(self.time>100):self.time -=100def test_stop():"""下面为播放与暂停的界面"""btn2.place(x=300, y=740)btn3.place(x=500, y=740)if self.isplaying == True:self.isplaying = Falsecounter = '播放'btn4.config(text=str(counter))else:self.isplaying = Truebtn4.config(text=str("暂停"))update(self.stop_idx)def exit_win():self.time = 500self.isplaying = Falsewin.destroy()def update(idx):    # 定时器函数frame = frames[idx]idx += 1         # 下一帧的序号:canvas.create_image(15, 15, anchor=NW, image=frame)# label.configure(image = frame) # 显示label方法当前帧的图片# 0.1秒(500毫秒)之后继续执行定时器函数(update)if self.isplaying:self.root.after(self.time, update, idx % numIdx)else:self.stop_idx = idxbtn1 = Button(win, text="结束", width=10, height=2, bg='White', fg='black',font='宋体 12 bold', relief='raised', command=exit_win)btn2 = Button(win, text="加速", width=10, height=2, bg='White', fg='black',font='宋体 12 bold', relief='raised', command=add_speed)btn3 = Button(win, text="减速", width=10, height=2, bg='White', fg='black',font='宋体 12 bold', relief='raised', command=slow_speed)btn4 = Button(win, text="播放", width=10, height=2, bg='White', fg='black',font='宋体 12 bold', relief='raised', command=test_stop)# btn5 = Button(win, text="绘图", width=10, height=2, bg='White', fg='black',#   font='宋体 12 bold', relief='raised', command= paint_photo)btn1.place(x=100, y=740)btn4.place(x=700, y=740)win.after(0, update(0))# 结束后重新调用

主函数

# 这是硬件实践的主界面测试·
from time import time
from tkinter import *
from main_test import test_menu
# 主函数
if __name__=="__main__":root = Tk()my_menu = test_menu(root,500)my_menu.show_test()

总结

提示:本文章只是为了解决不能把图片放映到源文件上。如果需要全部源文件,可以在文章下面留言我,我在把代码文件发出来,希望对大家有所帮助。

Python tkinter的简单使用,在绘布上播放GIF和图片相关推荐

  1. python简单代码加法-Python tkinter实现简单加法计算器代码实例

    tkinter 是 Python 的标准 GUI 库.Python 使用 tkinter 可以快速的创建 GUI 应用程序.由于 tkinter 是内置到 python 的安装包中.只要安装好 Pyt ...

  2. python实现ping命令_[小菜随笔]python tkinter实现简单的ping命令

    本文主要是介绍python图形界面上的按键与实际功能的对接,其实编程掌握了基础之后的学习应该都是靠自己去挖掘其他的 在网上发现多半教程都是2的,故本文使用的是python3.5,其实也没什么区别,就有 ...

  3. Python+tkinter实现简单的登录界面

    import tkinter as tk # 使用Tkinter前需要先导入 import tkinter.messagebox import pickle# 第1步,实例化object,建立窗口wi ...

  4. 基于python tkinter的简单计算器(v1.0)

    import tkinter#定义计算器类 class Calc:#初始化魔术方法def __init__(self):#初始化共用属性#定义一个用于存放被计算字符串的列表self.operation ...

  5. python科学计算库numpy和绘图库PIL的结合,素描图片(原创)

    # 导入绘图库 from PIL import Image #导入科学计算库 import numpy as np #封装一个图像处理工具类 class TestNumpy(object):def p ...

  6. python计算机体系三层结构_Python tkinter 实现简单登陆注册 基于B/S三层体系结构,实现用户身份验证...

    Python tkinter 实现简单登陆注册 最终效果 开始界面 ​ 注册 登陆 ​ 源码 login.py # encoding=utf-8 from tkinter import * from ...

  7. python tkinter计算器实例_Python+tkinter使用80行代码实现一个计算器实例

    Python+tkinter使用80行代码实现一个计算器实例 本文主要探索的是使用Python+tkinter编程实现一个简单的计算器代码示例,具体如下. 闲话不说,直奔主题.建议大家跟着敲一遍代码, ...

  8. python tkinter库、添加gui界面_使用Python中tkinter库简单gui界面制作及打包成exe的操作方法(二)...

    使用Python中tkinter库简单gui界面制作及打包成exe的操作方法(二),创建一个,界面,布局,文件,路径 使用Python中tkinter库简单gui界面制作及打包成exe的操作方法(二) ...

  9. tkinter message_【莫烦Python】Tkinter 做简单的窗口视窗lt;学习笔记(2)gt;

    接(1) 还有五个苹果:[莫烦Python]Tkinter 做简单的窗口视窗<学习笔记>​zhuanlan.zhihu.com 登录窗口小例子(pickle存取) import

最新文章

  1. 我的第一个MapReduce程序(WordCount)
  2. Android线程间通信的几种实现方式
  3. Glusterfs初试
  4. MySQL多种安装方式选择
  5. 【转】对称加密和分组加密中的四种模式(ECB、CBC、CFB、OFB)
  6. Java 判断文件是否隐藏
  7. Tiktok代运营才是跨境卖家的未来?别再错过这个风口了!
  8. 让你人际关系更上一层楼的26条原则
  9. 联通辟谣“不支持华为 5G”;罗永浩称索尼手机不如锤子;Linux 5.2.1 发布 | 极客头条...
  10. 在线GIF图片帧拆分工具
  11. 移动端真机调试的两种方法
  12. l如何更新linux内核,WSL更新Linux内核版本
  13. 24. Django部署:项目部署
  14. xampp apache windows10 同一局域网下他人访问本地项目
  15. Dev C++使用教程(使用Dev C++编写C语言程序)
  16. hive hql 交差并集 练习
  17. Houdini17 OptiX Denoise使用
  18. mysql数据库安全开关_对MySQL数据库的安全进行的详述
  19. cada0图纸框_CAD的图框应该怎么画-百度经验
  20. python:机器学习(五):(TensorFlow)

热门文章

  1. 瑞萨单片机iap串口升级boot程序与app程序合并的工程构建-学习记录
  2. 上海市社保和医保以及公积金转出到异地(注意事项)
  3. LabVIEW开发FPGA参考框架
  4. iOS 如何获取手机型号、系统版本、电池电量
  5. 还儿童一个健康上网环境,正式开启我的路由器URL网址白名单之旅
  6. 前端js正则验证大全(一套完整的正则验证解决方案)@莫成尘
  7. 01 ArcGIS空间分析--水文分析--利用水文分析方法提取山谷山脊线
  8. 20180529-A · Comic book characters · ggplot2 geom_bar geom_text 柱状图 条形图 图例 · R 语言数据可视化 案例 源码
  9. 京东百万年薪大佬用JAVA绘制“五子棋棋盘”(附代码)
  10. 转行做软件编程开发的经历