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 8707253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:49:29+00:00 2026-06-13T03:49:29+00:00

I am writing a program with a lot of buttons(over 100), and each one

  • 0

I am writing a program with a lot of buttons(over 100), and each one needs a unique result, but all of the results are similar, this is the code of the first button

box1= 'filepath to text file'
def openfile(filename):
    filetxt = (open(filename,"r").read())
    return filetxt
    var.set(filetxt)

def Box1():
    var.set(openfile(box1))

openfile(box1)
window1 = Tk()
window1.geometry('450x450')

var = StringVar()

Button1 = Button(donut,text = "Box #1", command= Box1 )
Button1.pack()

each button will do the same thing, but access a different file, is there a more efficient way to do this than simply writing a unique callback function for every button?

  • 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-06-13T03:49:31+00:00Added an answer on June 13, 2026 at 3:49 am

    Usually, you’d have the files in a list:

    list_of_files = ...
    

    Then you would create a function to make a button which opens a file input:

    def file_open_button(filename):
        b = Button(donut, text = 'open {0}'.format(filename), command = lambda: openfile(filename))
        return b
    

    Now iterate over your list of files and create the buttons, packing as you go:

    for f in list_of_files:
        button = file_open_button(f)
        button.pack()
    

    Perhaps the thing you’re missing is an understanding of lambda (anonymous) functions. a lambda function is much like a regular function:

    def foo(x):
        return x*x
    
    bar = lambda x: x*x
    

    The above statements are very similar. e.g. foo(x) == bar(x) will always be True.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a program that involves a ton of images, over 100 per screen
I'm writing a program that needs to do a lot of string formatting and
I am writing a program to communicate over the serial port. All data that
I am writing a small computation program with lot of read operations on blob
I am currently writing a program using Weka that builds a model (using one
I'm writing a Python program with a lot of file access. It's running surprisingly
OK, the title is a bit(lot) cryptic, but that's the best one-line-summary I could
I am writing a program in C++ which involves parsing a lot of text
Writing a simple program that will find exact duplicate files on my computer, but
I'm writing a program with a lot of enumerations, and I'm having to return

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.