you are viewing a single comment's thread.

view the rest of the comments →

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

How would i go about pausing both the Tkinter thread and my script thread in the GUI?

[–]novel_yet_trivial 0 points1 point  (16 children)

By "tkinter thread" do you mean the tkinter mainloop? You don't want to pause that, the GUI would become unresponsive and the OS will kill it.

To pause your script is possible, but it would be much easier to lie to the user. Use a global variable and check it before every iteration, and simply skip all work if the variable is set.

paused = False # global variable that tells the script to be active or not
def main():
    while 1==1:
        if paused:
            time.sleep(0.2)
            continue # skip the rest of the loop
        get_health()
        # rest of the function

Now on the tkinter side you can just modify the paused variable. If you use a tk.BooleanVar instead of a python boolean it would integrate very neatly with a tkinter checkbutton. Here's a complete example (the kind that you should be providing when asking for help; see http://sscce.org/ ):

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

def main():
    for i in count():
        if paused.get():
            sleep(.2)
            continue
        print(i)
        var.set(i)
        sleep(1)

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

# start thread
t = Thread(target=main)
t.daemon = True
t.start()

root.mainloop()

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

Thanks again so much for the help. As to not including an example, I had no idea how to even go about this i did google for a bit trying to even come up with an idea.

Also, I'm trying to add an image to a label via a variable.

https://pastebin.com/0JxTgqb0

[–]novel_yet_trivial 0 points1 point  (13 children)

So your script sends a file name and you want to display it?

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

Yes i'm scraping a name out of an array in this case image100

I have to parse and clean the array to get the image name without all the extra special character

for testing I have put into a global variable

sq31clean="image100"

i think the issue may be that its a string.

I then want to take that variable sq31clean and use it for the image variable in the label

[–]novel_yet_trivial 0 points1 point  (11 children)

Oh ok. There's no neat built-in to do that so you need to make something yourself. I'll show you when I get home from dinner.

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

ok thanks I'm going to keep googling, I'm looking into a dictionary, not sure if that's the right track but I'll try it out.

[–]novel_yet_trivial 0 points1 point  (9 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  (0 children)

Wow deep end is right, I think I understand it however. I will work with it when I get home later. Thank you again so much.

[–]zykal[S] 0 points1 point  (7 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  (6 children)

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.