Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6350231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T21:46:19+00:00 2026-05-24T21:46:19+00:00

I want to add 10 buttons to Tkinter, named One to Ten. I basically

  • 0

I want to add 10 buttons to Tkinter, named One to Ten. I basically just used the brute force method, adding each button as I went, in the init function of my application’s class. It works, but I want to minimize the code used, to be more efficient, such as using a data structure to hold all the buttons.

I was thinking of using a buttonBox to hold all the buttons in, but I was not sure if I could manipulate the placement via grid() to place the buttons how I wanted.

self.one = Button(frame, text="One", command=self.callback)
self.one.grid(sticky=W+E+N+S, padx=1, pady=1)

self.two = Button(frame, text="Two", command=self.callback)
self.two.grid(sticky=W+E+N+S, row=0, column=1, padx=1, pady=1)

self.three = Button(frame, text="Three", command=self.callback)
self.three.grid(sticky=W+E+N+S, row=0, column=2, padx=1, pady=1)

# ...

self.ten = Button(frame, text="Ten", command=self.callback)
self.ten.grid(sticky=W+E+N+S, row=1, column=4, padx=1, pady=1)

Can anyone show me a way to make this more efficient, such as a data structure?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T21:46:20+00:00Added an answer on May 24, 2026 at 9:46 pm

    Instead of naming the buttons self.one, self.two, etc., it would be more convenient to refer to them by indexing a list, such as self.button.

    If the buttons do different things, then you just have to explicitly
    associate buttons with callbacks. For example:

    name_callbacks=(('One',self.callback_one),
                    ('Two',self.callback_two),
                    ...,
                    ('Ten',self.callback_ten))
    self.button=[]
    for i,(name,callback) in enumerate(name_callbacks):
        self.button.append(Button(frame, text=name, command=callback))
        row,col=divmod(i,5)
        self.button[i].grid(sticky=W+E+N+S, row=row, column=col, padx=1, pady=1)
    

    If the buttons all do similar things, then one callback may suffice to service them all. Since the callback itself can’t take arguments, you can setup a callback factory to pass the arguments in through a closure:

    def callback(self,i): # This is the callback factory. Calling it returns a function.
        def _callback():
            print(i) # i tells you which button has been pressed.
        return _callback
    
    def __init__(self):
        names=('One','Two','Three','Four','Five','Six','Seven','Eight','Nine','Ten')
        self.button=[]
        for i,name in enumerate(names):
            self.button.append(Button(frame, text=name, command=self.callback(i+1)))
            row,col=divmod(i,5)
            self.button[i].grid(sticky=W+E+N+S, row=row, column=col, padx=1, pady=1)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to add buttons one below the other. I have this simple code:
I want to add three buttons on a web form. Each button has its
I want to add buttons in one of columns in the grid. I try
For example I want to add 3 buttons on screen: one align left, one
I have been developing an Android RSS news reader.I want to add two buttons
I have a loop displaying records, and I want to add submit buttons to
I tried to add buttons, textboxes and text to tableview. I want the row
I want to add haptic feedback to my application's buttons and control them programmatically
In my application I want to add a simple animation to buttons and other
I am using C#.net I want to add custom edit/delete buttons to my GridView1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.