0

I've been following along with this Tkinter introduction tutorials.

Link: https://www.youtube.com/watch?v=YXPyB4XeYLA

Currently going through the image viewer app tutorial.

The guy in the video uses a lot of global variables, and I understand that's a bit of a no-no, so I sort of did the same thing but with classes and class methods instead. It works, which is great, but I can't help feel it's unnecessarily spaghetti like and a simpler way might be possible?

Note: I replaced the real directory I used with 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()
Harshit
  • 1,510
  • 19
  • 42
Pokebab
  • 183
  • 8

1 Answers1

0

I'd say that globals are ok for a small program. Also the reason your OOP code seems a bit spaghetti is because you wrote it that way. Thinking through how to best construct an application with objects is an aquired skill and you'll have to examine a bunch of examples to get the hang of it. So, here's an example of how you could structure the app:

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()

Here is a good place for study: Best way to structure a tkinter application?

figbeam
  • 7,001
  • 2
  • 12
  • 18