473,387 Members | 1,441 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,387 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 6627
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.