Python Forum

Full Version: Aligning Python Tkinter Buttons In Rows
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to make a program in Tkinter where you can press a button and it will add it to a list. I want to align the buttons with **No** text in the box **At All**. I want them in 6 rows of 6, then the 7th row with two in the center. Do you know how I can do this?

My Current Code Is:

from tkinter import *

result = []

def printa():
    result.append('a')
def printb():
    result.append('b')
def printc():
    result.append('c')
def printd():
    result.append('d')
def printe():
    result.append('e')
def printf():
    result.append('f')
def printg():
    result.append('g')
def printh():
    result.append('h')
def printi():
    result.append('i')
def printj():
    result.append('j')
def printk():
    result.append('k')
def printl():
    result.append('l')
def printm():
    result.append('m')
def printn():
    result.append('n')
def printo():
    result.append('o')
def printp():
    result.append('p')
def printq():
    result.append('q')
def printr():
    result.append('r')
def prints():
    result.append('s')
def printt():
    result.append('t')
def printu():
    result.append('u')
def printv():
    result.append('v')
def printw():
    result.append('w')
def printx():
    result.append('x')
def printy():
    result.append('y')
def printz():
    result.append('z')
def printspace():
    result.append('space')
def printperiod():
    result.append('period')
def print0():
    result.append('0')
def print1():
    result.append('1')
def print2():
    result.append('2')
def print3():
    result.append('3')
def print4():
    result.append('4')
def print5():
    result.append('5')
def print6():
    result.append('6')
def print7():
    result.append('7')
def print8():
    result.append('8')
def print9():
    result.append('9')
def printall():
    print(''.join(result))
def clear():
    result = []


root=Tk()

ba=Button(root,command=printa)
bb=Button(root,command=printb)
bc=Button(root,command=printc)
bd=Button(root,command=printd)
be=Button(root,command=printe)
bf=Button(root,command=printf)
bg=Button(root,command=printg)
bh=Button(root,command=printh)
bi=Button(root,command=printi)
bj=Button(root,command=printj)
bk=Button(root,command=printk)
bl=Button(root,command=printl)
bm=Button(root,command=printm)
bn=Button(root,command=printn)
bo=Button(root,command=printo)
bp=Button(root,command=printp)
bq=Button(root,command=printq)
br=Button(root,command=printr)
bs=Button(root,command=prints)
bt=Button(root,command=printt)
bu=Button(root,command=printu)
bv=Button(root,command=printv)
bw=Button(root,command=printw)
bx=Button(root,command=printx)
by=Button(root,command=printy)
bz=Button(root,command=printz)
bspace=Button(root,command=printspace)
bperiod=Button(root,command=printperiod)
b0=Button(root,command=print0)
b1=Button(root,command=print1)
b2=Button(root,command=print2)
b3=Button(root,command=print3)
b4=Button(root,command=print4)
b5=Button(root,command=print5)
b6=Button(root,command=print6)
b7=Button(root,command=print7)
b8=Button(root,command=print8)
b9=Button(root,command=print9)
ok=Button(root,text='Print Result',command=printall)
clear=Button(root,text='Clear',command=clear)

photoa=PhotoImage(file='Symbols\\a.png')
photob=PhotoImage(file='Symbols\\b.png')
photoc=PhotoImage(file='Symbols\\c.png')
photod=PhotoImage(file='Symbols\\d.png')
photoe=PhotoImage(file='Symbols\\e.png')
photof=PhotoImage(file='Symbols\\f.png')
photog=PhotoImage(file='Symbols\\g.png')
photoh=PhotoImage(file='Symbols\\h.png')
photoi=PhotoImage(file='Symbols\\i.png')
photoj=PhotoImage(file='Symbols\\j.png')
photok=PhotoImage(file='Symbols\\k.png')
photol=PhotoImage(file='Symbols\\l.png')
photom=PhotoImage(file='Symbols\\m.png')
photon=PhotoImage(file='Symbols\\n.png')
photoo=PhotoImage(file='Symbols\\o.png')
photop=PhotoImage(file='Symbols\\p.png')
photoq=PhotoImage(file='Symbols\\q.png')
photor=PhotoImage(file='Symbols\\r.png')
photos=PhotoImage(file='Symbols\\s.png')
photot=PhotoImage(file='Symbols\\t.png')
photou=PhotoImage(file='Symbols\\u.png')
photov=PhotoImage(file='Symbols\\v.png')
photow=PhotoImage(file='Symbols\\w.png')
photox=PhotoImage(file='Symbols\\x.png')
photoy=PhotoImage(file='Symbols\\y.png')
photoz=PhotoImage(file='Symbols\\z.png')
photospace=PhotoImage(file='Symbols\\space.png')
photoperiod=PhotoImage(file='Symbols\\period.png')
photo0=PhotoImage(file='Symbols\\0.png')
photo1=PhotoImage(file='Symbols\\1.png')
photo2=PhotoImage(file='Symbols\\2.png')
photo3=PhotoImage(file='Symbols\\3.png')
photo4=PhotoImage(file='Symbols\\4.png')
photo5=PhotoImage(file='Symbols\\5.png')
photo6=PhotoImage(file='Symbols\\6.png')
photo7=PhotoImage(file='Symbols\\7.png')
photo8=PhotoImage(file='Symbols\\8.png')
photo9=PhotoImage(file='Symbols\\9.png')

