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

您所在的位置:网站首页 播放使用的图片 Python tkinter的简单使用,在绘布上播放GIF和图片

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

2023-08-15 05:36| 来源: 网络整理| 查看: 265

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)] # 填充帧内容到frames 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 = idx win.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 Jump class test_menu: def __init__(self, root, time): self.title = ' 大勇' self.root = root self.time = time self.isplaying = False self.stop_idx = 0 self.show_about self.minimum self.show_test self.text_exit def get_time(self): return self.time def 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= 300 y =240 w = (screen_width - x) / 2 h = (screen_height -y) /2 root1.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_time choose_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 = 720 y = 480 w = (screen_width - x) / 2 h = (screen_height -y) /2 self.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) # 建立菜单类对象,并将此菜单类别命名为File filemenu = 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('', 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 = isplaying self.root = root self.time = time def update_time(self,current_time): if current_time != self.time: self.time = current_time def 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)] # 填充帧内容到frames def slow_speed(): # 减速 if(self.time 100): self.time -=100 def test_stop(): """下面为播放与暂停的界面""" btn2.place(x=300, y=740) btn3.place(x=500, y=740) if self.isplaying == True: self.isplaying = False counter = '播放' btn4.config(text=str(counter)) else: self.isplaying = True btn4.config(text=str("暂停")) update(self.stop_idx) def exit_win(): self.time = 500 self.isplaying = False win.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 = idx btn1 = 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() 总结

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



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3