Home > Software design >  Python with TKinter - how do you access data from an entry multiple times?
Python with TKinter - how do you access data from an entry multiple times?

Time:11-25

In this program, I have the user input their username into a login entry. Once they click the login button, the login_user() function is called to verify that their username exists in a .txt file.

I am trying to access the user's username in a later part of the program, more specifically in this line:

welcomeLabel = ttk.Label(F3, text="Welcome, {}".format(usernameEntry), background='#EAE2E2', font=('Inter', '20')).grid()

More specifically, I am trying to display a string "Welcome, USERNAME" on the home page, but when I reference the usernameEntry it gives the value None.

I have also tried creating a global variable in the login_user() function and referencing that along with reading the loginString String variable, but those have not worked.

I am new to TKinter, so any help would be appreciated - thanks!

More specifically, I am trying to display a string "Welcome, USERNAME" on the home page, but when I reference the usernameEntry it gives the value None.

I have also tried creating a global variable in the login_user() function and referencing that along with reading the loginString String variable, but those have not worked.

I am new to TKinter, so any help would be appreciated - thanks!

I also attached my code below.

from tkinter import *
from tkinter import ttk

def raise_frame(frame):
    frame.tkraise()

def login_user():
    #Without this if-statement, the blank input is captured and goes to the second frame
    uInput = loginString.get()
    if uInput == '':
        return loginString.get()
    with open('username.txt', 'r') as searchUsername:
        textFileSearch = searchUsername.readlines()
        for row in textFileSearch:
            findUserName = row.find(uInput)
            if findUserName == 0:
                raise_frame(F3)
                break
        else:
            print('Your username does not exist')

root = Tk()
root.title("FLASHCARDS PROGRAM")
root.resizable(width=False,height=False)

style = ttk.Style()
style.theme_use('default')

#Login Screen Frame
F1 = Frame(root, background = '#BACDAF', highlightbackground="black", highlightthickness=1)
F1.grid(padx=410,pady=210)

#Top-Label Frame
login_top_frame = Frame(F1, background='#468D70')
login_top_frame.grid(row=0, column=0, padx=10, pady=5)

#Light-Gray Background Frame
F2 = Frame(root, width=1280, height=720, background='#939393')
F2.grid(row=0, column=0, sticky='news')

#Home Screen Frame
F3 = Frame(root, width=1280, height=720, background='#EAE2E2')
F3.grid(row=0, column=0, sticky='news')

header_frame = Frame(F3, width = 1000, height = 100, background='#E4DDF4', highlightbackground="black", highlightthickness=1)
header_frame.grid(row=0, column=0, padx=10, pady=5, ipadx=475)

#Login Screen Widgets
loginHeader = ttk.Label(login_top_frame, text = "Flashcards For Free", background = '#468D70', font=('Inter','28','bold')).grid(column = 0, row = 0, padx = 90, pady = 20)
loginLabel = ttk.Label(F1, text = "Username:", font=('Inter', '15'), background='#BACDAF').grid(column=0,row=1, padx=20, sticky=W)
loginString = StringVar()
usernameEntry = ttk.Entry(F1, width=10, textvariable=loginString, background='green').grid(column=0, row=2, padx=20, pady=5, ipady = 5, sticky=W)
usernameInput = ttk.Button(F1, text="LOGIN", width = 8, command=login_user).grid(column=0, row=5, padx=21, sticky=W)
createAccount = ttk.Button(F1, text="No Account? Click Here").grid(column=0, row=8, padx=125,pady=25)

#Home Screen Widgets
menuLabel = ttk.Label(header_frame, text="Flashcards for Free", background='#E4DDF4', font=('Inter', '15')).grid(row=0, column=1, padx=10)
homeButton = ttk.Button(header_frame, text='Home').grid(row=0, column=2, padx=5)
createButton = ttk.Button(header_frame, text='Create').grid(row=0, column=3, padx=5)

welcomeLabel = ttk.Label(F3, text="Welcome, {}".format(usernameEntry), background='#EAE2E2', font=('Inter', '20')).grid()

raise_frame(F2)
raise_frame(F1)
root.mainloop()

CodePudding user response:

You have to call the get method of the entry after the user has had a chance to enter some data. Your code is trying to use the value about a millisecond after you create the entry widget.

You can do something like the following in the function that logs the user in:

welcomeLabel.configure(text=f"Welcome {usernameEntry.get()}")

CodePudding user response:

You have to make a .txt file and add your entries in there and can use the entries where ever you want to use them. You can use the following links:

File Handling in Python

Extract the data from line of text file

CodePudding user response:

Use loginString.get() to get the value of a tkinter variable.

  • Related