Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make tkinter Scale span it's full grid cell

I have a Tkinter ttk.Scale in a GUI. What I want to do is add some labels on the top or bottom (or side if its vertical) that show the values the user is selecting through. I have a minimum working example shown below.

My problem comes from the fact that I cannot figure out how to get the Scale to line up with the labels. It is set to span all the columns of the labels, but the Scale widget comes with a length keyword that automatically forces it to be 100 pixels in size. I can't know, a priori, what to set that length to and I can't figure out how to tell it to fill its grid space.

Is there a way to tell the Scale to choose a length such that it fills its grid space?

import tkinter as tk
from tkinter import ttk

def show_values():
    print (w1.get())

def scaleFunc(val):
    scaleVal = float(w1.get())
    if int(scaleVal) != scaleVal:
        w1.set(round(float(val)))

root = tk.Tk()
for i,text in enumerate(['O','B','A','F','G','K','M','L']):
    ttk.Label(root, text = text).grid(row=0,column=i,padx=10)

w1 = ttk.Scale(root, to=7, command=scaleFunc, length=None)
w1.grid(row = 1, column = 0, columnspan = 8,ipadx=0)

ttk.Button(root, text='Show', command=show_values).grid(row=2,column=0,columnspan=8)

root.mainloop()

enter image description here

like image 473
zephyr Avatar asked Sep 13 '25 01:09

zephyr


2 Answers

I suppose I should have played around a bit more. It looks like the Scale widget listens to the sticky keyword in the grid manager.

w1 = ttk.Scale(root, to=7, command=scaleFunc, length=None)
w1.grid(row=1,column=0,columnspan=8,padx=(10,7),sticky='NSEW')
like image 60
zephyr Avatar answered Sep 15 '25 15:09

zephyr


You can use either one of these solutions:

Solution 1: w1.grid(row = 1, column = 0, columnspan=8, sticky='ew')

Solution 2: w1 = ttk.Scale(root, to=7, command=scaleFunc, length=250)

The 1st solution is cleaner.

like image 38
Sun Bear Avatar answered Sep 15 '25 16:09

Sun Bear