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

  • SEARCH
  • Home
  • 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 6826685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:10:37+00:00 2026-05-26T22:10:37+00:00

In Tkinter how can I correct this loop/function so that each button changes a

  • 0

In Tkinter how can I correct this loop/function so that each button changes a value to the value of the button?

This is a simplified version of my code, at the moment each button changes the value of size to 15 rather than the number on the button. I was wondering if there was anyway of fixing this loop without printing each individual button and value without a loop?

from Tkinter import *

size = 7

def AI():
    AIBoard = Tk()        
    AIBoard.title("Board Select")
    BoardSize = Label(AIBoard, text = "Please pick a board size: ", font = ('Helvetica',20))
    BoardSize.pack(side = 'top')

    for a in range(5,16,1):
        sizeBut = Button(AIBoard, text = a, width = 5, command = lambda: inputBoardSize(a))
        sizeBut.pack(side = 'left')

    AIBoard.mainloop()

def inputBoardSize(x):
    size = x
    print size

AI()

Thanx

  • 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-26T22:10:38+00:00Added an answer on May 26, 2026 at 10:10 pm

    Change your lambda to bind the value at the time the anonymous function is created.

    lambda a=a: inputBoardSize(a)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im running into this error that I can't work out Im writing some code
I am writing a Tkinter program that requires a loop. I can't run the
I'm currently trying to setup a GUI in Tkinter so that I can show
Something like this: from Tkinter import * root = Tk() but = Button(root, text
If i create a widget in Tkinter i can specify a widget name that
How can I bind arguments to a Python function so that I can call
Specs: Python2.7.1 Tkinter (Tk version 8.5) Windows7 IDLE 2.7.1 I'm coding a program that
I am having trouble building a Python function that launches TkInter objects, with commands
I know that Tkinter is just a thin layer over Tcl/Tk. Tkinter you can
Where can I find the most modern tutorial that teaches tkinter together with ttk

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.