Tkinter spacing

Christian Gollwitzer auriocus at gmx.de
Wed Jan 27 01:48:49 EST 2016


Am 26.01.16 um 04:41 schrieb KP:
> If I want to have some space between, say, btn_last & btn_new, will
> I  have to use a dummy label in between these two or is there a better way?

If you just want to put space there, you can use the pad options of 
grid, just like you do for lbl_Recs. Specifically, if you want to put it 
on one side only, you'd do:

	btn_new.grid(column=4, row=0, padx="20 0")

which does 20px of space to the left of btn_new. Besides that, I have a 
few comments (I assume you are new to Tkinter and/or GUI programming in 
general)

> from tkinter import *
> from tkinter import ttk
>
> root = Tk()

> root.geometry("822x600+100+100")

This is very bad. The grid geometry manager calculates the needed space 
automatically. Your code fails, if you use a different ttk theme, or a 
different font size. For instance, on my OSX computer, the window is cut 
off at the right side at the Print button. Delete this line. As soon as 
you fill the bottom with content, the window will look reasonanble.

> nav_bar = ttk.Frame(root, borderwidth=2, relief='ridge', padding=(10, 3, 10, 3))
>
> btn_first  = ttk.Button(nav_bar, text='|<', width=4)  # for buttons showing text only, this will be text units (= average characters?)
> btn_prev   = ttk.Button(nav_bar, text='<',  width=4)  # for image buttons, it will be in pixels
> btn_next   = ttk.Button(nav_bar, text='>',  width=4)
> btn_last   = ttk.Button(nav_bar, text='>|', width=4)

If you want these buttons to look like a real Toolbur, there is a ttk 
widget class for that. Try:


btn_first  = ttk.Button(nav_bar, text='|<', style="Toolbutton")
btn_prev   = ttk.Button(nav_bar, text='<',  style="Toolbutton")
btn_next   = ttk.Button(nav_bar, text='>',  style="Toolbutton")
btn_last   = ttk.Button(nav_bar, text='>|', style="Toolbutton")


Also note that I left off the manual width. Probably you wwant to use 
images in the end, and if these all have the same size, which is usual 
for an icon set (32x32 or 48x48, e.g.) then the size of all the buttons 
will look good. You shouldn't have to set size values manually.

> btn_new    = ttk.Button(nav_bar, text='New')
> btn_edit   = ttk.Button(nav_bar, text='Edit')
> btn_delete = ttk.Button(nav_bar, text='Delete')
> btn_cancel = ttk.Button(nav_bar, text='Cancel')
> btn_print  = ttk.Button(nav_bar, text='Print')
> btn_help   = ttk.Button(nav_bar, text='Help')
> btn_save   = ttk.Button(nav_bar, text='Save')
> lbl_Recs   = ttk.Label(nav_bar,  text='Records')


> lbl_RCount = ttk.Label(nav_bar,  text='0 ', width=10, borderwidth=2, relief='sunken', anchor='e')  # fake entry look

This will also look correct only with some themes. Maybe you want a 
readonly or disabled entry? You can do this like

sometkvar=Tk.StringVar()
lbl_RCount = ttk.Entry(nav_bar,  textvariable=sometkvar, width=10, 
state='readonly')
sometkvar.set('0 ')



> nav_bar.grid(column=0, row=0, columnspan=13)

There is no need to give the nav_bar a columnspan. So far you have no 
elements in the main frame besides the nav_bar. Columnspan=13 would mean 
that the navbar should be the same size as 13 columns below it. You 
don't tell it the number of children (your buttons) here. So for the 
moment just

	nav_bar.grid(column=0, row=0)

HTH,

	Christian





More information about the Python-list mailing list