473,244 Members | 1,353 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,244 software developers and data experts.

TkInter buttons - callback (command) function

It seems that in some cases the callback (command) function in a TkInter Button runs automatically when drawn for the first time. Example:
b=Button(frame, text="Click me",command=myFunction())
b.pack()
..
so, myFunction() executes when the screen is drawn for the first time.
But (!) if I do not include the "()" after myFunction, then it does not run until it is explicitly clicked. IE:
b=Button(frame, text="Click me", command=myFunction)
..
In this case myFunction does not run until clicked.
Is this correct behavior? It seems strange to me, and I do not find it documented anywhere.
(I am using a very recent version of python/tkinter on XP)
Thanks!
Erasmus
Sep 11 '07 #1
2 6615
oops. I see whats going on .. obviously: Python calls the function and returns the result to command. So is there any way to send parameters directly in a callback function?

It seems that in some cases the callback (command) function in a TkInter Button runs automatically when drawn for the first time. Example:
b=Button(frame, text="Click me",command=myFunction())
b.pack()
..
so, myFunction() executes when the screen is drawn for the first time.
But (!) if I do not include the "()" after myFunction, then it does not run until it is explicitly clicked. IE:
b=Button(frame, text="Click me", command=myFunction)
..
In this case myFunction does not run until clicked.
Is this correct behavior? It seems strange to me, and I do not find it documented anywhere.
(I am using a very recent version of python/tkinter on XP)
Thanks!
Erasmus
Sep 11 '07 #2
ilikepython
844 Expert 512MB
oops. I see whats going on .. obviously: Python calls the function and returns the result to command. So is there any way to send parameters directly in a callback function?
Do you mean like this:?
Expand|Select|Wrap|Line Numbers
  1. but = Button(text = "Press", command = lambda x = "blah": myFunction(x))
  2.  
Notice that the function doesn't get called right away, it is in the body of the lambda.
Sep 11 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Andrew Koenig | last post by:
from Tkinter import * class Application(Frame): def setcolor(self): self = "blue" def createWidgets(self): self.b1 = Button(self, bg = "red", command = self.setcolor) self.b1.place(height...
2
by: mksql | last post by:
New to Tkinter. Initially, I had some code that was executing button commands at creation, rather than waiting for user action. Some research here gave me a solution, but I am not sure why the...
1
by: midtoad | last post by:
I'm trying to display a GIF image in a label as the central area to a Tkinter GUI. The image does not appear, though a space is made for it. Why is this so? I notice that I can display a GIF...
8
by: dakman | last post by:
Recently, I have been needing to do this alot and I can never find a way around it, the main reason I want to do this is because for example in the application I am making right now, it creates a...
3
by: Jay | last post by:
I wold like to be able to generate buttons from a list in a file. How would I go about making each button have a different command, Lets say make the button print its label instead of print "." ...
1
by: Michael Yanowitz | last post by:
Hello: Below I have included a stripped down version of the GUI I am working on. It contains 2 dialog boxes - one main and one settings. It has the following problems, probably all related, that...
6
by: Eric_Dexter | last post by:
Instead of creating my buttons and waiting for me to press them to execute they are executing when I create them and won't do my callback when I press them.. thanks for any help in advance ...
2
by: Doran, Harold | last post by:
I am currently reading An Intro to Tkinter (1999) by F. Lundh. This doc was published in 1999 and I wonder if there is a more recent version. I've googled a bit and this version is the one I keep...
3
by: J-Burns | last post by:
Hello. Im a bit new to using Tkinter and im not a real pro in programming itself... :P. Need some help here. Problem 1: How do I make something appear on 2 separate windows using Tkinter? By...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.