Tkinter image viewer app - Classes vs Global Variables - python

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

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?

Related

python - how to go to next image in slideshow using tkinter gui

i'm trying to make a photo slideshow program. it isn't working as i want to go to the next photo in the list using the self.counter variable, but the value of self.counter is going back to 0 as it forgets the fact that i changed the value. i didn't think i could use configure as then the buttons to go to the next image wouldn't work either.
i hope you can help :) much appreciated.
from tkinter import *
class SlideShowGUI:
def __init__(self, parent, counter = 0):
self.counter = counter
self.images_list = ["smiley.png", "carrot.png", "bunny.jpg"]
self.photo = PhotoImage(file = self.images_list[self.counter])
self.b1 = Button(parent, text = "", image = self.photo, bg = "white")
self.b1.grid(row = 0, column = 0)
back = Button(parent, width = 2, anchor = W, text = "<", relief = RIDGE, command = self.previous_image)
back.grid(row = 1, column = 0)
forward = Button(parent, width = 2, anchor = E, text = ">", relief = RIDGE, command = self.next_image)
forward.grid(row = 1, column = 1)
def previous_image(self):
self.counter -= 1
self.photo.configure(file = self.images_list[self.counter])
def next_image(self):
self.counter += 1
self.photo.configure(file = self.images_list[self.counter])
#main routine
if __name__ == "__main__":
root = Tk()
root.title("hello")
slides = SlideShowGUI(root)
root.mainloop()
sorry it will not work without getting images !
error message if i click next button two times
use this instead:
def previous_image(self):
self.counter -= 1
self.photo.configure(file = images_list[self.counter])
def next_image(self):
self.counter += 1
self.photo.configure(file = images_list[self.counter])
except You have to watch out for List Out Of Index errors
Also why are You using global images_list? There seems to be no point. If You wanted to reuse that list in the class You could have just named it self.images_list = [your, items].
The error You are getting: read this

Tkinter: image loop slows down

