Checking value of Tkinter Entry widget - python

I have a submit button here:
submit_btn = Button(top, text="Submit", width=10, command=callback)
submit_btn.grid(row=3, column=1)
and as soon as you click it, it should check the entry field above if the input == "Vincent".
This is the entry field:
top = Tk()
user_label = Label(top, text="User Name")
user_label.grid(row=0, column=0)
username = Entry(top, bd = 5)
username.grid(row=0, column=1)
Is there a way to check if the value of the 'username' Entry object is "Vincent"?

I believe this should do the trick.
submit_btn = Button(top, text="Submit", width=10, command=check_vincent)
def check_vincent():
if username.get() == "Vincent":
print "Hi, Vincent!"
else:
print "Where's Vincent?"

Related

Tkinter - Change Label Text/Entry on Button Click

I have two labels and entry fields (A & B). When I enter the username/password for "A Username/A Password", I want to click the "Submit" button, then have the labels/entry fields change to "B Username/B Password" and be able to click the "Submit" button again, using Tkinter.
Python Code
import tkinter as tk
root = tk.Tk()
a_user_var = tk.StringVar()
a_pass_var = tk.StringVar()
b_user_var = tk.StringVar()
b_pass_var = tk.StringVar()
def submit():
a_user = a_user_var.get()
a_pass = a_pass_var.get()
a_user_var.set("")
a_pass_var.set("")
b_user = b_user_var.get()
b_pass = b_pass_var.get()
b_user_var.set("")
b_pass_var.set("")
a_user_label = tk.Label(root, text="A Username")
a_user_entry = tk.Entry(root, textvariable=a_user_var)
a_pass_label = tk.Label(root, text="A Password")
a_pass_entry = tk.Entry(root, textvariable=a_pass_var, show="•")
b_user_label = tk.Label(root, text="B Username")
b_user_entry = tk.Entry(root, textvariable=b_user_var)
b_pass_label = tk.Label(root, text="B Password")
b_pass_entry = tk.Entry(root, textvariable=b_pass_var, show="•")
sub_btn = tk.Button(root, text="Submit", command=submit)
a_user_label.grid(row=0, column=0)
a_user_entry.grid(row=0, column=1)
a_pass_label.grid(row=1, column=0)
a_pass_entry.grid(row=1, column=1)
b_user_label.grid(row=0, column=0)
b_user_entry.grid(row=0, column=1)
b_pass_label.grid(row=1, column=0)
b_pass_entry.grid(row=1, column=1)
sub_btn.grid(row=2, column=0)
root.mainloop()
Current Result
Desired Result (after clicking Submit button)
There is no need to create a unique label and entry widgets for A and B. Instead just use one set of widgets and change the label's text upon pressing the button to B. If you need to store the contents of the entry widget, you can grab the label text and parse it to see which character the specific set belongs to.
For example:
import tkinter as tk
root = tk.Tk()
user_var = tk.StringVar()
pass_var = tk.StringVar()
entries = {}
def submit():
user = user_var.get()
passw = pass_var.get()
label_text = user_label["text"]
char = label_text.split()[0]
entries[char] = (user, passw)
if char == "A":
user_label["text"] = "B" + label_text[1:]
pass_label["text"] = "B" + pass_label["text"][1:]
user_var.set('')
pass_var.set('')
print(entries)
user_label = tk.Label(root, text="A Username")
user_entry = tk.Entry(root, textvariable=user_var)
pass_label = tk.Label(root, text="A Password")
pass_entry = tk.Entry(root, textvariable=pass_var, show="•")
sub_btn = tk.Button(root, text="Submit", command=submit)
sub_btn.grid(row=2, column=0)
user_label.grid(row=0, column=0)
user_entry.grid(row=0, column=1)
pass_label.grid(row=1, column=0)
pass_entry.grid(row=1, column=1)
root.mainloop()

How can I import this entry answer into a text after

