鱼C论坛

 找回密码
 立即注册
查看: 1942|回复: 9

[技术交流] tkinter做的图片浏览器

[复制链接]
发表于 2019-9-20 19:04:07 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 一个账号 于 2020-3-7 13:37 编辑

1.jpg

ProgressBar.py
  1. """
  2. A  basic widget for showing the progress
  3. being made in a task.

  4. """

  5. from tkinter import *


  6. class ProgressBar:
  7.     def __init__(self, master=None, orientation="horizontal",
  8.                  min=0, max=100, width=100, height=18,
  9.                  doLabel=1, appearance="sunken",
  10.                  fillColor="blue", background="gray",
  11.                  labelColor="yellow", labelFont="Verdana",
  12.                  labelText="", labelFormat="%d%%",
  13.                  value=50, bd=2):
  14.         # preserve various values
  15.         self.master = master
  16.         self.orientation = orientation
  17.         self.min = min
  18.         self.max = max
  19.         self.width = width
  20.         self.height = height
  21.         self.doLabel = doLabel
  22.         self.fillColor = fillColor
  23.         self.labelFont = labelFont
  24.         self.labelColor = labelColor
  25.         self.background = background
  26.         self.labelText = labelText
  27.         self.labelFormat = labelFormat
  28.         self.value = value
  29.         self.frame = Frame(master, relief=appearance, bd=bd)
  30.         self.canvas = Canvas(self.frame, height=height, width=width, bd=0,
  31.                              highlightthickness=0, background=background)
  32.         self.scale = self.canvas.create_rectangle(0, 0, width, height,
  33.                                                   fill=fillColor)
  34.         self.label = self.canvas.create_text(self.canvas.winfo_reqwidth() / 2,
  35.                                              height / 2, text=labelText,
  36.                                              anchor="c", fill=labelColor,
  37.                                              font=self.labelFont)
  38.         self.update()
  39.         self.canvas.pack(side='top', fill='x', expand='no')

  40.     def updateProgress(self, newValue, newMax=None):
  41.         if newMax:
  42.             self.max = newMax
  43.         self.value = newValue
  44.         self.update()

  45.     def update(self):
  46.         # Trim the values to be between min and max
  47.         value = self.value
  48.         if value > self.max:
  49.             value = self.max
  50.         if value < self.min:
  51.             value = self.min
  52.         # Adjust the rectangle
  53.         if self.orientation == "horizontal":
  54.             self.canvas.coords(self.scale, 0, 0,
  55.                                float(value) / self.max * self.width, self.height)
  56.         else:
  57.             self.canvas.coords(self.scale, 0,
  58.                                self.height - (float(value) / self.max * self.height),
  59.                                self.width, self.height)
  60.         # Now update the colors
  61.         self.canvas.itemconfig(self.scale, fill=self.fillColor)
  62.         self.canvas.itemconfig(self.label, fill=self.labelColor)
  63.         # And update the label
  64.         if self.doLabel:
  65.             if value:
  66.                 if value >= 0:
  67.                     pvalue = int((float(value) / float(self.max)) * 100.0)
  68.                 else:
  69.                     value = 0
  70.                 self.canvas.itemconfig(self.label, text=self.labelFormat % value)
  71.             else:
  72.                 self.canvas.itemconfig(self.label, text='')
  73.         else:
  74.             self.canvas.itemconfig(self.label, text=self.labelFormat % self.labelText)
  75.         self.canvas.update_idletasks()

复制代码


