How to set the text/value/content of an `Entry` widget using a button in tkinter
You might want to use insert
method. You can find the documentation for the Tkinter Entry Widget here.
This script inserts a text into Entry
. The inserted text can be changed in command
parameter of the Button.
from tkinter import *
def set_text(text):
e.delete(0,END)
e.insert(0,text)
return
win = Tk()
e = Entry(win,width=10)
e.pack()
b1 = Button(win,text="animal",command=lambda:set_text("animal"))
b1.pack()
b2 = Button(win,text="plant",command=lambda:set_text("plant"))
b2.pack()
win.mainloop()
Using Tkinter while calling a function from button using commad to insert in entry widget suing function , it says entry variable is not defined
The problem can be fixed by defining the search_box
as global within the page()
function.
global w2, search_box # Added global definition for search_box
And by shifting the id()
function below the page function, it seems to be working as intended now.
The full code with these changes in place looks like -:
from tkinter import *
from tkinter import messagebox
root=Tk()
root.geometry('1000x700+122+1')
root.configure(bg='#8644f4')
#page after login
def page():
global w2, search_box # Added global definition for search_box
w2=Tk()
w2.geometry('1000x700+122+1')
w2.configure(bg='#8644f4')
# search and register for patient
search_PID = Label(w2, text="Patient ID", bg='#8644f4')
search_PID.place(x=10, y=20)
search_box =Entry(w2)
search_box.place(x=70, y=20)
getinfo = Button(w2, text='Search')
getinfo.place(x=100, y=50)
voice_button = Button(w2, text='Voice Search ', command=id)
voice_button.place(x=85, y=90)
register_button = Button(w2, text='Register')
register_button.place(x=95, y=130)
w2.title('Find And Register Patient Information')
w2.mainloop()
# shifted this function below
def id():
global search_box
search_box.insert(0,'Enter id') #here is the problem , the error says search_box is not defined
def logcheck():
uid = str(user_entry.get())
psw=str(pw_entry.get())
if uid =='a':
if psw=='a':
messagebox.showinfo('cool', 'login successful')
root.destroy()
page()
else:
messagebox.showinfo('Error', 'Password Wrong')
elif uid=='' and psw=='':
messagebox.showinfo('Error',"All Field are Required")
else:
messagebox.showinfo('Error', 'ID and Password Wrong')
#loginframe
logf=Frame(root,bg='#8081CD')
logf.place(x=250,y=150, height=340, width=500)
logintitle=Label(logf,text='Log in Panel',font=('impact',35,'bold'),bg='#8081CD', fg='darkblue')
logintitle.place(x=119,y=1)
username=Label(logf, text='Username',bg='lightblue')
username.place(x=135,y=110)
user_entry=Entry(logf)
user_entry.place(x=210,y=110)
password=Label(logf, text='Password',bg='lightblue')
password.place(x=135,y=140)
pw_entry=Entry(logf, bg='lightgray',show='*')
pw_entry.place(x=210,y=141)
login=Button(logf,text='Log in',bg='#8644f4',command=logcheck, cursor='hand2')
login.place(x=210,y=180)
root.title('Patient Information')
root.mainloop()
NOTE: Also, as id
is an existing keyword in python eventhough its not throwing any errors yet, it is suggested you change the name of your function perhaps to id_
or some other name, as id is also a built-in function, and defining another id function, overrides the built-in id
function, which may or may not cause errors down the development process. Even if there is no error it is generally a good practice to make the code more readable.
Make the button enabled when text is entered in Entry widget on Tkinter
One of the ways to achieve this is using StringVar
:
def capture(*args):
if e.get():
button['state'] = 'normal'
else:
button['state'] = 'disabled'
var = StringVar()
e = Entry(root, font = 20,borderwidth=5,textvariable=var)
e.pack()
var.trace('w',capture)
trace()
will call the provided callback each time the value of var
is changed.
The second way is to use bind
:
def capture():
if e.get():
button['state'] = 'normal'
else:
button['state'] = 'disabled'
e = Entry(root, font = 20,borderwidth=5)
e.pack()
e.bind('<KeyRelease>',lambda e: capture()) # Bind keyrelease to the function
With bind
, each time the key is released the function is called, whatever the key maybe. This might be a bit better because you are not creating a StringVar
just for the sake of using its trace
.
How to set default text for a Tkinter Entry widget
Use Entry.insert
. For example:
try:
from tkinter import * # Python 3.x
except Import Error:
from Tkinter import * # Python 2.x
root = Tk()
e = Entry(root)
e.insert(END, 'default text')
e.pack()
root.mainloop()
Or use textvariable
option:
try:
from tkinter import * # Python 3.x
except Import Error:
from Tkinter import * # Python 2.x
root = Tk()
v = StringVar(root, value='default text')
e = Entry(root, textvariable=v)
e.pack()
root.mainloop()
Tkinter entry widget input are written backwards
So the problem was that entered.insert(0, '+')
the 0 is where its going to place the + so every time you were pushing the button you were placing the 1 and the 2 and the + at position 0
from tkinter import *
root = Tk()
i= 0
def add():
global i
entered.insert(i, '+')
i += 1
def num_1():
global i
entered.insert(i, 1)
i += 1
def num_2():
global i
entered.insert(i, 2)
i += 1
entered = Entry(root)
entered.pack()
btn1 = Button(root, text='1', command=num_1).pack()
btn2 = Button(root, text='2', command=num_2).pack()
btn_add = Button(root, text=' +', command=add).pack()
root.mainloop()
so now you have the global i that will change the position of the placement...
Related Topics
Escape Special Characters in a Python String
How to Add a Qvideowidget in Qt Designer
How to Set Window Size in Selenium Chrome Python
Cannot Install Python 3.7 on Osx-Arm64
How to Find a Particular JSON Value by Key
Python Selenium Chrome Webdriver
How to Get "Python -M Venv" to Directly Install Latest Pip Version
Set Up Python on Windows to Not Type "Python" in Cmd