7

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.

wyz23x2
  • 298
  • 4
  • 16
david_p
  • 129
  • 1
  • 2
  • 12

3 Answers3

9

If you must use the xpnative theme or themes like it, then you will likely not have the option to change the thickness the conventional way. However if you use the default theme, you can configure the thickness with a style. There are likely other themes that let you do this as well, and if you're going to be playing around a lot with the look and feel of your program, you may wish to use these instead.

from Tkinter import *
from ttk import *

def main():
    root = Tk()
    s = Style()
    s.theme_use("default")
    s.configure("TProgressbar", thickness=50)
    pb = Progressbar(root, style="TProgressbar")
    pb.pack() 
    root.mainloop()

main()
BANZ111
  • 91
  • 1
8

You can just use the ipady option of pack manager.

progressbar = ttk.Progressbar(myGui, orient=HORIZONTAL,
                          length=400, mode="determinate",
                          variable=value_progress,
                          )
progressbar.pack(ipady=10)
h4t1
  • 79
  • 1
  • 3
7

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!

Community
  • 1
  • 1
Noelkd
  • 7,686
  • 2
  • 29
  • 43
  • Thank you very much. One would think we could control it directly. However, no support for that portion just makes one think out side the box. And this is outside the box. – david_p Jul 28 '13 at 23:19
  • 1
    Took me a while to figure something out. Knew adding it to a canvas would help somehow but the canvas then would resize to the size of the progress bar! Thats when I found the canvas create_window option. Glad its worked, +1 if its helped – Noelkd Jul 28 '13 at 23:22
  • After the above example got me thinking, I did a lot of thinking and came up with the following idea. First, create a frame that wont expand: Second, put the progress bar inside the frame. Lastly, control the thickness of the progress bar with the height attribute of the frame. # Progressbar and Frame y= LabelFrame(root, width=400, border = 0, height=5) y.pack(pady=0) y.pack_propagate(0) pro= ttk.Progressbar(y, mode='determinate', length=400) pro.pack(side=BOTTOM) – david_p Jul 29 '13 at 14:44