TonyIT
TonyIT

Reputation: 115

Alignment of the widgets in Python Tkinter

I'm going crazy with this stupid problem. As you can see in running this simple code, the "combobox" widget is not in line with the other "entry" widgets. Where am I wrong?

from tkinter import *
from tkinter import ttk

root = Tk()

frame = Frame(root)
frame.grid()

x = Label(frame, text="alpha", width = 8, anchor = W)
x.grid(row=1, columnspan=1)
x = Entry(frame,  width = 24)
x.grid(row=1, column=2, columnspan=2,  sticky=W)

x = Label(frame, text="beta", width = 8, anchor = W)
x.grid(row=2, columnspan=1)
x = Entry(frame,  width = 24)
x.grid(row=2, column=2, columnspan=2,  sticky=W)

x = Label(frame, text="gamma", width = 8, anchor = W)
x.grid(row=3, columnspan=1)
x = Entry(frame, width = 7, justify = 'center')
x.grid(row=3, column=2, columnspan=1, sticky=W)
#x = ttk.Combobox(frame, width = 4, justify = "center")
#x.grid(row=3, column=3, columnspan=1, sticky=W)

x = Label(frame, text="delta", width = 8, anchor = W)
x.grid(row=4, columnspan=1)
x = ttk.Combobox(frame, width=20)
x.grid(row=4, column=2, columnspan=2,  sticky=W)

x = Label(frame, text="epsilon", width = 8, anchor = W)
x.grid(row=5, columnspan=1)
x = Entry(frame,  width = 24)
x.grid(row=5, column=2, columnspan=2,  sticky=W)

mainloop()

enter image description here

Upvotes: 0

Views: 1788

Answers (1)

Ron Norris
Ron Norris

Reputation: 2690

Not sure why it's doing that. It displays differently on Windows vs Linux. Never had that problem. What you can do is within the combobox grid statement, add padx=1 when a Linux installation is detected, if Windows, no padx if you need multi-platform compatibility. I know this is a workaround, but the fundamental GUI behavior is different between the two systems.

import platform
from tkinter import *
from tkinter import ttk

os_info = platform.platform()
....

if os_info.startswith('Linux'):
    x.grid(row=3, column=2, columnspan=1, sticky=W, padx=1)
elif os_info.startswith('Windows'):
    x.grid(row=3, column=2, columnspan=1, sticky=W)
...

Upvotes: 1

Related Questions