前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >python pyview幻灯片展示

python pyview幻灯片展示

作者头像
用户5760343
发布于 2022-05-13 02:40:35
发布于 2022-05-13 02:40:35
1.8K00
代码可运行
举报
文章被收录于专栏:sktjsktj
运行总次数:0
代码可运行

image.png

slideShow.py

""" ###################################################################### SlideShow: a simple photo image slideshow in Python/tkinter; the base feature set coded here can be extended in subclasses; ###################################################################### """

from tkinter import * from glob import glob from tkinter.messagebox import askyesno from tkinter.filedialog import askopenfilename import random Size = (450, 450) # canvas height, width at startup and slideshow start

imageTypes = [('Gif files', '.gif'), # for file open dialog ('Ppm files', '.ppm'), # plus jpg with a Tk patch, ('Pgm files', '.pgm'), # plus bitmaps with BitmapImage ('All files', '*')]

class SlideShow(Frame): def init(self, parent=None, picdir='.', msecs=3000, size=Size, **args): Frame.init(self, parent, *args) self.size = size self.makeWidgets() self.pack(expand=YES, fill=BOTH) self.opens = picdir files = [] for label, ext in imageTypes[:-1]: files = files + glob('%s/%s' % (picdir, ext)) self.images = [(x, PhotoImage(file=x)) for x in files] self.msecs = msecs self.beep = True self.drawn = None

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
def makeWidgets(self):
    height, width = self.size
    self.canvas = Canvas(self, bg='white', height=height, width=width)
    self.canvas.pack(side=LEFT, fill=BOTH, expand=YES)
    self.onoff = Button(self, text='Start', command=self.onStart)
    self.onoff.pack(fill=X)
    Button(self, text='Open',  command=self.onOpen).pack(fill=X)
    Button(self, text='Beep',  command=self.onBeep).pack(fill=X)
    Button(self, text='Quit',  command=self.onQuit).pack(fill=X)

def onStart(self):
    self.loop = True
    self.onoff.config(text='Stop', command=self.onStop)
    self.canvas.config(height=self.size[0], width=self.size[1])
    self.onTimer()

def onStop(self):
    self.loop = False
    self.onoff.config(text='Start', command=self.onStart)

def onOpen(self):
    self.onStop()
    name = askopenfilename(initialdir=self.opens, filetypes=imageTypes)
    if name:
        if self.drawn: self.canvas.delete(self.drawn)
        img = PhotoImage(file=name)
        self.canvas.config(height=img.height(), width=img.width())
        self.drawn = self.canvas.create_image(2, 2, image=img, anchor=NW)
        self.image = name, img

def onQuit(self):
    self.onStop()
    self.update()
    if askyesno('PyView', 'Really quit now?'):
        self.quit()

def onBeep(self):
    self.beep = not self.beep    # toggle, or use ^ 1

def onTimer(self):
    if self.loop:
        self.drawNext()
        self.after(self.msecs, self.onTimer)

def drawNext(self):
    if self.drawn: self.canvas.delete(self.drawn)
    name, img  = random.choice(self.images)
    self.drawn = self.canvas.create_image(2, 2, image=img, anchor=NW)
    self.image = name, img
    if self.beep: self.bell()
    self.canvas.update()

if name == 'main': import sys if len(sys.argv) == 2: picdir = sys.argv[1] else: picdir = '../gifs' root = Tk() root.title('PyView 1.2') root.iconname('PyView') Label(root, text="Python Slide Show Viewer").pack() SlideShow(root, picdir=picdir, bd=3, relief=SUNKEN) root.mainloop()


pyView.py

""" ############################################################################# PyView 1.2: an image slide show with associated text notes.

SlideShow subclass which adds note files with an attached PyEdit object, a scale for setting the slideshow delay interval, and a label that gives the name of the image file currently being displayed;

Version 1.2 is a Python 3.x port, but also improves repacking note for expansion when it's unhidden, catches note destroys in a subclass to avoid exceptions when popup window or full component editor has been closed, and runs update() before inserting text into newly packed note so it is positioned correctly at line 1 (see the book's coverage of PyEdit updates). ############################################################################# """

import os from tkinter import * from textEditor import * from slideShow import SlideShow

from slideShow_threads import SlideShow

Size = (300, 550) # 1.2: start shorter here, (h, w)

class SlideShowPlus(SlideShow): def init(self, parent, picdir, editclass, msecs=2000, size=Size): self.msecs = msecs self.editclass = editclass SlideShow.init(self, parent, picdir, msecs, size)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
def makeWidgets(self):
    self.name = Label(self, text='None', bg='red', relief=RIDGE)
    self.name.pack(fill=X)
    SlideShow.makeWidgets(self)
    Button(self, text='Note', command=self.onNote).pack(fill=X)
    Button(self, text='Help', command=self.onHelp).pack(fill=X)
    s = Scale(label='Speed: msec delay', command=self.onScale,
              from_=0, to=3000, resolution=50, showvalue=YES,
              length=400, tickinterval=250, orient='horizontal')
    s.pack(side=BOTTOM, fill=X)
    s.set(self.msecs)

    # 1.2: need to know if editor destroyed, in popup or full component modes
    self.editorGone = False
    class WrapEditor(self.editclass):   # extend PyEdit class to catch Quit
        def onQuit(editor):             # editor is PyEdit instance arg subject
            self.editorGone = True      # self is slide show in enclosing scope
            self.editorUp   = False
            self.editclass.onQuit(editor)       # avoid recursion

    # attach editor frame to window or slideshow frame
    if issubclass(WrapEditor, TextEditorMain):     # make editor now
        self.editor = WrapEditor(self.master)      # need root for menu
    else:
        self.editor = WrapEditor(self)             # embedded or pop-up
    self.editor.pack_forget()                      # hide editor initially
    self.editorUp = self.image = None

def onStart(self):
    SlideShow.onStart(self)
    self.config(cursor='watch')

def onStop(self):
    SlideShow.onStop(self)
    self.config(cursor='hand2')

def onOpen(self):
    SlideShow.onOpen(self)
    if self.image:
        self.name.config(text=os.path.split(self.image[0])[1])
    self.config(cursor='crosshair')
    self.switchNote()

def quit(self):
    self.saveNote()
    SlideShow.quit(self)

def drawNext(self):
    SlideShow.drawNext(self)
    if self.image:
        self.name.config(text=os.path.split(self.image[0])[1])
    self.loadNote()

def onScale(self, value):
    self.msecs = int(value)

def onNote(self):
    if self.editorGone:                # 1.2: has been destroyed
        return                         # don't rebuild: assume unwanted
    if self.editorUp:
        #self.saveNote()               # if editor already open
        self.editor.pack_forget()      # save text?, hide editor
        self.editorUp = False
    else:
        # 1.2: repack for expansion again, else won't expand now
        # 1.2: update between pack and insert, else @ line 2 initially
        self.editor.pack(side=TOP, expand=YES, fill=BOTH)
        self.editorUp = True           # else unhide/pack editor
        self.update()                  # see Pyedit: same as loadFirst issue
        self.loadNote()                # and load image note text

def switchNote(self):
    if self.editorUp:
        self.saveNote()                # save current image's note
        self.loadNote()                # load note for new image

def saveNote(self):
    if self.editorUp:
        currfile = self.editor.getFileName()     # or self.editor.onSave()
        currtext = self.editor.getAllText()      # but text may be empty
        if currfile and currtext:
            try:
                open(currfile, 'w').write(currtext)
            except:
                pass  # failure may be normal if run off a cd

def loadNote(self):
    if self.image and self.editorUp:
        root, ext = os.path.splitext(self.image[0])
        notefile  = root + '.note'
        self.editor.setFileName(notefile)
        try:
            self.editor.setAllText(open(notefile).read())
        except:
            self.editor.clearAllText()   # might not have a note

def onHelp(self):
    showinfo('About PyView',
             'PyView version 1.2\nMay, 2010\n(1.1 July, 1999)\n'
             'An image slide show\nProgramming Python 4E')

if name == 'main': import sys picdir = '../gifs' if len(sys.argv) >= 2: picdir = sys.argv[1]

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
editstyle = TextEditorComponentMinimal
if len(sys.argv) == 3:
    try:
        editstyle = [TextEditorMain,
                     TextEditorMainPopup,
                     TextEditorComponent,
                     TextEditorComponentMinimal][int(sys.argv[2])]
    except: pass

root = Tk()
root.title('PyView 1.2 - plus text notes')
Label(root, text="Slide show subclass").pack()
SlideShowPlus(parent=root, picdir=picdir, editclass=editstyle)
root.mainloop()
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
python 编辑框 text
print('PP4E scrolledtext') from tkinter import *
用户5760343
2022/05/13
8930
python 编辑框 text
python tkinter(2)
1、设置label的字体、颜色、背景色、宽、高 from tkinter import * root = Tk() labelfont = ('times', 20, 'bold') # family, size, style widget = Label(root, text='Hello config world') widget.config(bg='black', fg='yellow') # yellow text on black label widget.config(font=labelfont) # use a larger font widget.config(height=3, width=20) # initial size: lines,chars widget.pack(expand=YES, fill=BOTH) root.mainloop() 2、bd设置边框、relief=设置边框类型,cursor鼠标
用户5760343
2022/05/13
8100
python tkinter(2)
python textEditor 自制编辑器
""" ############################################################################### An extended Frame that makes window menus and toolbars automatically. Use GuiMakerFrameMenu for embedded components (makes frame-based menus). Use GuiMakerWindowMenu for top-level windows (makes Tk8.0 window menus). See the self-test code (and PyEdit) for an example layout tree format. ############################################################################### """
用户5760343
2022/05/13
1.3K0
python textEditor 自制编辑器
python tkinter pil 缩略图 脚本
""" display all images in a directory as thumbnail image buttons that display the full image when clicked; requires PIL for JPEGs and thumbnail image creation; to do: add scrolling if too many thumbs for window! """
用户5760343
2022/05/13
4120
Python实现计算机屏幕任意区域截图
程序功能与用法:运行后有个主窗体,上面有个按钮,单击后开始截图,鼠标坐标落下开始截图,鼠标左键抬起表示截图结束,然后弹出对话框提示保存截图文件。 本文要点在于Python扩展库pillow提供的ImageGrab支持全屏幕或指定区域的截图。 import tkinter import tkinter.filedialog import os from PIL import ImageGrab from time import sleep #创建tkinter主窗口 root = tkinter.Tk() #
Python小屋屋主
2018/04/16
6.1K0
python canvas 拖拽和移动\事件
""" draw elastic shapes on a canvas on drag, move on right click; see canvasDraw_tags*.py for extensions with tags and animation """
用户5760343
2022/05/13
9910
python canvas 拖拽和移动\事件
python pyphoto图片查看器
""" ############################################################################ PyPhoto 1.1: thumbnail image viewer with resizing and saves.
用户5760343
2022/05/13
1.3K0
python pyphoto图片查看器
python tkinter
1、 from tkinter import Label widget=Label(None,text='Hello Gui') widget.pack() widget.mainloop() 2| expand fill:组件随窗口调整大小 from tkinter import * widget=Label(None,text='Hello Gui') widget.pack(expand=YES,fill=BOTH) widget.mainloop()
用户5760343
2022/05/13
1.3K0
python tkinter
Python实现屏幕取色器功能
代码主要思路:首先获取全屏幕截图,在截取的图像上获取指定位置的像素颜色值并转换为十六进制表示形式。 遗憾之处:这个程序的部分代码比较长,在手机上查看时这些换行可能会影响阅读质量,记得之前有个朋友提过建议让我加上代码块,不过我试了试,微信公众号后台的编辑器不支持这个功能,网上找了几个编辑器也不太好用。如果有朋友知道简单易用的支持代码块的编辑器还请推荐一下,谢谢! import os from time import sleep import tkinter import tkinter.filedialog
Python小屋屋主
2018/04/17
7.8K3
Python+tkinter+pillow实现屏幕任意区域截图
基本思路:首先获取并显示全屏幕截图,然后在全屏幕截图上响应鼠标左键按下和抬起事件,最后进行二次截图。 import tkinter import tkinter.filedialog import os from PIL import ImageGrab from time import sleep root = tkinter.Tk() #设置窗口大小与位置 root.geometry('100x40+400+300') #设置窗口大小不可改变 root.resizable(False, False) #
Python小屋屋主
2018/04/16
8K3
python 动态GUI表单生成器 脚本***
""" ################################################################## a reusable form class, used by getfilegui (and others) ################################################################## """
用户5760343
2022/05/13
6520
python 动态GUI表单生成器 脚本***
精选了20个Python实战项目(附源码),拿走就用!
Python是目前最好的编程语言之一。由于其可读性和对初学者的友好性,已被广泛使用。
小F
2021/12/21
3.8K0
精选了20个Python实战项目(附源码),拿走就用!
python 简易编辑器 text
print('PP4E scrolledtext') from tkinter import *
用户5760343
2022/05/13
4360
python 简易编辑器 text
【工具】旋转图片-数据集制作工具, 开源!
        之前做了一个下载百度的旋转图片验证码的工具(多进程下载百度旋转验证码图片-制作数据集),那么拿到了图片数据,就需要手动校正调整来制作数据集,所以出了这个工具。
小锋学长生活大爆炸
2023/11/09
2481
【工具】旋转图片-数据集制作工具, 开源!
用python对图片批量压缩和格式转换
python作为简单、易学的编程语言,上手入门要容易许多。如果数据分析、机器学习、人工智能这些对你来说比较遥远,用python写一点实用的小脚本帮你解决问题、提高办公效率也是不错的。
快学Python
2021/08/09
6540
项目展示:像素化图片生成器
https://cloud.tencent.com/developer/article/2465647?shareByChannel=link
小说男主
2024/11/25
1851
项目展示:像素化图片生成器
python guimixin 消息调用 工具dialog封装
""" ############################################################################### a "mixin" class for other frames: common methods for canned dialogs, spawning programs, simple text viewers, etc; this class must be mixed with a Frame (or a subclass derived from Frame) for its quit method ############################################################################### """
用户5760343
2022/05/13
3140
有趣的python代码_python五角星代码
天天敲代码的朋友,有没有想过代码也可以变得很酷炫又浪漫?今天就教大家用Python模拟出绽放的烟花庆祝昨晚法国队夺冠,工作之余也可以随时让程序为自己放一场烟花秀。
全栈程序员站长
2022/11/09
1.5K0
有趣的python代码_python五角星代码
Python: 屏幕取色器(识别屏幕上不同位置的颜色)
文章背景:工作中,有时候需要判断图片中不同位置的颜色。有些颜色不太容易区分,所以想通过Python编写代码,通过屏幕取点,获取某个位置的颜色值。
Exploring
2022/09/20
5.1K0
Python:  屏幕取色器(识别屏幕上不同位置的颜色)
​Python | GUI编程之tkinter (一)
本文内容为使用Python3的tkinter模块,开发GUI。在阅读本文前,请确保你已经或可能满足以下条件:
LogicPanda
2019/07/30
6K0
相关推荐
python 编辑框 text
更多 >
领券
社区富文本编辑器全新改版!诚邀体验~
全新交互,全新视觉,新增快捷键、悬浮工具栏、高亮块等功能并同时优化现有功能,全面提升创作效率和体验
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文