+ 8

[SOLVED] A question to tkinter python

See actually I was trying to created a calculator with tkinter . My personal logic 1). I created 25 buttons 2). Each button which can be used in evel function ie. 1234567890+/*- is commnded with cal function. Means whenever any of these button is pressed cal function is called with a parameter of itself value as a string 3). This function append the value in a list every time it is called 4). The appended list is joined and shows to a screen . This is all what i wamt to did till now . But problem is that when the problem executes first time then automatically 11 times function runs each time for every buttons. And it print the content over label .. I dont need that actually. In my view it seems all right . See the code and run the code for better understanding . Have a look to code \/ https://code.sololearn.com/c84jeOHL0kL5/?ref=app [ SOLVED ! ]

27th Jun 2020, 1:34 AM
Ayush Kumar
Ayush Kumar - avatar
3 Answers
+ 4
This is because you're calling the functions when setting your command=func You just want the identifier without the parentheses. For the functions that you need to pass the arguments you can just use a lambda and call the function in the lambda with the arguments. Here is an example of each of the above. Button(root, command=ac, bg=o, text="AC", height=3, width=5).place(x=370, y=300) Button(root, command=lambda: cal("%"), bg=sb, text="%", height=3, width=5).place(x=550, y=300) Apply this principle to the rest of your buttons and retest. More info; https://www.delftstack.com/howto/JUMP_LINK__&&__python__&&__JUMP_LINK-tkinter/how-to-pass-arguments-to-tkinter-button-command/
27th Jun 2020, 2:25 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
Yup, that's correct. That link has some other work arounds for simple ways to set the function and pass arguments etc.
27th Jun 2020, 2:30 AM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Thanks ChaoticDawg I totally forgotten about it. That we can't pass a function with parenthesis directly over a command. Because it will call the function in that way . Thanku so much bro . đŸ€—đŸ€—đŸ€—đŸ€—
27th Jun 2020, 2:28 AM
Ayush Kumar
Ayush Kumar - avatar