Home > front end >  Tkinter button placement (best practice)
Tkinter button placement (best practice)

Time:07-07

I have written some tkinter code which places a few checkbuttons into separate frames. It has two issues:

  1. It is very unelegant! Any suggestions to do it better?
  2. The frames do not have the same size. I tried to fix the size of the frames with the arguments width and height instead using padding but this does not work.
import tkinter as tk
from tkinter import *

root = tk.Tk()

frames = [tk.Frame(master=root, relief = RAISED ,padx = 20, pady = 20, borderwidth = 2) for _ in range(16)]

frames[0].grid(row = 0, column = 0)
frames[1].grid(row = 0, column = 1)
frames[2].grid(row = 0, column = 2)
frames[3].grid(row = 0, column = 3)
frames[4].grid(row = 1, column = 0)
frames[5].grid(row = 1, column = 1)
frames[6].grid(row = 1, column = 2)
frames[7].grid(row = 1, column = 3)
frames[8].grid(row = 0, column = 4)
frames[9].grid(row = 0, column = 5)
frames[10].grid(row = 0, column = 6)
frames[11].grid(row = 0, column = 7)
frames[12].grid(row = 1, column = 4)
frames[13].grid(row = 1, column = 5)
frames[14].grid(row = 1, column = 6)
frames[15].grid(row = 1, column = 7)

Checkbuttons = {}
checkbuttonBooleans = {}
placementIndex = [0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,11,11,11,11,12,12,12,12,13,13,13,13,14,14,14,14,15,15,15,15]

i = 0
for channel in range(64):
    checkbuttonBooleans[channel] = tk.BooleanVar()
    Checkbuttons[channel] = tk.Checkbutton(master = frames[placementIndex[channel]], text = str(channel), variable = checkbuttonBooleans[channel]).grid(row = 0   i, column = 0 , sticky = W)
    i  = 1

    if i==4:
        i = 0

root.mainloop()

CodePudding user response:

I will answer the question on the frames not being the same size as that is more objective. When using the grid layout you can use the sticky keyword to tell the widget what sides of the grid the widget should stick to. For example, sticky='nw' would stick to the top left corner and use the space the it requires, which may not be the full grid cell. If you use sticky='news' (for north,south,east, and west - order doesn't matter), then the widget will fill the grid. So, simply add the keyword argument sticky='news' to each grid layout as shown below.

frames[0] .grid(row = 0, column = 0, sticky='news')
frames[1] .grid(row = 0, column = 1, sticky='news')
frames[2] .grid(row = 0, column = 2, sticky='news')
frames[3] .grid(row = 0, column = 3, sticky='news')
frames[4] .grid(row = 1, column = 0, sticky='news')
frames[5] .grid(row = 1, column = 1, sticky='news')
frames[6] .grid(row = 1, column = 2, sticky='news')
frames[7] .grid(row = 1, column = 3, sticky='news')
frames[8] .grid(row = 0, column = 4, sticky='news')
frames[9] .grid(row = 0, column = 5, sticky='news')
frames[10].grid(row = 0, column = 6, sticky='news')
frames[11].grid(row = 0, column = 7, sticky='news')
frames[12].grid(row = 1, column = 4, sticky='news')
frames[13].grid(row = 1, column = 5, sticky='news')
frames[14].grid(row = 1, column = 6, sticky='news')
frames[15].grid(row = 1, column = 7, sticky='news')
  • Related