Search code examples
pythonpython-3.xtkinterttk

tkinter.ttk.Progressbar: How to change thickness of a horizontal bar


I am using the ttk.Progressbar in my app. I have scoured the net for an answer but no avail.

I have the following code which is working well. But I want to change the thickness of the bar.

progressbar = ttk.Progressbar(myGui, orient=HORIZONTAL,
                              length=400, mode="determinate",
                              variable=value_progress,
                              )
progressbar.pack()

I want the length to still be 400, but from the top of the bar to the bottom, I wish to decrease that so its half or less then half. (I want my bar on a diet, so to say)

But I am beating my head against the wall to figure out a solution.

Andy ideas? Thanks in advance.


Solution

  • The ttk progress bar appears to lack the width option in Python.

    Using a work around (here) for an issue with a Tkinter Button. From this I have been able to create a working solution.

    The key to solving the issue was to add the progress bar to a window inside the canvas. Using a window inside the canvas doesn't cause the canvas to resize when the widget is added which means we can control the width of the progress bar.

    I have created some working example code:

    from ttk import Progressbar
    import Tkinter
    
    class Example(Tkinter.Frame):
        def __init__(self, parent):
            Tkinter.Frame.__init__(self, parent)
            self.parent = parent
            self.initUI()
    
        def initUI(self):
            value_progress =50
            self.parent.title("Progressbar Thingymawhatsit")
            self.config(bg = '#F0F0F0')
            self.pack(fill = Tkinter.BOTH, expand = 1)
                    #create canvas
            canvas = Tkinter.Canvas(self, relief = Tkinter.FLAT, background = "#D2D2D2",
                                                width = 400, height = 5)
    
            progressbar = Progressbar(canvas, orient=Tkinter.HORIZONTAL,
                                      length=400, mode="indeterminate",
                                      variable=value_progress,
    
                                      )
            # The first 2 create window argvs control where the progress bar is placed
            canvas.create_window(1, 1, anchor=Tkinter.NW, window=progressbar)
            canvas.grid()
    
    
    def main():
        root = Tkinter.Tk()
        root.geometry('500x50+10+50')
        app = Example(root)
        app.mainloop()
    
    if __name__ == '__main__':
        main()
    

    So to sum up the progress bar is the same size but you just cant see half of it!