AppShell.py

  1. #! /usr/env/python

  2. """
  3. AppShell provides a GUI application framework.

  4. This is a streamlined adaptation of GuiAppD.py, originally
  5. created by Doug Hellmann (doughellmann@mindspring.com).

  6. """

  7. from tkinter import *
  8. import Pmw
  9. import sys, string
  10. import ProgressBar


  11. class AppShell(Pmw.MegaWidget):
  12.     appversion = '1.0'
  13.     appname = 'Generic Application Frame'
  14.     copyright = 'Copyright YYYY Your Company. All Rights Reserved'
  15.     contactname = 'Your Name'
  16.     contactphone = '(999) 555-1212'
  17.     contactemail = 'youremail@host.com'

  18.     frameWidth = 450
  19.     frameHeight = 320
  20.     padx = 5
  21.     pady = 5
  22.     usecommandarea = 0
  23.     balloonhelp = 1

  24.     busyCursor = 'watch'

  25.     def __init__(self, **kw):
  26.         optiondefs = (
  27.             ('padx', 1, Pmw.INITOPT),
  28.             ('pady', 1, Pmw.INITOPT),
  29.             ('framewidth', 1, Pmw.INITOPT),
  30.             ('frameheight', 1, Pmw.INITOPT),
  31.             ('usecommandarea', self.usecommandarea, Pmw.INITOPT))
  32.         self.defineoptions(kw, optiondefs)

  33.         self.root = Tk()
  34.         self.initializeTk(self.root)
  35.         Pmw.initialise(self.root)
  36.         self.root.title(self.appname)
  37.         self.root.geometry('%dx%d' % (self.frameWidth, self.frameHeight))

  38.         # Initialize the base class
  39.         Pmw.MegaWidget.__init__(self, parent=self.root)

  40.         # initialize the application
  41.         self.appInit()

  42.         # create the interface
  43.         self.__createInterface()

  44.         # create a table to hold the cursors for
  45.         # widgets which get changed when we go busy
  46.         self.preBusyCursors = None

  47.         # pack the container and set focus
  48.         # to ourselves
  49.         self._hull.pack(side=TOP, fill=BOTH, expand=YES)
  50.         self.focus_set()

  51.         # initialize our options
  52.         self.initialiseoptions(AppShell)

  53.     def appInit(self):
  54.         # Called before interface is created (should be overridden).
  55.         pass

  56.     def initializeTk(self, root):
  57.         # Initialize platform-specific options
  58.         if sys.platform == 'mac':
  59.             self.__initializeTk_mac(root)
  60.         elif sys.platform == 'win32':
  61.             self.__initializeTk_win32(root)
  62.         else:
  63.             self.__initializeTk_unix(root)

  64.     def __initializeTk_colors_common(self, root):
  65.         root.option_add('*background', 'grey')
  66.         root.option_add('*foreground', 'black')
  67.         root.option_add('*EntryField.Entry.background', 'white')
  68.         root.option_add('*Entry.background', 'white')
  69.         root.option_add('*MessageBar.Entry.background', 'gray85')
  70.         root.option_add('*Listbox*background', 'white')
  71.         root.option_add('*Listbox*selectBackground', 'dark slate blue')
  72.         root.option_add('*Listbox*selectForeground', 'white')

  73.     def __initializeTk_win32(self, root):
  74.         self.__initializeTk_colors_common(root)
  75.         root.option_add('*Font', 'Verdana 10 bold')
  76.         root.option_add('*EntryField.Entry.Font', 'Courier 10')
  77.         root.option_add('*Listbox*Font', 'Courier 10')

  78.     def __initializeTk_mac(self, root):
  79.         self.__initializeTk_colors_common(root)

  80.     def __initializeTk_unix(self, root):
  81.         self.__initializeTk_colors_common(root)

  82.     def busyStart(self, newcursor=None):
  83.         if not newcursor:
  84.             newcursor = self.busyCursor
  85.         newPreBusyCursors = {}
  86.         for component in self.busyWidgets:
  87.             newPreBusyCursors[component] = component['cursor']
  88.             component.configure(cursor=newcursor)
  89.             component.update_idletasks()
  90.         self.preBusyCursors = (newPreBusyCursors, self.preBusyCursors)

  91.     def busyEnd(self):
  92.         if not self.preBusyCursors:
  93.             return
  94.         oldPreBusyCursors = self.preBusyCursors[0]
  95.         self.preBusyCursors = self.preBusyCursors[1]
  96.         for component in self.busyWidgets:
  97.             try:
  98.                 component.configure(cursor=oldPreBusyCursors[component])
  99.             except KeyError:
  100.                 pass
  101.             component.update_idletasks()

  102.     def __createAboutBox(self):
  103.         Pmw.aboutversion(self.appversion)
  104.         Pmw.aboutcopyright(self.copyright)
  105.         Pmw.aboutcontact(
  106.             'For more information, contact:\n %s\n Phone: %s\n Email: %s' % \
  107.             (self.contactname, self.contactphone,
  108.              self.contactemail))
  109.         self.about = Pmw.AboutDialog(self._hull,
  110.                                      applicationname=self.appname)
  111.         self.about.withdraw()
  112.         return None

  113.     def showAbout(self):
  114.         # Create the dialog to display about and contact information.
  115.         self.about.show()
  116.         self.about.focus_set()

  117.     def toggleBalloon(self):
  118.         if self.toggleBalloonVar.get():
  119.             self.__balloon.configure(state='both')
  120.         else:
  121.             self.__balloon.configure(state='status')

  122.     def __createMenuBar(self):
  123.         self.menuBar = self.createcomponent('menubar', (), None,
  124.                                             Pmw.MenuBar,
  125.                                             (self._hull,),
  126.                                             hull_relief=RAISED,
  127.                                             hull_borderwidth=1,
  128.                                             balloon=self.balloon())

  129.         self.menuBar.pack(fill=X)
  130.         self.menuBar.addmenu('Help', 'About %s' % self.appname, side='right')
  131.         self.menuBar.addmenu('File', 'File commands and Quit')

  132.     def createMenuBar(self):
  133.         self.menuBar.addmenuitem('Help', 'command',
  134.                                  'Get information on application',
  135.                                  label='About...', command=self.showAbout)
  136.         self.toggleBalloonVar = IntVar()
  137.         self.toggleBalloonVar.set(1)
  138.         self.menuBar.addmenuitem('Help', 'checkbutton',
  139.                                  'Toggle balloon help',
  140.                                  label='Balloon help',
  141.                                  variable=self.toggleBalloonVar,
  142.                                  command=self.toggleBalloon)

  143.         self.menuBar.addmenuitem('File', 'command', 'Quit this application',
  144.                                  label='Quit',
  145.                                  command=self.quit)

  146.     def __createBalloon(self):
  147.         # Create the balloon help manager for the frame.
  148.         # Create the manager for the balloon help
  149.         self.__balloon = self.createcomponent('balloon', (), None,
  150.                                               Pmw.Balloon, (self._hull,))

  151.     def balloon(self):
  152.         return self.__balloon

  153.     def __createDataArea(self):
  154.         # Create data area where data entry widgets are placed.
  155.         self.dataArea = self.createcomponent('dataarea',
  156.                                              (), None,
  157.                                              Frame, (self._hull,),
  158.                                              relief=GROOVE,
  159.                                              bd=1)
  160.         self.dataArea.pack(side=TOP, fill=BOTH, expand=YES,
  161.                            padx=self['padx'], pady=self['pady'])

  162.     def __createCommandArea(self):
  163.         # Create a command area for application-wide buttons.
  164.         self.__commandFrame = self.createcomponent('commandframe', (), None,
  165.                                                    Frame,
  166.                                                    (self._hull,),
  167.                                                    relief=SUNKEN,
  168.                                                    bd=1)
  169.         self.__buttonBox = self.createcomponent('buttonbox', (), None,
  170.                                                 Pmw.ButtonBox,
  171.                                                 (self.__commandFrame,),
  172.                                                 padx=0, pady=0)
  173.         self.__buttonBox.pack(side=TOP, expand=NO, fill=X)
  174.         if self['usecommandarea']:
  175.             self.__commandFrame.pack(side=TOP,
  176.                                      expand=NO,
  177.                                      fill=X,
  178.                                      padx=self['padx'],
  179.                                      pady=self['pady'])

  180.     def __createMessageBar(self):
  181.         # Create the message bar area for help and status messages.
  182.         frame = self.createcomponent('bottomtray', (), None,
  183.                                      Frame, (self._hull,), relief=SUNKEN)
  184.         self.__messageBar = self.createcomponent('messagebar',
  185.                                                  (), None,
  186.                                                  Pmw.MessageBar,
  187.                                                  (frame,),
  188.                                                  # entry_width = 40,
  189.                                                  entry_relief=SUNKEN,
  190.                                                  entry_bd=1,
  191.                                                  labelpos=None)
  192.         self.__messageBar.pack(side=LEFT, expand=YES, fill=X)

  193.         self.__progressBar = ProgressBar.ProgressBar(frame,
  194.                                                      fillColor='slateblue',
  195.                                                      doLabel=1,
  196.                                                      width=150)
  197.         self.__progressBar.frame.pack(side=LEFT, expand=NO, fill=NONE)

  198.         self.updateProgress(0)
  199.         frame.pack(side=BOTTOM, expand=NO, fill=X)

  200.         self.__balloon.configure(statuscommand= \
  201.                                      self.__messageBar.helpmessage)

  202.     def messageBar(self):
  203.         return self.__messageBar

  204.     def updateProgress(self, newValue=0, newMax=0):
  205.         self.__progressBar.updateProgress(newValue, newMax)

  206.     def bind(self, child, balloonHelpMsg, statusHelpMsg=None):
  207.         # Bind a help message and/or status message to a widget.
  208.         self.__balloon.bind(child, balloonHelpMsg, statusHelpMsg)

  209.     def interior(self):
  210.         # Retrieve the interior site where widgets should go.
  211.         return self.dataArea

  212.     def buttonBox(self):
  213.         # Retrieve the button box.
  214.         return self.__buttonBox

  215.     def buttonAdd(self, buttonName, helpMessage=None,
  216.                   statusMessage=None, **kw):
  217.         # Add a button to the button box.
  218.         newBtn = self.__buttonBox.add(buttonName)
  219.         newBtn.configure(kw)
  220.         if helpMessage:
  221.             self.bind(newBtn, helpMessage, statusMessage)
  222.         return newBtn

  223.     def __createInterface(self):
  224.         self.__createBalloon()
  225.         self.__createMenuBar()
  226.         self.__createDataArea()
  227.         self.__createCommandArea()
  228.         self.__createMessageBar()
  229.         self.__createAboutBox()
  230.         #
  231.         # Create the parts of the interface
  232.         # which can be modified by subclasses
  233.         #
  234.         self.busyWidgets = (self.root,)
  235.         self.createMenuBar()
  236.         self.createInterface()

  237.     def createInterface(self):
  238.         # Override this method to create the interface for the app.
  239.         pass

  240.     def main(self):
  241.         # This method should be left intact!
  242.         self.pack()
  243.         self.mainloop()

  244.     def run(self):
  245.         self.main()


  246. class TestAppShell(AppShell):
  247.     usecommandarea = 1

  248.     def createButtons(self):
  249.         self.buttonAdd('Ok',
  250.                        helpMessage='Exit',
  251.                        statusMessage='Exit',
  252.                        command=self.quit)

  253.     def createMain(self):
  254.         self.label = self.createcomponent('label', (), None,
  255.                                           Label,
  256.                                           (self.interior(),),
  257.                                           text='Data Area')
  258.         self.label.pack()
  259.         self.bind(self.label, 'Space taker')

  260.     def createInterface(self):
  261.         AppShell.createInterface(self)
  262.         self.createButtons()
  263.         self.createMain()


  264. if __name__ == '__main__':
  265.     test = TestAppShell(balloon_state='both')
  266.     test.run()

