Naming a progress bar in the attached clever example of a threaded tkinter

Question:

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

Asked By: TonyF

||

Answers:

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()
Answered By: Alexander
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.