Python: Buttons: Why are the commands always executed immediately?

Hello! I'm doing some testing with graphical programming. While playing around with buttons and menu bars, I noticed that commands were always executed immediately. This sometimes led to error messages or malfunctions.

Why is this happening and can it be stopped?

Example:

When I run this program, it prints "Hello World!" three times in the command line, but not when I press a button. Is this because one happens in the command line and the other in the window?

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
4 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
jo135
1 year ago
  1. Please don’t have any unsharp screen photos with the phone. Screenshot is better, even better copy the source code here.
  2. When you write a(), the function is called at this moment. So exactly when .add_command() is executed. What you want is to pass a function, not its return value. So it must be:
erstesmenu.add_command(label="Hi", command=a)
SanWU
1 year ago

fenster.geometry(“400×400”)

and

firstmenu.add_command(label=”Hi!!”, command=a())