2

I'm a python newbe and am trying to come to grips with threaded parts in GUI apps I downloaded the following example that names the progress box "tk" this seems to be tied up with self and parent so i guess i need to find a way to make the test string "progress" go into one of the places where these names appear. Being a python newbe i'm not clever enough to know the correct and most compact way to do this given the compact way this entire example is constructed.

"""
An examnple of the use of threading to allow simultaneous operations in a
tkinter gui (which is locked to a single thread)
"""

import threading
import tkinter as tk
from tkinter import ttk


class Progress():
    """ threaded progress bar for tkinter gui """
    def __init__(self,parent, row, column, columnspan):
        self.maximum = 100
        self.interval = 10
        self.progressbar = ttk.Progressbar(parent, orient=tk.HORIZONTAL,
                                           mode="indeterminate",
                                           maximum=self.maximum)
                                   
        self.progressbar.grid(row=row, column=column,
                              columnspan=columnspan, sticky="we")
        self.thread = threading.Thread()
        self.thread.__init__(self.progressbar.start(self.interval))
        self.thread.start()
      

    def pb_stop(self):
        """ stops the progress bar """
        if not self.thread.is_alive():
            VALUE = self.progressbar["value"]
            self.progressbar.stop()
            self.progressbar["value"] = VALUE

    def pb_start(self):
        """ starts the progress bar """
        if not self.thread.is_alive():
            VALUE = self.progressbar["value"]
            self.progressbar.configure(mode="indeterminate",
                                       maximum=self.maximum,
                                       value=VALUE)
            self.progressbar.start(self.interval)

    def pb_clear(self):
        """ stops the progress bar """
        if not self.thread.is_alive():
            self.progressbar.stop()
            self.progressbar.configure(mode="determinate", value=0)

    def pb_complete(self):
        """ stops the progress bar and fills it """
        if not self.thread.is_alive():
            self.progressbar.stop()
            self.progressbar.configure(mode="determinate",
                                       maximum=self.maximum,
                                       value=self.maximum)

def printmsg():
    """ prints a message in a seperate thread to tkinter """
    print("proof a seperate thread is running")
    


    class AppGUI(tk.Frame):
        """ class to define tkinter GUI"""
    def __init__(self, parent,):
        tk.Frame.__init__(self, master=parent)
        prog_bar = Progress(parent, row=0, column=0, columnspan=2)
                
        # Button 1
        start_button = ttk.Button(parent, text="start",
                                  command=prog_bar.pb_start)
        start_button.grid(row=1, column=0)
        # Button 2
        stop_button = ttk.Button(parent, text="stop",
                                 command=prog_bar.pb_stop)
        stop_button.grid(row=1, column=1)
        # Button 3
        complete_button = ttk.Button(parent, text="complete",
                                     command=prog_bar.pb_complete)
        complete_button.grid(row=2, column=0)
        # Button 4
        clear_button = ttk.Button(parent, text="clear",
                                  command=prog_bar.pb_clear)
        clear_button.grid(row=2, column=1)
        # Button 5
        test_print_button = ttk.Button(parent, text="thread test",
                                       command=printmsg)
        test_print_button.grid(row=3, column=0, columnspan=2, sticky="we")


ROOT = tk.Tk()
APP = AppGUI(ROOT)
ROOT.mainloop()

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
TonyF
  • 25
  • 6
  • what is it you want to do? change the print message? What do you mean it's named tk? – Alexander Oct 08 '22 at 03:57
  • This copy seems to have got an indent here: class AppGUI(tk.Frame): """ class to define tkinter GUI""" and lost a return character – TonyF Oct 08 '22 at 04:09
  • That much I figured out. What is it you are trying to do with the code? – Alexander Oct 08 '22 at 04:13
  • am learning about threading and i wanted to change the name of the box from tk to anything else in a text string e.g "progress" in a way matching the compact nature of the example code – TonyF Oct 08 '22 at 04:20
  • Do you mean the window title bar? – Alexander Oct 08 '22 at 04:23
  • Yes where "tk" appears at the top of the bar box to anything else i could "quote" someplace in the code – TonyF Oct 08 '22 at 04:34
  • 'tk' is just the default name. You can change it to whatever you want as shown in my answer. I don't understand what that has to do with threading though – Alexander Oct 08 '22 at 04:38
  • Perfect just what i wanted to do Thanks and problem solved Alexander. Newbies never guess what is under the bonnet in the way of interactions or argument passing in multi threaded stuff. Your help is much appreciated. – TonyF Oct 08 '22 at 05:25
  • TonyF, be sure to follow SO etiquette and click this to make it your accepted answer – Greg Oct 08 '22 at 05:49
  • TonyF, if your code example now makes more sense you might like to try this code example to [customise the progress bar](https://stackoverflow.com/a/34482761/2348597) – Greg Oct 09 '22 at 00:44

1 Answers1

2

To change the title bar... just use the title method on the root widget. At the base of your script.

ROOT = tk.Tk()
ROOT.title("Anything you want it to be.")
APP = AppGUI(ROOT)
ROOT.mainloop()
Alexander
  • 16,091
  • 5
  • 13
  • 29