How to detect text change of an entry in tkinter Python [duplicate] - python

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()

Related

How to know if an error has occurred in a tkinter app without a console? [duplicate]

This question already has answers here:
How can I make silent exceptions louder in tkinter?
(5 answers)
Closed 11 months ago.
I am working on tkinter project, which the user can't see the console. Is there a way the user can know an error has occurred in the program with out seeing the console.
messagebox.showerror()
You can use this code for showing error.
You can add try except statement from the beginning to end of your program, and in except you can use messagebox.showerror to show error.
And for catching the Tkinter error check this answer
But if you want to catch Python errors too for example if index out of range error occur while running a for loop you use
# Note import files before the try statement
from tkinter import *
from tkinter import messagebox
import sys,traceback
def show_error(slef, *args):
err = traceback.format_exception(*args)
messagebox.showerror('Exception',err)
try:
root=Tk()
Tk.report_callback_exception = show_error
exec(input()) # Just used to throw error
a=Button(text='s',command=lambda: print(8/0)) # Just used to throw error
a.pack()
root.mainloop()
except BaseException as e:
messagebox.showerror('Exception',e)

tkinter multi threading auto start [duplicate]

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()

Disabling a tkinter button returns nonetype error [duplicate]

This question already has an answer here:
Why do my Tkinter widgets get stored as None? [duplicate]
(1 answer)
Closed 3 years ago.
edit_bar=Tk()
self.entry_bn=Entry(edit_bar)
self.entry_bn.grid(row=0,column=1)
submit_bn=Button(edit_bar,text="Submit Business Name",command=self.business_name)\
.grid(row=1,column=2)
def business_name(self):
self.businessname=self.entry_bn.get()
submit_bn["state"]="disabled"
Hello,
I am trying to disable the button once the 'business name' has been input and the button clicked.
I am getting:
submit_bn["state"]="disabled"
'NoneType' object does not support item assignment
as my error.
Any ideas on why this may not be working would be great. I believe i have used the correct syntax for disabling a button, although there seems to be many alternatives.
Any help would be great and if you need the whole of my code please let me know.
Thanks
Code:
import tkinter as tk
edit_bar = tk.Tk()
entry_bn = tk.Entry(edit_bar)
entry_bn.grid(row=0, column=1)
def business_name():
businessname = entry_bn.get()
if len(businessname) > 0:
submit_bn.config(state='disabled')
submit_bn = tk.Button(edit_bar, text="Submit Business Name", command = business_name)
submit_bn.grid(row=1, column=1)
edit_bar.mainloop()
When you input a business name, its length should be always be greater than 0, and then disable the button.

Python/Tkinker Combobox event not launching [duplicate]

This question already has answers here:
How to pass an argument to event handler in tkinter?
(7 answers)
Closed 4 years ago.
I'm pulling my hair out and sure I'll be embarrassed by how simple my mistake is. I have created a Combobox which is supposed to launch a function each time is it selected. However nothing happens when you pick a different selection.
Here's the code:
acc_drop_box = ttk.Combobox(mainframe, textvariable=acc_value)
acc_drop_box['values'] = acc_list
acc_drop_box.grid(column=1, row=2, sticky=(W, E))
acc_drop_box.bind('<<ComboboxSelected>>', pick_acc(acc_value))
Right now the function "pick_acc" just prints the word "Hi!" for testing purposes. That happens once when I launch the program but not again no matter what I do. Thanks for your help!
You're giving the return of the callback as the reference as opposed to callback's reference. Replace:
acc_drop_box.bind('<<ComboboxSelected>>', pick_acc(acc_value))
with:
acc_drop_box.bind('<<ComboboxSelected>>', lambda var=acc_value: pick_acc(var))

Python: How to clear text entry when that entry is clicked? [duplicate]

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()

Categories