ba.config(image=photoa)
bb.config(image=photob)
bc.config(image=photoc)
bd.config(image=photod)
be.config(image=photoe)
bf.config(image=photof)
bg.config(image=photog)
bh.config(image=photoh)
bi.config(image=photoi)
bj.config(image=photoj)
bk.config(image=photok)
bl.config(image=photol)
bm.config(image=photom)
bn.config(image=photon)
bo.config(image=photoo)
bp.config(image=photop)
bq.config(image=photoq)
br.config(image=photor)
bs.config(image=photos)
bt.config(image=photot)
bu.config(image=photou)
bv.config(image=photov)
bw.config(image=photow)
bx.config(image=photox)
by.config(image=photoy)
bz.config(image=photoz)
bspace.config(image=photospace)
bperiod.config(image=photoperiod)
b0.config(image=photo0)
b1.config(image=photo1)
b2.config(image=photo2)
b3.config(image=photo3)
b4.config(image=photo4)
b5.config(image=photo5)
b6.config(image=photo6)
b7.config(image=photo7)
b8.config(image=photo8)
b9.config(image=photo9)


ba.pack()
bb.pack()
bc.pack()
bd.pack()
be.pack()
bf.pack()
bg.pack()
bh.pack()
bi.pack()
bj.pack()
bk.pack()
bl.pack()
bm.pack()
bn.pack()
bo.pack()
bp.pack()
bq.pack()
br.pack()
bs.pack()
bt.pack()
bu.pack()
bv.pack()
bw.pack()
bx.pack()
by.pack()
bz.pack()
bspace.pack()
bperiod.pack()
b0.pack()
b1.pack()
b2.pack()
b3.pack()
b4.pack()
b5.pack()
b6.pack()
b7.pack()
b8.pack()
b9.pack()
ok.pack()
clear.pack()

root.mainloop()
And every button just appears in one huge list going down.

N.B: I would like to avoid classes if possible, don't worry if it can't be done without.
This doesn't answer your question, but seeing the path you've taken so far makes me want to help with something else. When you see large chunks of code that are identical in *almost* every way, you can normally write a single function that handles both cases. In this instance, one function instead of 28:
from tkinter import *

result = []

def add_to_result(value):
    result.append(value)

def clear_results():
    result = []

def handler(value):
    return lambda: add_to_results(value)

root = Tk()

characters = 'abcdefghijklmnopqrstuvwxyz'
for char in characters:
    btn = Button(root, command=handler(char))
    photo = PhotoImage(file="Symbols/{0}.png".format(char))
    btn.config(image=photo)
    btn.pack()
Use partial to send values to a function from a button command. And please don't get in the habit of using wildcard imports as you can't tell which variable or function comes from which name-space.
import sys
if 3 == sys.version_info[0]:  ## 3.X is default if dual system
    import tkinter as tk     ## Python 3.x
else:
    import Tkinter as tk     ## Python 2.x

from functools import partial

result = []
 
def add_to_result(value):
    result.append(value)
 
def clear_results():
    result = []
 
def handler(value):
    print("letter =", value)
    add_to_result(value)
 
root = tk.Tk()
 
characters = 'abcdefghijklmnopqrstuvwxyz'
for char in characters:
    btn = tk.Button(root, command=partial(handler, char))
    photo = tk.PhotoImage(file="Symbols/{0}.png".format(char))
    btn.config(image=photo)
    btn.pack() 

Use partial to send values to a function from a button command. And please don't get in the habit of using wildcard imports as you can't tell which variable or function comes from which name-space.
import sys
if 3 == sys.version_info[0]:  ## 3.X is default if dual system
    import tkinter as tk     ## Python 3.x
else:
    import Tkinter as tk     ## Python 2.x

from functools import partial
result = []
 
def add_to_result(value):
    result.append(value)
 
def clear_results():
    result = []
 
def handler(value):
    add_to_result(value)
 
root = tk.Tk()
 
characters = 'abcdefghijklmnopqrstuvwxyz'
current_row=0
    btn = tk.Button(root, text=char, width=10,
                    command=partial(handler, char))
##    photo = tk.PhotoImage(file="Symbols/{0}.png".format(char))
##    btn.config(image=photo)
    btn.pack()
 
root.mainloop()