Sean Preston
Sean Preston

Reputation: 37

Off-set position of tkinter scale using the .grid manager

I am currently writing a simple piece of inventory management software for a school python assessment. My GUI involves listing out all of the items in stock, and giving the user the ability to stock/sell multiple items at once. This was achieved by placing an entry (amount to buy) and a scale widget (amount to sell) next to each item, and utilizing an "apply" button to enact the changes (For context, the scenario is a comic book store):

https://i.sstatic.net/qHSDH.png

This is the code that I have used to create the new widgets from each file (all of which are contained in a dictionary, pulled from a CSV file):

itteration = 2

labels = {}

nameTitle = Label(main, text="Title").grid(row=1, column=1, padx=5)
stockTitle = Label(main, text="Stock").grid(row=1, column=2, padx=5)
buyTitle = Label(main, text="Amount to Buy").grid(row=1, column=3, padx=5)
sellTitle = Label(main, text="Amount to Sell").grid(row=1, column=4, padx=5)

for item in comic_books:

    name = item.replace(" ", "")
    nameStock = "{}Stock".format(item).replace(" ", "")
    nameBuy = "{}Buy".format(item).replace(" ", "")
    nameSell = "{}Sell".format(item).replace(" ", "")
    labels[name] = Label(main, text=item+":")
    labels[name].grid(column=1, row=itteration, padx=5)
    labels[nameStock] = Label(main, text=comic_books.get(item))
    labels[nameStock].grid(column=2, row=itteration, padx=5)
    labels[nameBuy] = Entry(main)
    labels[nameBuy].grid(column=3, row=itteration, padx=20)
    labels[nameSell] = Scale(main, from_=0, to=comic_books.get(item), orient=HORIZONTAL)
    labels[nameSell].grid(column=4, row=itteration, padx=5)
    itteration += 1

However, I am not a fan of how the the scales appear to be centering themselves in the row based on both the center of the slider, and the number on top. This makes the slider appear lower than the entry widget next to it.

https://i.sstatic.net/EOxh7.png

My question is: how would I go about potentially offsetting the position of the scale widgets upwards by a given amount (maybe 5-10px?) from its original position, to push the actual slider in line with the entry widget before it?

Please let me know if you need any extra clarification

Cheers,

-Sean

Upvotes: 2

Views: 833

Answers (1)

furas
furas

Reputation: 142814

Using sticky='s' and sticky='n' to stick to bottom (south) and top (north) of cell I got this

enter image description here

I didn't use different values in pady

import tkinter as tk

root = tk.Tk()

l = tk.Label(root, text='Label')
l.grid(column=1, row=0, padx=5, sticky='s', pady=10)

e = tk.Entry(root)
e.grid(column=2, row=0, padx=20, sticky='s', pady=10)

s = tk.Scale(root, from_=0, to=10, orient='horizontal')
s.grid(column=3, row=0, padx=5, sticky='n', pady=10)

root.mainloop()

Using sticky with pady=(0,20) for one widget I got this

enter image description here

import tkinter as tk

root = tk.Tk()

l = tk.Label(root, text='Label')
l.grid(column=1, row=0, padx=5, sticky='s', pady=10)

e = tk.Entry(root)
e.grid(column=2, row=0, padx=20, sticky='s', pady=10)

s = tk.Scale(root, from_=0, to=10, orient='horizontal')
s.grid(column=3, row=0, padx=5, sticky='n', pady=(0,20))

root.mainloop()

Upvotes: 2

Related Questions