Okay so I have this sign up form and there is a part where you have to enter your name, I want that name answer to be taken to the page after.
import tkinter as tk
root = tk.Tk()
root.geometry("150x50+680+350")
def FormSubmission():
global button_start
button_start.place_forget()
l1.place_forget()
root.attributes("-fullscreen", True)
frame = tk.Frame(root)
tk.Label(frame, text="First Name:").grid(row=0)
name = entry1 = tk.Entry(frame) # I want the name written here to be taken from here to the welcome text.
entry1.grid(row=0, column=1)
tk.Label(frame, text="Last Name:").grid(row=1)
e2 = tk.Entry(frame)
e2.grid(row=1, column=1)
tk.Label(frame, text="Email:").grid(row=2)
e3 = tk.Entry(frame)
e3.grid(row=2, column=1)
tk.Label(frame, text="Date of Birth:").grid(row=3)
e4 = tk.Entry(frame)
e4.grid(row=3, column=1)
frame.pack(anchor='center', expand=True)
button_next = tk.Button(frame, text = "Next", height = 2, width = 7, command =lambda: MainPage(frame))
button_next.grid(row=4, column=1)
def MainPage(frame):
global FormSubmission
frame.pack_forget()
root.attributes("-fullscreen", True)
l1.place(x = 500, y = 10)
button_start.place_forget()
l1 = tk.Label(root, text="Welcome," , font=("Arial", 44)) #As you can see here in this line I want the entry 1 name here after welcome and the comma
button_start = tk.Button(root, text="Start", height=3, width=20, command = FormSubmission)
button_start.place(x = 0, y = 10)
button_exit = tk.Button(root, text="Exit", command=root.destroy)
button_exit.place(x=1506, y=0)
root.mainloop()
What I want to do is take the entry 1 answer and put it in the welcome text. There is an indicator on the lines I'm talking about.
Here is an example how to provide text from your widget entry1
in function FormSubmission():where you are defining your button you should pass the text you want to show in your label
button_next = tk.Button(frame, text = "Next", height = 2, width = 7, command =lambda: MainPage(frame, entry1.get()))
in funtion MainPage(frame):you should set text to your label:
def MainPage(frame, entry1):
global FormSubmission
frame.pack_forget()
root.attributes("-fullscreen", True)
l1.place(x = 500, y = 10)
button_start.place_forget()
l1.config(text="Welcome," + entry1) #<-------

How to wait for input in Entry widget in tkinter

