This question already has answers here:
Entry box text clear when pressed Tkinter
(2 answers)
Closed 7 years ago.
How can I clear a tkinter entry when being clicked?
I tried putting command like button but its not working.
self.entry1= Entry(self.mw,width=25,text=str1,justify=RIGHT,fg="red")
self.entry1.insert(INSERT, "type here..")
self.entry1.pack()
All you need to do is bind the entry to a function that uses the delete function.
def clear(event):
self.entry1.delete(0, END)
self.entry1= Entry(self.mw,width=25,text=str1,justify=RIGHT,fg="red")
self.entry1.insert(INSERT, "type here..")
self.entry1.bind('<Button-1>', clear())
self.entry1.pack()
Related
This question already has answers here:
How do I get an event callback when a Tkinter Entry widget is modified?
(9 answers)
Closed 5 months ago.
How can detect that a user entering characters in tkinter entry ?
I want to calculate the total cost from 2 different entry. here is my code but does not work!
from tkinter import *
root=Tk()
def calculate_total_cost(event):
if count_ent.get().isdigit() and unit_cost_ent.get().isdigit():
total_cost=int(count_ent.get())*int(unit_cost_ent.get())
print(total_cost)
count_ent=Entry(root).pack()
unit_cost_ent=Entry(root).pack()
unit_cost_ent.bind("<key>",calculate_total_cost)
Please check this, insert value in both entry and press enter, you will get the results. Although your questions is also not cleared, but from your statement I decided that you are facing issue of "AttributeError: 'NoneType' object has no attribute 'bind'"...
By executing the below code, I hope you will get your answer. First execute this simple program, you will get the desired output in terminal.
from tkinter import *
root=Tk()
def calculate_total_cost(event):
if count_ent.get().isdigit() and unit_cost_ent.get().isdigit():
total_cost=int(count_ent.get())*int(unit_cost_ent.get())
print(total_cost)
count_ent=Entry(root)
count_ent.pack()
# count_ent.insert(0, value1)
unit_cost_ent=Entry(root)
unit_cost_ent.pack()
# unit_cost_ent.insert(0, value2)
unit_cost_ent.bind("<Return>",calculate_total_cost)
root.mainloop()
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 1 year ago.
So in my tkinter app I need to check input on button but when I start the program with this code it starts the function (without me clicking the button) and I have no idea why it does that is.
submit = tk.Button(app,text='Submit details',bg='black',fg='white',
command=threading.Thread(target=get_input_info).start()).grid(row=4)
You need to remove Parenthesis -
command=threading.Thread(target=get_input_info).start
Or use, lambda (useful when you need to pass args) -
command=lambda:threading.Thread(target=get_input_info).start()
Just remove the pair of parantheis from the end of the command argument.
eg:
from tkinter import *
import threading
def hehe():
print("some stuff")
win=Tk()
submit = Button(text="something", command=threading.Thread(target=hehe).start).pack()
win.mainloop()
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().