复制代码


Example_8_10.py

  1. from tkinter import *
  2. import Pmw
  3. import os
  4. import AppShell
  5. from PIL import Image, ImageTk

  6. # Load the PIL plugins for all image types...
  7. for m in ["BmpImagePlugin", "GifImagePlugin", "JpegImagePlugin",
  8.           "PpmImagePlugin", "TiffImagePlugin"]:
  9.     try:
  10.         __import__(m)
  11.     except ImportError:
  12.         pass  # ignore missing driver for now
  13. Image._initialized = 1

  14. path = "./icons/"
  15. imgs = "./images/"


  16. class Node:
  17.     def __init__(self, master, tree, icon=None,
  18.                  openicon=None, name=None, action=None):
  19.         self.master, self.tree = master, tree
  20.         self.icon = PhotoImage(file=icon)
  21.         if openicon:
  22.             self.openicon = PhotoImage(file=openicon)
  23.         else:
  24.             self.openicon = None
  25.         self.width, self.height = 1.5 * self.icon.width(),1.5 * self.icon.height()
  26.         self.name = name
  27.         self.var = StringVar()
  28.         self.var.set(name)
  29.         self.text = Entry(tree, textvariable=self.var, bg=tree.bg,
  30.                           bd=0, width=len(name) + 2, font=tree.font,
  31.                           fg=tree.textcolor, insertwidth=1,
  32.                           highlightthickness=1,
  33.                           highlightbackground=tree.bg,
  34.                           selectbackground="#044484",
  35.                           selectborderwidth=0,
  36.                           selectforeground='white')
  37.         self.action = action
  38.         self.x = self.y = 0  # drawing location
  39.         self.child = []
  40.         self.state = 'colapsed'
  41.         self.selected = 0

  42.     def addChild(self, tree, icon=None, openicon=None, name=None,
  43.                  action=None):
  44.         child = Node(self, tree, icon, openicon, name, action)
  45.         self.child.append(child)
  46.         self.tree.display()
  47.         return child

  48.     def deleteChild(self, child):
  49.         self.child.remove(child)
  50.         self.tree.display()

  51.     def textForget(self):
  52.         self.text.place_forget()
  53.         for child in self.child:
  54.             child.textForget()

  55.     def deselect(self):
  56.         self.selected = 0
  57.         for child in self.child:
  58.             child.deselect()

  59.     def boxpress(self, event=None):
  60.         if self.state == 'expanded':
  61.             self.state = 'colapsed'
  62.         elif self.state == 'colapsed':
  63.             self.state = 'expanded'
  64.         self.tree.display()

  65.     def invoke(self, event=None):
  66.         if not self.selected:
  67.             self.tree.deselectall()
  68.             self.selected = 1
  69.             self.tree.display()
  70.             if self.action:
  71.                 self.action(self.name)
  72.         self.name = self.text.get()
  73.         self.text.config(width=len(self.name) + 2)

  74.     def displayIconText(self):
  75.         tree, text = self.tree, self.text
  76.         if self.selected and self.openicon:
  77.             self.pic = tree.create_image(self.x, self.y,
  78.                                          image=self.openicon)
  79.         else:
  80.             self.pic = tree.create_image(self.x, self.y,
  81.                                          image=self.icon)
  82.         text.place(x=self.x + self.width / 2, y=self.y, anchor=W)
  83.         text.bind("<ButtonPress-1>", self.invoke)
  84.         tree.tag_bind(self.pic, "<ButtonPress-1>", self.invoke, "+")
  85.         text.bind("<Double-Button-1>", self.boxpress)
  86.         tree.tag_bind(self.pic, "<Double-Button-1>",
  87.                       self.boxpress, "+")

  88.     def displayRoot(self):
  89.         if self.state == 'expanded':
  90.             for child in self.child:
  91.                 child.display()
  92.         self.displayIconText()

  93.     def displayLeaf(self):
  94.         self.tree.hline(self.y, self.master.x + 1, self.x)
  95.         self.tree.vline(self.master.x, self.master.y, self.y)
  96.         self.displayIconText()

  97.     def displayBranch(self):
  98.         master, tree = self.master, self.tree
  99.         x, y = self.x, self.y
  100.         tree.hline(y, master.x, x)
  101.         tree.vline(master.x, master.y, y)
  102.         if self.state == 'expanded' and self.child != []:
  103.             for child in self.child:
  104.                 child.display()
  105.             box = tree.create_image(master.x, y,
  106.                                     image=tree.minusnode)
  107.         elif self.state == 'colapsed' and self.child != []:
  108.             box = tree.create_image(master.x, y,
  109.                                     image=tree.plusnode)
  110.         tree.tag_bind(box, "<ButtonPress-1>", self.boxpress, "+")
  111.         self.displayIconText()

  112.     def findLowestChild(self, node):
  113.         if node.state == 'expanded' and node.child != []:
  114.             return self.findLowestChild(node.child[-1])
  115.         else:
  116.             return node

  117.     def display(self):
  118.         master, tree = self.master, self.tree
  119.         n = master.child.index(self)
  120.         self.x = master.x + self.width
  121.         if n == 0:
  122.             self.y = master.y + (n + 1) * self.height
  123.         else:
  124.             previous = master.child[n - 1]
  125.             self.y = self.findLowestChild(previous).y + self.height
  126.         if master == tree:
  127.             self.displayRoot()
  128.         elif master.state == 'expanded':
  129.             if self.child == []:
  130.                 self.displayLeaf()
  131.             else:
  132.                 self.displayBranch()
  133.             tree.lower('line')


  134. class Tree(Canvas):
  135.     def __init__(self, master, icon, openicon, treename, action,
  136.                  bg='white', relief='sunken', bd=2,
  137.                  linecolor='#808080', textcolor='black',
  138.                  font=('MS Sans Serif', 8)):
  139.         Canvas.__init__(self, master, bg=bg, relief=relief, bd=bd,
  140.                         highlightthickness=0)
  141.         self.pack(side='left', anchor=NW, fill='both', expand=1)
  142.         self.bg, self.font = bg, font
  143.         self.linecolor, self.textcolor = linecolor, textcolor
  144.         self.master = master
  145.         self.plusnode = PhotoImage(file=path + 'plusnode.gif')
  146.         self.minusnode = PhotoImage(file=path + 'minusnode.gif')
  147.         self.inhibitDraw = 1
  148.         self.imageLabel = None
  149.         self.imageData = None
  150.         self.child = []
  151.         self.x = self.y = -10
  152.         self.child.append(Node(self, self, action=action,
  153.                                icon=icon, openicon=openicon, name=treename))

  154.     def display(self):
  155.         if self.inhibitDraw: return
  156.         self.delete(ALL)
  157.         for child in self.child:
  158.             child.textForget()
  159.             child.display()

  160.     def deselectall(self):
  161.         for child in self.child:
  162.             child.deselect()

  163.     def vline(self, x, y, y1):
  164.         for i in range(0, int(abs(y - y1)), 2):
  165.             self.create_line(x, y + i, x, y + i + 1, fill=self.linecolor,
  166.                              tags='line')

  167.     def hline(self, y, x, x1):
  168.         for i in range(0, int(abs(x - x1)), 2):
  169.             self.create_line(x + i, y, x + i + 1, y, fill=self.linecolor,
  170.                              tags='line')


  171. class ImageBrowser(AppShell.AppShell):
  172.     usecommandarea = 1
  173.     appname = 'Image Browser'

  174.     def createButtons(self):
  175.         self.buttonAdd('Ok',
  176.                        helpMessage='Exit',
  177.                        statusMessage='Exit',
  178.                        command=self.quit)

  179.     def createMain(self):
  180.         self.panes = self.createcomponent('panes', (), None,
  181.                                           Pmw.PanedWidget,
  182.                                           (self.interior(),),
  183.                                           orient='horizontal')
  184.         self.panes.add('browserpane', min=150, size=160)
  185.         self.panes.add('displaypane', min=.1)

  186.         f = path + 'folder.gif'
  187.         of = path + 'openfolder.gif'
  188.         self.browser = self.createcomponent('browse', (), None,
  189.                                             Tree,
  190.                                             (self.panes.pane('browserpane'),),
  191.                                             icon=f,
  192.                                             openicon=of,
  193.                                             treename='Multimedia',
  194.                                             action=None)
  195.         self.browser.pack(side=TOP, expand=YES, fill=Y)

  196.         self.datasite = self.createcomponent('datasite', (), None,
  197.                                              Frame,
  198.                                              (self.panes.pane('displaypane'),))

  199.         self.datasite.pack(side=TOP, expand=YES, fill=BOTH)

  200.         f = path + 'folder.gif'
  201.         of = path + 'openfolder.gif'
  202.         gf = path + 'gif.gif'
  203.         jf = path + 'jpg.gif'
  204.         xf = path + 'other.gif'

  205.         self.browser.inhibitDraw = 1

  206.         top = self.browser.child[0]
  207.         top.state = 'expanded'
  208.         jpeg = top.addChild(self.browser, icon=f, openicon=of,
  209.                             name='Jpeg', action=None)
  210.         gif = top.addChild(self.browser, icon=f, openicon=of,
  211.                            name='GIF', action=None)
  212.         other = top.addChild(self.browser, icon=f, openicon=of,
  213.                              name='Other', action=None)

  214.         imageDir = {'.jpg': (jpeg, jf), '.jpeg': (jpeg, jf),
  215.                     '.gif': (gif, gf), '.bmp': (other, xf),
  216.                     '.ppm': (other, xf)}

  217.         files = os.listdir(imgs)
  218.         for file in files:
  219.             r, ext = os.path.splitext(file)
  220.             if ext:
  221.                 cont, icon = imageDir.get(ext, (None, None))
  222.                 if cont:
  223.                     cont.addChild(self.browser, icon=icon,
  224.                                   name=file, action=self.showMe)

  225.         self.browser.inhibitDraw = 0
  226.         self.browser.display()

  227.         self.panes.pack(side=TOP,
  228.                         expand=YES,
  229.                         fill=BOTH)

  230.     def createImageDisplay(self):
  231.         self.imageDisplay = self.createcomponent('image', (), None,
  232.                                                  Label,
  233.                                                  (self.datasite,))
  234.         self.browser.imageLabel = self.imageDisplay
  235.         self.browser.imageData = None
  236.         self.imageDisplay.place(relx=0.5, rely=0.5, anchor=CENTER)

  237.     def createInterface(self):
  238.         AppShell.AppShell.createInterface(self)
  239.         self.createButtons()
  240.         self.createMain()
  241.         self.createImageDisplay()

  242.     def showMe(self, dofile):
  243.         if self.browser.imageData: del self.browser.imageData
  244.         self.browser.imageData = ImageTk.PhotoImage(Image.open('%s%s' % (imgs, dofile)))
  245.         self.browser.imageLabel['image'] = self.browser.imageData


  246. if __name__ == '__main__':
  247.     imageBrowser = ImageBrowser()
  248.     imageBrowser.run()
