This question already has an answer here:
Basic query regarding bindtags in tkinter
(1 answer)
Closed 10 months ago.
HI guys i'm trying to simply create a text space that replaces the letters or any input into asterisks just like happen with the regular passwords. But why if i bind any key to the function "text" if i press any key the first input is empty? Becuase if i type "m" when in the function i print(e.get()) the output is empty in the console? Why it ignores the input and how to simply show the asterisk in the text area and bind the normal password to the variable "a" just like in my code? With e.delete and e.insert i simply cut the last character and replace it with asterisk here is my code
from tkinter import *
a = ""
window = Tk()
def entra():
global a
if a == "cgf":
print ("a")
else:
print("noo")
def text (event):
global a
a = a + e.get()
print(e.get())
e.delete(len(e.get()) - 1)
e.insert(END, "*")
e = Entry(window, borderwidth=3, width=50, bg="white", fg="black")
e.place(x=100, y=35)
e.bind("<Key>", text)
bottone_entra = Button (window, text = "Entra", borderwidth=2.5,command = entra)
bottone_entra.place (x=100, y=65)
etichetta = Label(window, text = "PASSWORD", bg = "#2B2B2B", fg = "white")
etichetta.place(x=97.5, y=10)
window.geometry ("500x500")
window.title ("Password")
window.configure(bg = "#2B2B2B")
window.mainloop()
You don't have to manually build a logic for it, tkinter luckily already provides it. You just have to set an extra option to the entry widget, show='*':
e = Entry(window, borderwidth=3, width=50, bg="white", fg="black", show='*')
There are many reasons as to why you should avoid building/using this logic of your own, to begin with, once you do get(), you will only get **** and not whatever the text originally was.
Related
I'm trying to create a function in tkinter where I can print out what the user writes in a Entry box. I'm able to print out ask_an_entry_get, but when I try to print what_is_answer_entry_get
, I get nothing my empty spaces.
Please find out the problem here. Also I'm using the Entry widget, along with the get() function, to get input from the user.
def answer_quizmaker_score():
print(ask_an_entry_get)
print(what_is_answer_entry_get)
I made a lot of global variables so I could use them all around my code.
global what_is_answer_entry
what_is_answer_entry = Entry(root4)
what_is_answer_entry.pack()
I then used the get() function to retrieve what the user typed.
global what_is_answer_entry_get
what_is_answer_entry_get = what_is_answer_entry.get()
This is the exact process I did for both ask_an_entry_get and what_is_answer_entry_get. However for some reason only ask_an_entry_get is printed, while what_is_answer_entry_get is printing nothing in the console.
from tkinter import *
root = Tk()
root.geometry("500x500")
txt1 = StringVar()
txt2 = StringVar()
def txt_printer():
print(txt1.get())
print(txt2.get())
x = Entry(root, textvariable=txt1, width=20)
x.place(x=0, y=0)
y = Entry(root, textvariable=txt2, width=20)
y.place(x=0, y=50)
btn_print = Button(root, text="print", command=txt_printer)
btn_print.place(x=0, y=100)
# Or if you want to show the txt on window then:
def txt_on_window():
lb1 = Label(root, text=txt1.get())
lb1.place(x=0, y=200)
lb2 = Label(root, text=txt2.get())
lb2.place(x=0, y=235)
btn_print_on_window = Button(root, text="print on screen", command=txt_on_window)
btn_print_on_window.place(x=0, y=150)
root.mainloop()
I'm using Tkinter for the GUI for my project (if you type ingredients you want into a textbox, it will return recipes using API.. and so on). I'm trying to save the user input into a variable so that I can use it later. However, the get() function seems like not catching anything. I've read a different post but not sure what I'm doing it wrong. Here is my code:
import tkinter as tk
import tkinter.font as font
# globally declare the expression variable
expression = ""
def getSentence():
global expression
# clear the entry fields
deleteEntryFields()
# ask a question
field2.insert(0, 'What do you want to eat? You can express and we will find something for you!')
expression = v.get()
return expression
def getIngredients():
pass
def searchWithSentence(sentence):
pass
def searchIngredients(ingredients):
pass
################################################################################
# This is where I'm testing if the user input is saved to a variable expression.
def enter():
field1.insert(0, str(expression))
print(expression)
################################################################################
def clear():
global expression
expression = ""
def deleteEntryFields():
field1.delete(0, 'end')
field2.delete(0, 'end')
# Driver code
if __name__ == "__main__":
# create a GUI window
master = tk.Tk()
v = tk.StringVar()
field1 = tk.Entry(master, textvariable=v)
field2 = tk.Entry(master)
field1.insert(0, 'Please type here')
field2.insert(0, 'Results will be shown here.')
field1.place(x=20, y=20, width=730, height=50)
field2.place(x=20, y=80, width=730, height=500)
# set the background colour of GUI window
master.configure(background="lemon chiffon")
# set the title of GUI window
master.title("Recipe Finder")
# set the configuration of GUI window
master.geometry("1050x600")
# set font
myFont = font.Font(family='Verdana', size=9, weight='bold')
# Buttons
button1 = tk.Button(master, text=' What do you feel like eating? ', fg='black', bg='salmon',
command=getSentence, height=5, width=30)
button1.place(x=770, y=100)
button1['font'] = myFont
button2 = tk.Button(master, text=' Type the ingredients you have! ', fg='black', bg='orange',
command=getIngredients, height=5, width=30)
button2.place(x=770, y=200)
button2['font'] = myFont
Clear = tk.Button(master, text=' CLEAR ', fg='black', bg='white',
command=clear, height=5, width=30)
Clear.place(x=770, y=300)
Clear['font'] = myFont
Enter = tk.Button(master, text=' ENTER ', fg='black', bg='white',
command=enter, height=5, width=30)
Enter.place(x=770, y=400)
Enter['font'] = myFont
# start the GUI
master.mainloop()
Previously I was not using StringVar(), but when I did some research, it says it can be one of the ways to get the get() function worked. But nothing happened... How can I effectively get user input and save it to global variable?
Any advice will be appreciated!
You are using the Entry itself to contain an explanation what to type?
To make that work you could bind a callback that clears the entry to the <FocusIn> event.
This will make sure that the entry field is empty when the user wants to type something.
And to be consistent, you would have to restore that text on a <FocusOut> event in the case that the user didn't fill in anything.
While this is used in places where screen space is extremely limited, it doesn't feel like good UI design.
A more common method is to put a Label with a description before the Entry.
i am developing an application to calculate some taxes and show the result in the graphical interface. The code itself works perfectly, but if i use numbers with bigger squares, the result overlaps over the previous one. My question is, is it possible to clear the previous result and calculate the new one?
Follow the complete code below:
from tkinter import *
root = Tk()
l_vlrRec = Label(root, text='Receita')
l_vlrRec.place(x=10, y=10)
e_vlrRec = Entry(root)
e_vlrRec.place(x=75, y=10, width=75)
def calcular():
receita = float(e_vlrRec.get())
l_result = Label(root, text='{:.2f}'.format(receita))
l_result.place(x=10, y=150)
e_vlrRec.delete(0, END)
bt = Button(root, text='Calcular', command=calcular)
bt.place(x=10, y=50)
root.mainloop()
You can use the label's textvariable and also you don't have to instantiate a new Label every time the button is pressed:
v_result = DoubleVar()
l_result = Label(root, textvariable=v_result)
l_result.place(x=10, y=150)
def calcular():
v_result.set(round(float(e_vlrRec.get()),2))
You can do the same for your Entry object e_vlrRec so you don't have to cast the string you get by calling e_vlrRec.get() but use the variable's get() instead
Without using textvariable you can also reconfigure the label's text parameter:
l_result.configure(text='{:.2f}'.format(receita))
or
l_result['text'] = '{:.2f}'.format(receita)
I'm trying to clear a Tkinter Entry Box by using a Tkinter Button. The problem is that my entry includes both 'Integers' aswell as a 'String' and my code wont clear the entry.
I have tried changing the attributes of the clearing method. Right now it's .delete(0, END). But i have tried changing it to .delete("0.0", END) and .delete(0.0, END. But this is just me desperatly trying things out.
enter code here
from tkinter import *
root = Tk()
def validatecontent(entry_text1):
return (entry_text1.isdigit() == bool(entry_text1)) or entry_text1 ==
(".")
def clear_entry():
entry_1.delete(0, END)
vcmd = (root.register(validatecontent), '%S')
entry_text1 = StringVar()
entry_1 = Entry(root, width=11, textvariable=entry_text1,
validate='all', validatecommand=vcmd)
entry_1.pack()
button = Button(root, width=10, text="Clear",
command=clear_entry)
button.pack()
root.mainloop()
If the entry contains Integers alone it gets cleared. But if the entry contains '.' nothing happens, no error code at all.
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 3 years ago.
All,
I have attempted this a few different ways and am still struggling here.
I want to have someone write in this Entry Box and then once submit is hit, it should write the text to the .txt file. I'm obviously not very good.
import datetime
from tkinter import *
def save():
with open("text.txt", "a") as f:
now = datetime.datetime.now()
test = TxtComplaint.get()
test = str(test)
f.write(test)
f.write(now)
window = Tk()
window.title("Documentation Window")
lbl = Label(window, text = "Enter In The Employee's Information")
TxtComplaint = Text(window, height = '10', width = '30')
benter = Button(window, text="Submit", command = save())
TxtComplaint.pack()
ee = Entry(window)
eelbl = Label(window, text = "Whats the name of the employee?")
eename = str(lbl)
lbl.pack()
benter.pack()
ee.pack()
eelbl.pack()
window.mainloop()
You need to provide the command, not the result of the command, to the Button. IOW leave off the (). It should be like this: benter = Button(window, text="Submit", command = save). Also you need to convert now to a string before writing, like this: f.write(str(now))