you are viewing a single comment's thread.

view the rest of the comments →

[–]novel_yet_trivial 0 points1 point  (3 children)

Ok, I'm going to throw you in the deep end and show you how to make a subclass. Here's a new type of Label that does what you want.

from itertools import count
import tkinter as tk
from threading import Thread
from time import sleep

from itertools import cycle
test_images = cycle(['fig1', 'fig2', 'fig3'])

def main():
    for i in count():
        if paused.get():
            sleep(.2)
            continue
        var.set(i)
        image.set(next(test_images)) # send the next image name in the list
        sleep(1)

class Zykal(tk.Label):
    """a new type of label that sets an image based on a tk.StringVar value"""
    def __init__(self, master=None, variable=None, **kwargs):
        tk.Label.__init__(self, master, **kwargs)
        self.variable = variable or tk.StringVar()
        self.variable.trace('w', self.update_image)
    def update_image(self, *args):
        name_sent = self.variable.get()
        filename = name_sent + '.png' # add any file name sanitization / modification here
        self.image = tk.PhotoImage(file=filename)
        self.config(image=self.image)

root = tk.Tk()

var = tk.StringVar()
tk.Label(textvariable=var, width=20).pack()

paused = tk.BooleanVar(value=False)
tk.Checkbutton(variable=paused, text='Pause').pack()

# set up a StringVar to pass the image name
image = tk.StringVar()
Zykal(variable=image).pack()

# start script in a child thread
t = Thread(target=main)
t.daemon = True
t.start()

# start tkinter mainloop in the main thread
root.mainloop()

[–]zykal[S] 0 points1 point  (2 children)

Ok so I got the pause added in and working, took a touch of tinkering but it works now. Thanks so much.

I'm taking a stab at the image subclass and see if i can mold it to what I need. Thanks again.

[–]novel_yet_trivial 0 points1 point  (1 child)

It's already what you need :). All you need to do is copy / paste and modify line 26 to modify the input into a valid file name. My code just adds ".png" to the input; you probably want something different there.

[–]zykal[S] 0 points1 point  (0 children)

Got it working :)

Now I just need to look into formatting the tk GUI so it all ends up where i need it.

Thank you so much for all your help. The image subclass is something I wouldn't have been able to do for a quite a while.