Python Forum
[Tkinter] Aligning Python Tkinter Buttons In Rows
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Aligning Python Tkinter Buttons In Rows
#4
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() 
Reply


Messages In This Thread
RE: Aligning Python Tkinter Buttons In Rows - by woooee - Feb-22-2018, 02:56 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Different rows colours in treeview tkinter Krissstian 1 1,286 Nov-20-2022, 09:59 PM
Last Post: woooee
  tkinter toggle buttons not working Nu2Python 26 7,047 Jan-23-2022, 06:49 PM
Last Post: Nu2Python
  Column headers not aligning properly with table kenwatts275 3 3,537 Jul-13-2020, 12:53 AM
Last Post: menator01
  TkInter Binding Buttons ifigazsi 5 4,452 Apr-06-2020, 08:30 AM
Last Post: ifigazsi
  Issue on tkinter with buttons Reldaing 1 2,460 Jan-07-2020, 08:21 AM
Last Post: berckut72
  Need tkinter help with clicking buttons pythonprogrammer 2 2,484 Jan-03-2020, 04:43 AM
Last Post: joe_momma
  Tkinter Buttons action d3fi 1 2,019 Nov-20-2019, 09:16 PM
Last Post: Larz60+
  Nesting menu buttons tkinter for python Mocap 1 2,738 Jul-18-2019, 11:46 AM
Last Post: metulburr
  Windows GUI with push buttons to launch python scripts drifterf 7 4,225 Jul-17-2019, 05:34 PM
Last Post: Yoriz
  Multi windows in tkinter buttons not responding correctly curtjohn86 13 11,600 Jul-01-2017, 03:42 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020