I wrote a Tkinter GUI that is supposed to show display 3D images by looping through the 2D slices of each 3D image. I read up on how to implement such an image loop, and it was recommended to use the after() function with recursion. This is, in principle, my implementation of that:
def load_image(self):
self.stack = read_3D_image(path)
slice = self.stack[self.slice_no]
im = Image.fromarray(slice)
self.photo = ImageTk.PhotoImage(image=im)
self.canvas.create_image(0, 0, image=self.photo, anchor=tk.NW)
if self.forward is True:
self.slice_no += 1
if self.slice_no == 21:
self.forward = False
if self.forward is False:
self.slice_no -= 1
if self.slice_no == 0:
self.forward = True
root.after(10, self.load_image)
This works well for some time, but after a couple of minutes, the loop notably slows down. I guess that is because of the high number of iterations. Is there a way to fix this?
Edit: I noticed this: when the program runs, the image loop will slow down to about half the original frequency after about 10 minutes. When I run a second instance, its loop runs equally slow. Then when I close the first instance, the second instance loop immediately runs faster. I launch from Eclipse.
Updated full code
import glob
import os.path
import tkinter as tk
from PIL import Image, ImageTk
import numpy as np
import helpers
class App():
def __init__(self, master):
super().__init__()
self.frame = tk.Frame(master)
master.bind("<KP_1>", lambda e: self.is_cell())
master.bind("<KP_2>", lambda e: self.is_not_cell())
self.frame.pack()
self.goto_button = tk.Button(
self.frame, text="Go to:", command=self.goto)
self.goto_button.pack(side=tk.TOP)
self.goto_entry = tk.Entry(self.frame, width=5)
self.goto_entry.pack(side=tk.TOP)
self.img_side_length = 100
self.canvas = tk.Canvas(
master=self.frame, width=self.img_side_length, height=self.img_side_length)
self.canvas.pack()
self.img_label = tk.Label(self.frame, text="Bla")
self.img_label.pack(side=tk.TOP)
self.no_cell_button = tk.Button(
self.frame, text="2: Not cell!", command=self.is_not_cell)
self.no_cell_button.pack(side=tk.RIGHT)
self.cell_button = tk.Button(
self.frame, text="1: Cell!", command=self.is_cell)
self.cell_button.pack(side=tk.RIGHT)
self.img_path = "/storage/images/"
self.img_list = glob.glob(os.path.join(self.img_path, "*"))
self.img_list.sort()
self.slice_no = 0
self.img_no = 0
self.forward = True
self.no_of_imgs = len(self.img_list)
self.stack = []
self.image_id = self.canvas.create_image(0, 0, anchor=tk.NW)
self.stack = helpers.read_image_sequence(self.img_list[self.img_no])
self.classifications = np.zeros(self.no_of_imgs)
self.out_path = "/dev/null"
self.loop_image()
def loop_image(self):
data = self.stack[self.slice_no]
im = Image.fromarray(data)
im = im.resize((self.img_side_length, self.img_side_length))
self.photo = ImageTk.PhotoImage(image=im)
self.canvas.itemconfigure(self.image_id, image=self.photo)
if self.forward is True:
self.slice_no += 1
if self.slice_no == 21:
self.forward = False
if self.forward is False:
self.slice_no -= 1
if self.slice_no == 0:
self.forward = True
root.after(10, self.loop_image)
def next_image(self):
self.img_no += 1
self.stack = helpers.read_image_sequence(self.img_list[self.img_no])
self.img_label['text'] = self.img_list[self.img_no].split("/")[-1]
def previous_image(self):
self.img_no -= 1
self.stack = helpers.read_image_sequence(self.img_list[self.img_no])
self.img_label['text'] = self.img_list[self.img_no].split("/")[-1]
def is_cell(self):
self.classifications[self.img_no] = 1
with open(self.out_path, "a") as f:
f.write(str(self.img_no) + "," + str(1) + "\n")
print(self.classifications)
self.next_image()
def is_not_cell(self):
self.classifications[self.img_no] = 2
with open(self.out_path, "a") as f:
f.write(str(self.img_no) + "," + str(2) + "\n")
print(self.classifications)
self.next_image()
def goto(self):
self.img_no = int(self.goto_entry.get())
root = tk.Tk()
app = App(root)
root.mainloop()
You are creating 100 images per second and are stacking them on top of each other. After 10 minutes, that's 60,000 images stacked on top of each other. The canvas has performance issues when it has tens of thousands of items on it, even if all but one is invisible.
Instead of creating more and more images on the canvas, just modify the existing image:
def __init__(self):
...
self.canvas = tk.Canvas(...)
...
# create the image object which will display the image
self.image_id = self.canvas.create_image(0, 0, anchor=tk.NW)
def load_image(self):
...
self.photo = ImageTk.PhotoImage(image=im)
# reconfigure the canvas item to show the new image
canvas.itemconfigure(self.image_id, image=self.photo)
...

OpenCV Video with Tkinter and threading Crash

