I have tried to get a variable from a text input by doing this:
username_var = StringVar()
user_name_input_area = Entry(top, width = 30, textvariable=username_var).place(x = 110, y = 100)
username = username_var.get()
However when I try to acess the variable in a function it does not do anything.
My two functions are:
def ifSubmit():
writeToFile(service)
writeToFile(username)
def writeToFile(input_variable):
with open("password&usernames.txt", "a") as input:
input.writelines(input_variable)
But when I open the text file, nothing appears. I have also tested with:
writeToFile('hello')
In this case I do get hello in my text file and so I suspect that it is the variables that is the probelm not the writeToFile() function.
So am I retrieving the text being inputed correctly?
Full code:
import string
import random
from tkinter import *
mypasslist = []
with open("password&usernames.txt", "r") as input:
for line in input:
items = line.split()
mypasslist.append([item for item in items[0:]])
def generatePassword():
characters = string.ascii_letters + string.punctuation + string.digits
password = "".join(random.choice(characters) for x in range(random.randint(8, 16)))
return password
def writeToFile(input_variable):
with open("password&usernames.txt", "a") as input:
input.writelines(input_variable)
top = Tk()
top.geometry("1920x1080")
def ifSubmit():
global a
a = generatePassword()
label1 = Label(top, text='Your password is: %s' % a, font=("Arial", 11)).place(x = 40, y = 170)
label1 = Label(top, justify='left', text='Your password and username has been saved, you can access them below.').place(x = 40, y = 227)
copy_button = Button(top, text = 'Copy', command=copyText).place(x=40, y=195)
writeToFile(service)
writeToFile(username)
writeToFile(str(a))
def copyText():
top.clipboard_clear()
top.clipboard_append(a)
top.update()
# the label for user_name
Service = Label(top, text = "Service").place(x = 40, y = 60)
user_name = Label(top, text = "Username").place(x = 40, y = 100)
submit_button = Button(top, text = "Submit", command=ifSubmit).place(x = 40, y = 130)
service_var = StringVar()
Service_input_area = Entry(top, width = 30, textvariable=service_var)
Service_input_area.place(x = 110, y = 60)
service = service_var.get()
username_var = StringVar()
user_name_input_area = Entry(top, width = 30, textvariable=username_var)
username = username_var.get()
user_name_input_area.place(x = 110, y = 100)
top.mainloop()