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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:50:08+00:00 2026-05-31T19:50:08+00:00

I am trying to figure out exactly how to implement a callback function which

  • 0

I am trying to figure out exactly how to implement a callback function which does something more meaningful than just print output. I am fairly inexperienced, so I am not sure how callback functions should or can be implemented in Python (or in any other language, for that matter).

Consider the following Python code:

from Tkinter import *

def callbackfunc(*args):
  print "Hello World!"

class App:

  def __init__(self, master):
    frame = Frame(master)
    frame.pack()

    optionvalue = IntVar(master)
    optionvalue.set(2)
    optionvalue.trace("w", callbackfunc)
    self.optionmenu = OptionMenu(master, optionvalue, 1, 2, 3, 4)
    self.optionmenu.pack()

I am trying to implement an OptionMenu (a Tkinter widget) such that when its selected value is changed, my callback function does something meaningful—more specifically, it will change a global variable value, defined somewhere else in the program. As it is implemented above, it simply prints output (albeit successfully).

I cannot figure out how to pass parameters to my callback function. I do not want this particular callback function to return anything; however, I am curious as to how I would make my callback function return something, and how I would implement the rest of my program so that it could utilize those returned results, whatever those might be. Am I trying to implement a Python callback function in a way in which it was not intended to be implemented? If not, how do I make this one work?

  • 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-31T19:50:10+00:00Added an answer on May 31, 2026 at 7:50 pm

    It’s a little bit unclear what you mean by “pass parameters to my callback function.” You’re already doing that! For example:

    from Tkinter import *
    
    def callbackfunc(*args, **kwargs):
        print args, kwargs
        print "Hello World!"
    
    class App(object):
        def __init__(self, master):
            frame = Frame(master)
            frame.pack()
    
            optionvalue = IntVar(master)
            optionvalue.set(2)
            optionvalue.trace("w", callbackfunc)
            self.optionmenu = OptionMenu(master, optionvalue, 1, 2, 3, 4)
            self.optionmenu.pack()
    
    root = Tk()
    app = App(root)
    root.mainloop()
    

    When run…

    $ python foo.py 
    ('PY_VAR0', '', 'w') {}
    Hello World!
    

    So you see, when Tkinter calls your callback, it passes parameters to it. If you wanted to do something other than print them, you could store them in some state, by passing a method instead of a function.

    from Tkinter import *
    
    class App(object):
        def __init__(self, master):
            frame = Frame(master)
            frame.pack()
    
            optionvalue = IntVar(master)
            optionvalue.set(2)
            optionvalue.trace("w", self.callbackfunc)
            self.optionmenu = OptionMenu(master, optionvalue, 1, 2, 3, 4)
            self.optionmenu.pack()
            self.state = []
    
        def callbackfunc(self, *args):
            self.state.append(args)
            print self.state
    
    
    root = Tk()
    app = App(root)
    root.mainloop()
    

    When run…

    $ python foo.py 
    [('PY_VAR0', '', 'w')]
    [('PY_VAR0', '', 'w'), ('PY_VAR0', '', 'w')]
    [('PY_VAR0', '', 'w'), ('PY_VAR0', '', 'w'), ('PY_VAR0', '', 'w')]
    

    Also, perhaps you want to access the value of optionvalue. You could save a reference to it then:

    from Tkinter import *
    
    class App(object):
        def __init__(self, master):
            frame = Frame(master)
            frame.pack()
    
            self.optionvalue = IntVar(master)
            self.optionvalue.set(2)
            self.optionvalue.trace("w", self.callbackfunc)
            self.optionmenu = OptionMenu(master, self.optionvalue, 1, 2, 3, 4)
            self.optionmenu.pack()
            self.state = []
    
        def callbackfunc(self, *args):
            self.state.append(args)
            print self.state
            print self.optionvalue.get()
    
    
    root = Tk()
    app = App(root)
    root.mainloop()
    

    When run…

    $ python foo.py 
    [('PY_VAR0', '', 'w')]
    1
    [('PY_VAR0', '', 'w'), ('PY_VAR0', '', 'w')]
    2
    [('PY_VAR0', '', 'w'), ('PY_VAR0', '', 'w'), ('PY_VAR0', '', 'w')]
    3
    

    You could also use root.getvar(name) with name = 'PY_VAR0' (the first arg passed to the callback), as noob oddy suggests.

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

Sidebar

Related Questions

I've been trying to figure out what exactly is happening here. I'm just trying
I am trying to figure out how exactly does treesort from here work (I
I am trying to figure out exactly what these new fangled data stores such
I'm trying to figure out what exactly Dependency Properties are, but when I look
Trying to figure out which to use.
Trying to figure out the best way to do this: Should I do something
I am trying to figure out how exactly Wordpress works around blog posts so
I am trying to figure out implement a Compose New Message view that works
I'm trying to figure out exactly what happens when you link to a same-domain
I'm still very new to rails and I'm trying to figure out what exactly

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.