0

I am struggling to align some widgets in my tkinter project. I want the label at the top of the window to be center aligned and the combobox to be under that label and right aligned.

I've been trying for a little while, but still can't get it working. I would really appreciate pointers from anyone who can tell where I've gone wrong. I've attached my code and a screenshot to better illustrate the issue.

import sqlite3
import tkinter as tk
from tkinter import ttk

from ctypes import windll
windll.shcore.SetProcessDpiAwareness(1)

root = tk.Tk()

root.title("Solve Times Tracker")
root.configure(background="gray")
root.geometry("1200x800")

tk.Label(root, text="Solving time logger", font='ariel 17', anchor="w").grid(row=0,column=0,sticky="N")

options = ["one","two","three"]
combotext = tk.StringVar()
combotext.set('Please select your name')

select = ttk.Combobox(root, textvariable=combotext, state="readonly", font='ariel 17', width=25)

select.grid(row=1,column=1,sticky="E", pady=20)

select['values'] = ("one","two","three")


def callback_function(event):
    print('You selected:', combotext.get())

root.bind('<<ComboboxSelected>>', callback_function)



root.mainloop()

image explanation

Thanks.

ArduinoBen
  • 161
  • 1
  • 8
  • 1
    Your problem should be solved by using `.grid_columnconfigure` and assigning `weight`s appropriately. You can refer to [this](https://stackoverflow.com/questions/71171950/tkinter-how-to-align-widgets-to-the-right-independently-of-the-length-of-widgets/71172483#71172483) answer to understand better. – Sriram Srinivasan Apr 12 '22 at 11:35
  • Thanks, that helped with the drop-down menu, but my title label is still towards the left. I'm going to try to play around with it a little more. – ArduinoBen Apr 12 '22 at 12:06

1 Answers1

0

For your case, it is easier to use .pack() instead of grid():

tk.Label(root, text="Solving time logger", font='arial 17').pack()
...
select = ttk.Combobox(root, textvariable=combotext, state="readonly", font='arial 17', width=25)
select.pack(anchor='e', pady=20) # put at the right side
...

If you insist on using grid():

...
# make column 0 use all the horizontal space
root.columnconfigure(0, weight=1)

tk.Label(root, text="Solving time logger", font='arial 17').grid(row=0, column=0)
...
select = ttk.Combobox(root, textvariable=combotext, state="readonly", font='arial 17', width=25)
select.grid(row=1, column=0, sticky="e", pady=20)
...
acw1668
  • 40,144
  • 5
  • 22
  • 34