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()
Related
My code has two entry boxes one for pizza and the other one is for sandwich. If the user starts typing in the pizza entry box the sandwich box should be disabled and the user cannot type in it. If the user starts typing in the sandwich box then the pizza entry box should be disabled and they cannot type in it. I would like the box to be disabled before the user clicks submit. I have found this answer to be the most similar to what I am looking for and it does the job but I would like to have the same effect before clicking the submit button. [Text] (Python tkinter disable the button until all the fields are filled)
import tkinter as tk
root = tk.Tk()
root.geometry("600x400")
pizza = tk.StringVar()
sandwich = tk.StringVar()
def click():
pizza = pizza_lunch.get()
sandwich = pie_lunch.get()
pizza_label = tk.Label(root, text='pizza')
pizza_entry = tk.Entry(root, textvariable=pizza)
sandwich_label = tk.Label(root, text='sandwich')
sandwich_entry = tk.Entry(root, textvariable=sandwich)
button = tk.Button(root, text='Submit', command=click)
pizza_label.grid(row=0, column=0)
pizza_entry.grid(row=0, column=1)
sandwich_label.grid(row=1, column=0)
sandwich_entry.grid(row=1, column=1)
button.grid(row=2, column=0)
root.mainloop()
**** modified version ****
import tkinter as tk
root = tk.Tk()
root.geometry("600x400")
pizza = tk.DoubleVar()
sandwich = tk.DoubleVar()
def click(event):
if pizza.get() != '':
sandwich_entry.config(state=tk.DISABLED)
pizza_entry.config(state=tk.NORMAL)
if sandwich.get() != '':
pizza_entry.config(state=tk.DISABLED)
sandwich_entry.config(state=tk.NORMAL)
if pizza.get() == '' and sandwich.get() == "":
sandwich_entry.config(state=tk.NORMAL)
pizza_entry.config(state=tk.NORMAL)
print("price: " + str(pizza))
print("price: " + str(sandwich))
pizza_label = tk.Label(root, text='pizza')
pizza_entry = tk.Entry(root, textvariable=pizza)
pizza_entry.bind('<KeyRelease>', click)
sandwich_label = tk.Label(root, text='sandwich')
sandwich_entry = tk.Entry(root, textvariable=sandwich)
sandwich_entry.bind('<KeyRelease>', click)
button = tk.Button(root, text='Submit',)
pizza_label.grid(row=0, column=0)
pizza_entry.grid(row=0, column=1)
sandwich_label.grid(row=1, column=0)
sandwich_entry.grid(row=1, column=1)
button.grid(row=2, column=0)
root.mainloop()
we can make the requirement by binding method, I have some code to your, See if usefull
import tkinter as tk
root = tk.Tk()
root.geometry("600x400")
pizza = tk.StringVar()
sandwich = tk.StringVar()
def click(event):
if pizza.get() != '':
sandwich_entry.config(state=tk.DISABLED)
pizza_entry.config(state=tk.NORMAL)
if sandwich.get() != '':
pizza_entry.config(state=tk.DISABLED)
sandwich_entry.config(state=tk.NORMAL)
if pizza.get() == '' and sandwich.get() == "":
sandwich_entry.config(state=tk.NORMAL)
pizza_entry.config(state=tk.NORMAL)
pizza_label = tk.Label(root, text='pizza')
pizza_entry = tk.Entry(root, textvariable=pizza)
pizza_entry.bind('<KeyRelease>', click)
sandwich_label = tk.Label(root, text='sandwich')
sandwich_entry = tk.Entry(root, textvariable=sandwich)
sandwich_entry.bind('<KeyRelease>', click)
button = tk.Button(root, text='Submit',)
pizza_label.grid(row=0, column=0)
pizza_entry.grid(row=0, column=1)
sandwich_label.grid(row=1, column=0)
sandwich_entry.grid(row=1, column=1)
button.grid(row=2, column=0)
root.mainloop()
Is there a method to add a button where it opens the savefile.txt and lets me edit it while using Tkinter?
My current method is just saving the file and manually opening my txt
Here is my code:
from tkinter import *
from MailSaverFunctions import *
def quitApp():
errmsg = tkinter.messagebox.askquestion("Quit", "Save Before quitting")
if errmsg == "yes":
exit(-1)
elif errmsg == "no":
pass
else:
pass
def SavingInput():
category = categoryEntry.get()
Mail = mailEntry.get()
password = passwordEntry.get()
filewrite = open("SaveFile.txt", "a")
filewrite.write(category + "----" + Mail + "----" + password+"\n")
filewrite.close()
categoryEntry.delete(0, END)
mailEntry.delete(0, END)
passwordEntry.delete(0, END)
root = Tk()
root.title("MailSaver")
savepic = PhotoImage(file="save.png")
quitpic = PhotoImage(file="close.png")
startlbl = Label(root, text="Mail saver, password saver, in addition to categpries, Also organized!! ")
lblcategory = Label(root, text="Category", bd=1, relief=SUNKEN, fg="red")
lblmail = Label(root, text="Mail", bd=1, relief=SUNKEN)
lblpassword = Label(root, text="Password", bd=1, relief=SUNKEN)
categoryEntry = Entry(root)
mailEntry = Entry(root)
passwordEntry = Entry(root)
savebtn = Button(root, image=savepic, command=SavingInput)
quitbtn = Button(root, image=quitpic, command=quitApp)
lblcategory.grid(row=1, column=3, sticky=E)
lblmail.grid(column=3, row=2, sticky=E)
lblpassword.grid(column=3, row=3, sticky=E)
categoryEntry.grid(row=1, column=4)
mailEntry.grid(row=2, column=4)
passwordEntry.grid(row=3, column=4)
savebtn.grid(row=4, column=3)
quitbtn.grid(row=4, column=4)
lblcategory.configure(font="70")
lblmail.configure(font="100")
lblpassword.configure(font="85")
root.mainloop()
just need to make a viewing window and a editing window to edit infile credintals.
Here are the labels and buttons I am using, I want the login button to check for passwords in code, then if correct go to the next screen.
from tkinter import *
root = Tk()
# Main window of Application
root.title("Please Login")
root.geometry("300x150")
root.config(background='lightblue', borderwidth=5)
# Creating Label Widget
myLabel1 = Label(root, text="Username :", background='lightblue')
myLabel2 = Label(root, text="Password :", background='lightblue')
# Entry fields
username_1 = Entry(root)
password_1 = Entry(root, show='*')
# Putting labels onto screen
myLabel1.grid(row=0, column=0)
myLabel2.grid(row=1, column=0)
# Entry field Locations
username_1.grid(row=0, column=1)
password_1.grid(row=1, column=1)
Here i have the command quit button but having a hard time with the command for login to go to next window.
# Creating Buttons
loginButton1 = Button(root, text="Login")
cancelButton3 = Button(root, text="Cancel", command=quit)
# Putting buttons onto screen
loginButton1.grid(row=6, column=1)
cancelButton3.grid(row=7, column=1)
# New window
root.mainloop()
That is a lot for one question... I'll try to give you an idea of how you can do this though. I usually do not create a whole new window; I simply change the already existing window.
from tkinter import *
root = Tk()
# Main window of Application
root.title("Please Login")
root.geometry("300x150")
root.config(background='lightblue', borderwidth=5)
# Possible Login
possible_users = {'user1': 'user1_pass', 'user2': 'user2_pass'} # dictionary of corresponding user name and passwords
# StringVars
the_user = StringVar() # used to retrieve input from entry
the_pass = StringVar()
# Creating Label Widget
myLabel1 = Label(root, text="Username :", background='lightblue')
myLabel2 = Label(root, text="Password :", background='lightblue')
bad_pass = Label(root, text="Incorrect Username or Password")
# Entry fields
username_1 = Entry(root, textvariable=the_user)
password_1 = Entry(root, show='*', textvariable=the_pass)
# Putting labels onto screen
myLabel1.grid(row=0, column=0)
myLabel2.grid(row=1, column=0)
# Entry field Locations
username_1.grid(row=0, column=1)
password_1.grid(row=1, column=1)
def login(user):
forget_login_window()
next_window(user)
def check_login():
requested_user = the_user.get()
try:
if possible_users[requested_user] == the_pass.get():
login(requested_user)
else:
bad_pass.grid(row=2, column=1)
except KeyError:
bad_pass.grid(row=2, column=1)
loginButton1 = Button(root, text="Login", command=check_login)
cancelButton3 = Button(root, text="Cancel", command=quit)
# Putting buttons onto screen
loginButton1.grid(row=6, column=1)
cancelButton3.grid(row=7, column=1)
# New window
def forget_login_window(): # forget all the grid items.
username_1.grid_forget()
password_1.grid_forget()
myLabel1.grid_forget()
myLabel2.grid_forget()
loginButton1.grid_forget()
cancelButton3.grid_forget()
bad_pass.grid_forget()
def next_window(my_user):
root.title(my_user) # desired changes here
# you will need to create your tkinter objects (buttons, labels etc) in global and pack / grid / place them here.
root.mainloop()
I have one Input and one button. I want the value of the input (Entry) to when I press the button. When I type print(mtext) it works well, but when I put it in a Label it doesn't work.
Here is the code:
from tkinter import *
root = Tk()
root.title("Mohamed Atef")
root.geometry("900x600")
var = StringVar()
var.set("Please write something")
label = Label(root, textvariable=var, padx=10, pady=10)
#input
text = StringVar()
entry = Entry(root, textvariable=text)
def mohamed():
mtext = text.get()
mohamed = Label(root, textvariable=mtext)
mohamed.pack()
#button
buttonText = StringVar()
buttonText.set("Click me !")
button = Button(root, textvariable=buttonText, command=mohamed)
label.pack()
entry.pack()
button.pack()
root.mainloop()
Same as Flilp, your finished product would look like this
from tkinter import *
root = Tk()
root.title("Mohamed Atef")
root.geometry("900x600")
var = StringVar()
var.set("Please write something")
label = Label(root, textvariable=var, padx=10, pady=10)
#input
text = StringVar()
entry = Entry(root, textvariable=text)
def mohamed() :
mtext = text.get()
mohamed = Label(root, text=mtext)
mohamed.pack()
#button
buttonText = StringVar()
buttonText.set("Click me !")
button = Button(root, textvariable=buttonText, command=mohamed)
label.pack()
entry.pack()
button.pack()
root.mainloop()
If you just want text that's in your Entry to appear under your labels you could do:
def mohamed():
mohamed = Label(root, textvariable=text)
mohamed.pack()
Your code didn't work because value passed as textvariable should be tkinter StringVar() not string.
If you don't want the text to be constantly updated when you change your Entry you should do:
def mohamed():
mtext = text.get()
mohamed = Label(root, text=mtext)
mohamed.pack()
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?"