复制代码


程序引用图片(解压缩后放到程序同目录下)

Icons.zip (5.56 KB, 下载次数: 2)
images.zip (176.71 KB, 下载次数: 2)

完整代码+图片如下:

图像查看器.zip (198.42 KB, 下载次数: 16)

原程序是用python2.x编译的,我修改了几个地方,让它可以在python3.7上运行

这个程序里的目录结构应该很常见。

我之前看到批量图片下载工具NeoDownloader大概有类似的界面,那个程序未必是用python写的,但是用python的话,用爬虫加tkinter就可以做出类似的程序来。感兴趣的可以研究一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-9-20 19:06:26 | 显示全部楼层
很好,很好
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-9-21 17:23:36 | 显示全部楼层
一个账号 发表于 2019-9-21 15:50
不好,好文转载没有表明转载地址。

这是《Python与Tkinter编程》第八章里的一个例子Example_8_10.py,源码可以在这里下载
https://www.manning.com/books/python-and-tkinter-programming
这本书很古老了,但是体现了tkinter的博大精深
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-21 17:57:00 From FishC Mobile | 显示全部楼层
好清静啊
好文转载没人看
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-9-21 18:45:22 | 显示全部楼层
_2_ 发表于 2019-9-21 17:57
好清静啊
好文转载没人看

我也是被这个程序困扰了许久,感兴趣默默研究一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-22 08:36:18 | 显示全部楼层
这个好像有个PDF
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-9-22 12:27:07 | 显示全部楼层

目录结构应用很广泛嘛
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-9-28 08:51:36 | 显示全部楼层
自己做个图片浏览器,厉害
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-10-17 17:49:09 | 显示全部楼层
很强大
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-4-23 22:17:26 | 显示全部楼层
十分感谢楼主的无私分享~最近也在看这个程序,但最后的Example_8_10.py一直调不通~看到楼主帖子帮了我很大的忙~再次感谢!!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-28 17:48

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表