I'm trying to make a multithreaded program with Python, OpenCV, and Tkinter.
My program has some general point.
Load Video from file
Create 2 thread
1st thread to fetch frames from capture object and put it to python Queue
2nd thread to get the frames from Queue
At last, if possible, start and stop the capture object
However, my script seems to behave weirdly. Sometimes it can finish playing video until the end, but sometimes it also crash at some point of the video. Here is what I've got so far.
from Tkinter import Tk, Text
from Tkinter import PhotoImage
from ttk import Frame, Scrollbar, Button, Label
from PIL import Image, ImageTk
import cv
import time
import Queue
import threading
def writeToLog(log, msg):
numlines = log.index('end - 1 line').split('.')[0]
if log.index('end-1c')!='1.0': log.insert('end', '\n')
log.insert('end', msg)
log.see('end')
def GetIplImageMode(img):
orientation = 1 if img.origin == 0 else -1
mode_list = {(1, cv.IPL_DEPTH_8U) : ("L", "L", 1),\
(3, cv.IPL_DEPTH_8U) : ("BGR", "RGB", 3),\
(1, cv.IPL_DEPTH_32F) : ("F", "F", 4)}
key = (img.nChannels, img.depth)
modes = mode_list[key]
return [modes[0], modes[1], orientation]
def IplImage2PIL(img, mode):
return Image.fromstring(mode[1], (img.width, img.height),\
img.tostring(), "raw", mode[0],\
img.width * img.channels,\
mode[2])
def ResizePILImage(pil, width = 260, height = 180):
return pil.resize((width, height), Image.ANTIALIAS)
def PIL2TkImage(pil):
return ImageTk.PhotoImage(pil)
def setImageLabelFromIplImage(label, img_ipl):
mode = GetIplImageMode(img_ipl)
img_pil = IplImage2PIL(img_ipl, mode)
img_resized = ResizePILImage(img_pil)
img_tk = PIL2TkImage(img_resized)
label.configure(image = img_tk)
label.image = img_tk
def setImageLabelFromFile(label, szFileName):
img_ipl = cv.LoadImage(szFileName)
setImageLabelFromIplImage(label, img_ipl)
def mat_from_ipl(ipl):
return cv.GetMat(ipl)
def ipl_from_mat(mat):
ipl = cv.CreateImageHeader((mat.width, mat.height),\
cv.IPL_DEPTH_8U, mat.channels)
cv.SetData(ipl, mat.tostring())
return ipl
class asdf(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.pack(fill='both', expand=True)
self.parent = parent
self.variables()
self.ui()
def variables(self):
self.ctr = 0
self.fps = 0
self.video = None
self.image = None
self.putProc = None
self.getProc = None
self.isRunning = False
self.queue = Queue.Queue()
def ui(self):
f1 = Frame(self)
frm1 = Frame(f1)
self.lbl1 = Label(frm1, image=None)
setImageLabelFromFile(self.lbl1, '../image.bmp')
self.txt1 = Text(frm1, width=30, height=8)
sb1 = Scrollbar(frm1, orient='vertical', command=self.txt1.yview)
self.txt1.configure(yscrollcommand = sb1.set)
self.lbl1.pack()
self.txt1.pack(side='left')
sb1.pack(side='left', fill='y')
frm1.pack(side='left')
frm2 = Frame(f1)
self.lbl2 = Label(frm2, image=None)
setImageLabelFromFile(self.lbl2, '../image.bmp')
self.txt2 = Text(frm2, width=30, height=8)
sb2 = Scrollbar(frm2, orient='vertical', command=self.txt2.yview)
self.txt2.configure(yscrollcommand = sb2.set)
self.lbl2.pack()
self.txt2.pack(side='left')
sb2.pack(side='left', fill='y')
frm2.pack(side='left')
f1.pack()
f2 = Frame(self)
Button(f2, text='Run', command=self.run).pack(side='left')
Button(f2, text='Stop', command=self.stop).pack(side='left')
f2.pack()
def put_to_queue(self):
while self.isRunning:
self.ctr = self.ctr + 1
self.image = cv.QueryFrame(self.video)
time.sleep(1 / self.fps)
try:
writeToLog(self.txt1, '\nPut to queue .. %d' % (self.ctr))
temp1 = cv.CloneImage(self.image)
setImageLabelFromIplImage(self.lbl1, temp1)
temp2 = mat_from_ipl(temp1)
self.queue.put([self.ctr, temp2])
except:
writeToLog(self.txt1, '\nReach end of video ..')
break
def get_from_queue(self):
while self.isRunning:
from_queue = self.queue.get()
self.ctr_fr = from_queue[0]
if self.ctr_fr == self.ctr: time.sleep(30 / self.fps)
temp1 = ipl_from_mat(from_queue[1])
setImageLabelFromIplImage(self.lbl2, temp1)
writeToLog(self.txt2, '\nGet from queue .. %d' % (self.ctr_fr))
time.sleep(1 / self.fps)
def run(self):
self.isRunning = True
self.video = cv.CreateFileCapture('../video.avi')
self.fps = cv.GetCaptureProperty(self.video, cv.CV_CAP_PROP_FPS)
writeToLog(self.txt1, '\nStart put_queue ..')
self.putProc = threading.Thread(target=self.put_to_queue)
self.putProc.start()
time.sleep(1)
writeToLog(self.txt2, '\nStart get_queue ..')
self.getProc = threading.Thread(target=self.get_from_queue)
self.getProc.start()
def stop(self):
self.isRunning = False
if self.putProc.isAlive():
self.putProc._Thread__stop()
writeToLog(self.txt1, '\nputProc still alive, stopping ..')
self.putProc = None
if self.getProc.isAlive():
self.getProc._Thread__stop()
writeToLog(self.txt2, '\ngetProc still alive, stopping ..')
self.getProc = None
self.ctr_fr = 0
self.ctr = 0
if __name__ == '__main__':
root = Tk()
c = asdf(root)
root.mainloop()
Am I doing it wrong?
Any ideas will be very appreciated.
Thanks

Python in Tkinter: What options can I use to resize a button?

I am a beginner programmer. I would like to resize my button, which is defined as "but", and it's defined in the Square class. What option would I use to adjust the size of this button to adjust the height and width? Any help is greatly appreciated and if you could add comments in your code that would be helpful!
import Tkinter
class TicWindow(Tkinter.Tk):
def __init__(self):
Tkinter.Tk.__init__(self)
self.squares = []
self.turn = 0
for r in range(3):
for c in range(3):
b = Square(self).grid(row=r,column=c)
self.squares.append(b)
self.geometry("500x500")
def turn(self):
return self.turn
def changeTurn(self):
if (self.turn == 0):
self.turn = 1
else:
self.turn = 0
class Square(Tkinter.Button):
def __init__(self,parent):
but = Tkinter.Button.__init__(self,parent, text=" ", command=self.changeButtonText)
self.canClick = True
def changeButtonText(self):
if (self.master.turn == 0) and (self.canClick == True):
self.config(text = "X")
elif (self.master.turn == 1) and (self.canClick == True):
self.config(text = "O")
self.master.changeTurn()
self.hasBeenClicked()
def canClick(self):
return self.canClick
def hasBeenClicked(self):
self.canClick = False
class ScoreBoard(Tkinter.Tk):
def __init__(self):
Tkinter.Tk.__init__(self)
self.board = Tkinter.Label(self, text = "No Score Yet")
self.board.pack()
self.geometry("500x500+300+300")
top = TicWindow()
scoreboard = ScoreBoard()
top.mainloop()
This has a simple answer.
Basically, just add the height and width variables when you create the buttons:
Tkinter.Button.__init__(self,parent, text=" ", command=self.changeButtonText, height = 20, width = 30)
You can modify the height and width buttons to change the size of the button.

In Python Tkinter: What option can I use to resize a window?

I am a beginner programmer! My program is not stellar. I just need to figure out how to resize the two windows I am calling on: TicWindow and ScoreBoard. Underneath my ScoreBoard class I have programmed self.board = TicWindow() & self.board.geometry("500x500+300+300"). I have read that to resize a window you need to call upon a root window, which is my TicWindow. I know that it is wrong because it looks like it is in the wrong place and it opens a third window that is resized. Any help is appreciated!
import Tkinter
class TicWindow(Tkinter.Tk):
def __init__(self):
Tkinter.Tk.__init__(self)
self.squares = []
self.turn = 0
for r in range(3):
for c in range(3):
b = Square(self).grid(row=r,column=c)
self.squares.append(b)
def turn(self):
return self.turn
def changeTurn(self):
if (self.turn == 0):
self.turn = 1
else:
self.turn = 0
class Square(Tkinter.Button):
def __init__(self,parent):
Tkinter.Button.__init__(self,parent, text=" ", command=self.changeButtonText)
self.canClick = True
def changeButtonText(self):
if (self.master.turn == 0) and (self.canClick == True):
self.config(text = "X")
elif (self.master.turn == 1) and (self.canClick == True):
self.config(text = "O")
self.master.changeTurn()
self.hasBeenClicked()
def canClick(self):
return self.canClick
def hasBeenClicked(self):
self.canClick = False
class ScoreBoard(Tkinter.Tk):
def __init__(self):
Tkinter.Tk.__init__(self)
self.board = Tkinter.Label(self, text = "No Score Yet").pack()
self.board = TicWindow()
self.board.geometry("500x500+300+300")
top = TicWindow()
scoreboard = ScoreBoard()
top.mainloop()
Sounds like you want to resize your ScoreBoard.
Inside ScoreBoard.__init__, there's no need to create another TicWindow instance. That's why you're getting three windows. Additionally, you shouldn't try to assign a widget and pack it on the same line - the variable will only contain None that way.
class ScoreBoard(Tkinter.Tk):
def __init__(self):
Tkinter.Tk.__init__(self)
self.board = Tkinter.Label(self, text = "No Score Yet")
self.board.pack()
self.geometry("500x500+300+300")
Result:

Categories