I am trying to make a quiz in tkinter, and I have five questions. But, I want to wait for an answer to be inputted to the entry widget. I know I will probably need a button, but I don't know how I would go about doing that.
My code so far:
for i in range(5):
randChoose = random.choice(choose)
questionLabel = Label(top, text=full[randChoose]).grid(row=0, column=0)
answerLabel = Label(top, text="Answer:").grid(row=1, column=0)
answerEntry = Entry(top, borderwidth=5).grid(row=1,column=1)
if answerEntry.get() == aFull[randChoose]:
correctLabel = Label(top, text="Correct!",fg="green").grid(row=2,column=0)
score += 1
scoreLabel = Label(top, text=f"Your Score is {score}",fg="green").grid(row=2,column=1)
else:
wrongLabel = Label(top, text="Incorrect!",fg="red").grid(row=2,column=0)
scoreLabel = Label(top, text=f"Your Score is {score}",fg="red").grid(row=2,column=1)
choose.remove(randChoose)
First, add a button
button_pressed = StringVar()
this variable button_pressed will tell us if the button was pressed or not
(we set button_pressed to a StringVar() so .set() can change this variable, in the button's command)
button = Button(app, text="Enter", command=lambda: button_pressed.set("button pressed"))
when the button is pressed it will set the variable button_pressed, to "button pressed"
button.grid(row=1, column=1)
Then, wait until the button is pressed
button.wait_variable(button_pressed)
this code will wait until the varialbe button_pressed is changed (to anything)
Finally, check the entry
if answerEntry.get() == aFull[randChoose]: etc.
Final code should look something like this:
for i in range(5):
randChoose = random.choice(choose)
questionLabel = Label(app, text=full[randChoose]).grid(row=0, column=0)
answerLabel = Label(app, text="Answer:").grid(row=1, column=0)
answerEntry = Entry(app, borderwidth=5).grid(row=1,column=1)
button_pressed = StringVar()
button = Button(app, text="Enter", command=lambda: button_pressed.set("button pressed"))
button.grid(row=1, column=1)
button.wait_variable(button_pressed)
if answerEntry.get() == aFull[randChoose]:
correctLabel = Label(app, text="Correct!", fg="green").grid(row=2, column=0)
score += 1
scoreLabel = Label(app, text="Your Score is {score}", fg="green").grid(row=2, column=1)
else:
wrongLabel = Label(app, text="Incorrect!", fg="red").grid(row=2, column=0)
scoreLabel = Label(app, text="Your Score is {score}", fg="red").grid(row=2, column=1)
choose.remove(randChoose)
and you probobly want to destroy this button so on the next question it wont display 2 buttons
button.destroy()

Unable to return a value from Tkinter Entry box - need to pass an entry value from one script to another

I'm just about ready to pull my hair out - have tried just about everything I can imagine to do something that on the face of it seems fairly simple...
I need to have an Entry box, which takes in a variable, which is returned to the code and can be used as a variable throughout the script. I actually need to import this script and use it in the code of another script.
At the moment, I know the Submit button is calling the get_data() function, because using 'print' displays the password entered. But using return, to return it to the parent function, and then returning that value and printing the output of the main function returns nothing.
Thanks
from tkinter import *
def get_params():
def get_data():
pw = pwentry_enter.get()
return pw
window = Tk()
headFrame = Frame(window)
headFrame.grid(row=0, pady=6)
header = Label(headFrame, text="Input Password", font=(f1, 20))
header.grid(column=0, row=0, columnspan=2, sticky="w")
mainFrame = Frame(window, bg="#1B2230")
mainFrame.grid(row=1, pady=6)
raw_password = StringVar()
pwentry_enter=Entry(mainFrame, width=30, font=(f2,10), show="*", textvariable=raw_password)
pwentry_enter.pack()
btnFrame = Frame(window)
btnFrame.grid(row=2, pady=6)
submit_btn = Button(btnFrame, text='Submit', command=get_data, width=10, bg="#DB4158", fg="black", font=(f2, 20))
submit_btn.grid(column=1, row=0)
quit_btn = Button(btnFrame, text='Quit', command=window.destroy, width=10, bg="#DB4158", fg="black", font=(f2, 20))
quit_btn.grid(column=0, row=0)
window.mainloop()
a = get_data()
return a
Don't add return in get_data() and use a global variable to store the password when submit button is clicked and return when quit button is pressed.
You are trying to read data of the Entry after destroying the window.
from tkinter import *
pw = ''
def get_params():
global pw
def get_data():
global pw
pw = pwentry_enter.get()
window = Tk()
headFrame = Frame(window)
headFrame.grid(row=0, pady=6)
header = Label(headFrame, text="Input Password", font=(f1, 20))
header.grid(column=0, row=0, columnspan=2, sticky="w")
mainFrame = Frame(window, bg="#1B2230")
mainFrame.grid(row=1, pady=6)
raw_password = StringVar()
pwentry_enter=Entry(mainFrame, width=30, font=(f2,10), show="*", textvariable=raw_password)
pwentry_enter.pack()
btnFrame = Frame(window)
btnFrame.grid(row=2, pady=6)
submit_btn = Button(btnFrame, text='Submit', command=get_data, width=10, bg="#DB4158", fg="black", font=(f2, 20))
submit_btn.grid(column=1, row=0)
quit_btn = Button(btnFrame, text='Quit', command=window.destroy, width=10, bg="#DB4158", fg="black", font=(f2, 20))
quit_btn.grid(column=0, row=0)
window.mainloop()
return pw

ttk.combobox: How to place a default text inside the box?

Tried using textvariable and insert but it didn't work. What I hope to achieve is this:
But the text wont be stored as a value inside the combobox just a default text when you haven't selected anything yet.
This is my code so far:
root = Tk()
root.geometry('800x600')
root.title('CSSD')
topFrame=Frame(root,width=800,height=150,pady=10,padx=250)
area=Label(topFrame,text='CSSD')
area.config(font=("Courier", 100))
frame=Frame(root,highlightbackground="black", highlightcolor="black", highlightthickness=1, width=100, height=100, bd= 0)
frame.place(relx=.5, rely=.5, anchor="center")
username = Label(frame, text='User Name')
username.config(font='Arial',width=15)
password = Label(frame, text='Password')
password.config(font='Arial',width=15)
enteruser = Entry(frame, textvariable=StringVar(),font=large_font)
enterpass = Entry(frame, show='*', textvariable=StringVar(),font=large_font)
combo=ttk.Combobox(frame)
combo['values']=('Clinic 1','Clinic 2','Clinic 3','Clinic 4','Clinic 5','Clinic 6','Clinic 10','Clinic 11','OB')
combo.state(['readonly'])
combo.grid(row=0,sticky=NW)
topFrame.grid(row=0,sticky=EW)
area.grid(row=0,columnspan=300)
username.grid(row=1, sticky=E)
enteruser.grid(row=1, column=1)
password.grid(row=2, sticky=E)
enterpass.grid(row=2, column=1)
You can use the set method like so:
combo.set('default')
Just another option in case nobody mentioned it.
method = StringVar(value='default')
choosen = ttk.Combobox(root, width = 14, textvariable = method, )
And this is working in Checkbutton too.

Categories