This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 2 years ago.
I'm trying to make a program that hides and shows all my desktop files (mac) by executing defaults write com.apple.finder CreateDesktop false #or 'true' to show my files
killall Finder
I'm using tkinker to make a gui
but everytime I execute my code, it shows all my files(without me doing anything) then when I request it to hide all my files it did, but it doesn't show them again.
Code:
import os
import tkinter as tk
def hide():
os.system("defaults write com.apple.finder CreateDesktop false")
os.system("killall Finder")
def show():
os.system("defaults write com.apple.finder CreateDesktop true")
os.system("killall Finder")
root = tk.Tk()
frame = tk.Frame(root)
frame.pack()
button = tk.Button(frame,
text="Hide",
fg="red",
command=hide)
button.pack(side=tk.LEFT)
slogan = tk.Button(frame,
text="Show",
fg="blue",
command=show())
slogan.pack(side=tk.RIGHT)
root.mainloop()
Change slogan to below:
slogan = tk.Button(frame,text="Show",fg="blue",command=show)
When you say command=show(), you are calling the function and hence it gets executed without you clicking the button, so instead remove the paranthesis and it will solve the problem.
Hope it cleared your doubt, if any errors do let me know
Cheers
Related
This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed last year.
I've had this problem for a while. When I start a program, any tkinter buttons will call their functions without being pressed or anything. and after that, I cannot use the button anymore because it only works once for some reason. Can anybody help?
This is not the actual code, but it is an example:
from tkinter import *
root = Tk()
def function():
print("activated")
button = Button(root, text="this is a button", command=function)
button.pack()
root.mainloop()
Apparently adding parentheses at the end of the command inside the button code was the problem. credit to #jasonharper for this one.
This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 2 years ago.
In creating a Python Tkinter program, I wish to create a button that will close the program. I have tried the
#with master = Tk()
master.quit()
method. And it did absolutely nothing to my program - apart from stopping anything from working, although I received no Tracebacks.
The other method I have tried is:
#with master = Tk()
master.destroy()
This again did nothing to my program - it did give me a traceback error though which was:
_tkinter.TclError: can't invoke "button" command: application has been destroyed
My full code is:
from tkinter import *
master = Tk()
exitbutton = Button(master,text="Exit",(all the other personalization stuff here),command=(master.quit()))
#or I used master.destroy() in the command area.
exitbutton.grid(column=0,row=0)
None of the above methods have worked.
Many Thanks
(For the future)
You must pass the function's name rather than as a callable:
from tkinter import *
master = Tk()
exitbutton = Button(master,text="Exit",command=master.destroy)##dont include the parentheses
##or I used master.destroy() in the command area.
exitbutton.grid(column=0,row=0)
This should fix your problem.
Problem:
The only problem is that you are using parentheses() while passing the function(exit or destroy) to the Button as a command, Which causes it to be executed at the point where it is defined.
Solution:
The solution is to remove the parentheses() while passing the function(exit or destroy) to the Button as a command.
Fixed Code:
from tkinter import *
master = Tk()
exitbutton = Button(master, text="Exit", command=master.quit) # you can also use master.destroy
exitbutton.grid(column=0, row=0)
master.mainloop()
Tip:
As importing all(*) is not a good practice, you should import tkinter as tk
or as anything you want. The only change you will is to add tk. before each object belonging to tkinter.
Then your code will be as follows.
Best Practice:
import tkinter as tk
master = tk.Tk()
exitbutton = tk.Button(master, text="Exit", command=master.quit) # you can also use master.destroy
exitbutton.grid(column=0, row=0)
master.mainloop()
You want to pass a function object into the command keyword, so don't use parentheses. Also you should be using the destroy function for TKinter.
exitbutton = Button(master,text="Exit",(all the other personalization stuff here),command=master.destroy)
This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 2 years ago.
My message box launches automatically without me clicking the button first when I run it in Pycharm.
from tkinter import *
from PIL import ImageTk,Image
from tkinter import messagebox
root = Tk()
root.title('Frame')
root.iconbitmap('D:\Tkinter\Frame.ico')
def popup():
messagebox.showinfo("Popup","You have clicked a button!")
Button(root, text = 'Click Me!',commmand=popup()).pack()
root.mainloop()
And this is what I get when I run it
In Button declaration, you are calling your function instead of passing a callback to it.
There is also a typo in a word 'command' - you wrote it with 3x m.
So, you should declare your button as:
Button(root, text = 'Click Me!',command=popup).pack()
How to create a basic button
This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 7 years ago.
I have this code python, while running the code does not show any error but when Please click on the button does not show the result
from tkinter import *
root = Tk()
def cal():
l=[15,7,9,11]
s=0
for i in range(0,len(l)):
s=s+l[i]
return s
b = Button(root, text="ok", command=cal())
b.pack()
label = Label(root, text=cal())
label.pack()
root.mainloop()
You need to store a reference to the function command=cal instead of storing the function's return value command=cal().
This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 7 years ago.
I want to make a button that displays a label when clicked. Here was my thinking:
from Tkinter import *
def write_hello():
w = Label(root, text = "Hello. This is a label.")
w.pack()
root = Tk()
button = Button(root, text = "Hello!", command = write_hello() )
button.pack()
root.mainloop()
Can anyone be nice enough to walk me through this? I'm very new.
You're calling the write_hello function before you even create the button, so what you're probably seeing is the label showing up before the button on the UI (and without clicking it). What you want to do is pass the function to the Button constructor instead of the function's return value:
button = Button(root, text = "Hello!", command = write_hello)