Tkinter图像浏览程序 - 类与全局变量的比较

问题描述 投票:0回答:1

我一直在关注这个Tkinter的介绍教程。

链接。https:/www.youtube.comwatch?v=YXPyB4XeYLA

目前正在看图片查看器的应用教程。

视频中的家伙使用了大量的全局变量,我知道这有点不妥,所以我也做了同样的事情,但用类和类方法代替。它的工作,这是伟大的,但我不禁觉得它是不必要的意大利面条一样,一个更简单的方法可能是可能的?

注:我把我使用的真实目录替换为 imagedirectory

from tkinter import *
from PIL import ImageTk, Image
import os

root = Tk()


class mylabel:
    def __init__(self):
        self.rty = ""
        self.imagedict = {}
        self.iNum = 0

    def removeself(self):
        self.rty.grid_forget()

    def makeyoself(self, imagenum):
        self.rty = Label(root, image=self.imagedict["image" + str(imagenum)])
        self.rty.image = self.imagedict["image" + str(imagenum)]
        self.rty.grid(row=0, column=1, columnspan=5)

    def gettheimages(self):
        os.chdir('imagedirectory')
        for a, b, c in os.walk('imagedirectory'):

            for imgs in range(len(c)):
                self.imagedict["image" + str(imgs)] = ImageTk.PhotoImage(Image.open(c[imgs]))


class buttonclass:
    def __init__(self, inrelationto):
        self.but = ""
        self.inrelationto = inrelationto
        self.notme = ""

    def makeforwarden(self):
        self.but = Button(root, text=">>", command = self.forward)
        self.but.grid(row=1,column=6)

    def makeforwarddis(self):
        self.but = Button(root, text=">>", command = self.forward, state=DISABLED)
        self.but.grid(row=1,column=6)

    def makebackwarden(self):
        self.but = Button(root, text="<<", command = self.backward)
        self.but.grid(row=1,column=0)

    def makebackwarddis(self):
        self.but = Button(root, text="<<", command = self.backward, state=DISABLED)
        self.but.grid(row=1,column=0)

    def removebut(self):
        self.but.grid_forget()

    def forward(self):
        if self.inrelationto.iNum < len(self.inrelationto.imagedict) -1:
            self.inrelationto.removeself()
            self.inrelationto.makeyoself(self.inrelationto.iNum +1)
            if self.inrelationto.iNum == 0:
                self.notme.removebut()
                self.notme.makebackwarden()
            if self.inrelationto.iNum == len(self.inrelationto.imagedict) -2:
                self.removebut()
                self.makeforwarddis()
            self.inrelationto.iNum += 1

    def backward(self):
        if self.inrelationto.iNum > 0:
            self.inrelationto.removeself()
            self.inrelationto.makeyoself(self.inrelationto.iNum - 1)
            if self.inrelationto.iNum == 1:
                self.removebut()
                self.makebackwarddis()
            if self.inrelationto.iNum == len(self.inrelationto.imagedict) - 1:
                self.notme.removebut()
                self.notme.makeforwarden()
            self.inrelationto.iNum -=1


def setup():
    pictureviewer = mylabel()
    buttonforward = buttonclass(pictureviewer)
    buttonbackward = buttonclass(pictureviewer)
    buttonforward.notme = buttonbackward
    buttonbackward.notme = buttonforward
    pictureviewer.gettheimages()

    pictureviewer.makeyoself(0)
    buttonforward.makeforwarden()
    buttonbackward.makebackwarddis()


if __name__ == '__main__':
    setup()
    root.mainloop()
python python-3.x tkinter tk
1个回答
0
投票

我想说,globals对于一个小程序来说是可以的。另外你的OOP代码看起来有点意淫的原因是你这样写的。思考如何用对象最好地构造一个应用程序是一种习得的技能,你必须研究一堆例子才能掌握它的窍门。所以,这里有一个例子,说明你可以如何构造应用程序。

from tkinter import *
from PIL import ImageTk, Image
import os

class viewer(Frame):
    def __init__(self, master):
        super().__init__()

        # Create image list
        self.image_list = []
        cwd = r'C:\Users\qwerty\Documents\Python\images'
        for path, dirs, files in os.walk(cwd):
            for file in files:
                full_name = os.path.join(path, file)
                self.image_list.append(ImageTk.PhotoImage(Image.open(full_name)))

        # Create GUI
        self.image_index = 0
        self.picture = Label(self, image=self.image_list[self.image_index])
        self.picture.pack()
        button_frame = Frame(self)
        button_frame.pack(expand=True, fill='x')
        self.buttonforward = Button(button_frame, text='>>', command=self.forward)
        self.buttonforward.pack(side='right')
        self.buttonbackward = Button(button_frame, text='<<', command=self.backward)
        self.buttonbackward.pack(side='left')
        self.buttonbackward.config(state='disabled')

    def forward(self):
        self.image_index = self.image_index + 1
        if self.image_index == len(self.image_list) - 1:
            self.buttonforward.config(state='disabled')
        self.buttonbackward.config(state='normal')
        self.picture.config(image=self.image_list[self.image_index])

    def backward(self): 
        self.image_index = self.image_index - 1
        if self.image_index == 0:
            self.buttonbackward.config(state='disabled')
        self.buttonforward.config(state='normal')
        self.picture.config(image=self.image_list[self.image_index])

if __name__ == '__main__':
    root = Tk()
    z = viewer(root)
    z.pack()
    root.mainloop()

这里是一个学习的好地方: 构建一个tkinter应用程序的最佳方式?

© www.soinside.com 2019 - 2024. All rights reserved.