I am trying to make a program that once the tick box is ticked it then the fields for the email appear but when it is un-ticked then the fields disappear when you press Update.
but I keep on getting:
emailLabel.destroy()
UnboundLocalError: local variable 'emailLabel' referenced before assignment
Any suggestions?, here is my code below:
#so when you tick the box, the value is 1 so this checks for that and sets a equal to 1
#if the box isnt ticked then it checks a and if it is equal to 1 then it
#deletes the fields for the email and code.
#PLEASE HELP. Error is in this bit
def update(var1):
global signupas
global a
print(a)
checklist = var1.get()
print(checklist)
if checklist == 1:
a= 1
if a == checklist:
# // but it's referenced here:
global email
emailLabel = Label(signupas, text="Email")
emailLabel.grid(row=3, column=0)
email = StringVar()
emailEntry = Entry(signupas, textvariable=email)
emailEntry.grid(row=3, column=1)
codeLabel = Label(signupas, text="code")
codeLabel.grid(row=4, column=0)
code = StringVar()
codeEntry = Entry(signupas, textvariable=email)
codeEntry.grid(row=4, column=1)
#its on about this bit:
if a == 1 and checklist == 0:
emailLabel.destroy()
codeLabel.destroy()
signupas.mainloop()
#//this gets the information for the update as it checks for the check box
#//error isn't here but above so don't worry about signup().
def signup():
global signupas
signupas = tk.Toplevel()
signupas.geometry('300x200')
tkWindow.title("database program")
usernameLabel = Label(signupas, text="Username").grid(row=0, column=0)
username = StringVar()
usernameEntry = Entry(signupas, textvariable=username).grid(row=0, column=1)
passwordLabel = Label(signupas,text="Password").grid(row=1, column=0)
password = StringVar()
passwordEntry = Entry(signupas, textvariable=password, show='*').grid(row=1, column=1)
var1 = IntVar()
Checkbutton(signupas, text="2step verification", variable=var1).grid(row=2)
SignUp = partial(checkuser_email, username, password, var1)
updates = partial(update, var1)
updateButton = Button(signupas, text="Update", command=updates).grid(row=5, column=2)
signupButton = Button(signupas, text="SignUp", command=SignUp).grid(row=5, column=1)
signupas.mainloop()
Thanks in advance :)
Related
have getting confused in getting the value from the Tkinter() Entry Field. I have this kind of code...
def login():
root_login = Tk()
root_login.title("Login")
root_login.geometry("400x400")
label_text_name = Label(root_login, text="Type mail your :")
label_text_name.pack()
label_text_name.place(x=25, y=50)
label_text_token = Label(root_login, text="Type Pasword :" )
label_text_token.pack()
label_text_token.place(x=58, y=150)
input_text_email = Entry(root_login, width=30)
input_text_email.pack()
input_text_email.place(x=150, y=50)
input_text_token = Entry(root_login, width=30)
input_text_token.pack()
input_text_token.place(x=150, y=150)
e1 = Entry(root_login)
e1.pack()
btn_login = Button(root_login,command=after_login, text="Login", height=1, width=10 )
btn_login.pack()
btn_login.place(x=50, y=250)
def after_login():
var = e1.get()
messagebox.showinfo(var1)
but i get a error !
var = e1.get()
NameError: name 'e1' is not defined
This is because your after_login function doesn't know what e1 is, since it's defined outside of the functions scope. You have to modify your code, maybe by setting the variable as global ( Which can lead to other problems if done incorrectly ) or some other way let after_login know what an e1 is ( by maybe giving it as parameter could solve this? )
Jimpsoni, I have put in a global vatable like this:
def login():
root_login = Tk()
root_login.title("Login")
root_login.geometry("400x400")
label_text_name = Label(root_login, text="Type mail your :")
label_text_name.pack()
label_text_name.place(x=25, y=50)
label_text_token = Label(root_login, text="Type Pasword :" )
label_text_token.pack()
label_text_token.place(x=58, y=150)
input_text_email = Entry(root_login, width=30)
input_text_email.pack()
input_text_email.place(x=150, y=50)
input_text_token = Entry(root_login, width=30)
input_text_token.pack()
input_text_token.place(x=150, y=150)
global var1
var1 = input_text_token.get()
btn_login = Button(root_login,command=after_login, text="Login", height=1, width=10 )
btn_login.pack()
btn_login.place(x=50, y=250)
def after_login():
messagebox.showinfo(var1)
but now I get this error!
global var1
^
IndentationError: unindent does not match any outer indentation level
I found the answer:
def callback():
print(email.get())
....
..
e = Entry(root, bd=1, textvariable=email, validate="focusout" , validatecommand=callback).grid(column=1, row=1)
I created a program which should dump the username and password in json file
But i am having problem in solving it
plz help
def createAccount():
A = tk.StringVar()
B = tk.StringVar()
root1 = Tk()
root1.resizable(0, 0)
root1.title('Signup')
instruction = Label(root1, text='Please Enter new Credentials')
instruction.grid(row=0, column=0, sticky=E)
nameL = Label(root1, text='New Username: ')
pwordL = Label(root1, text='New Password: ')
nameL.grid(row=1, column=0, sticky=W)
pwordL.grid(row=2, column=0, sticky=W)
nameE = Entry(root1, textvariable=A)
pwordE = Entry(root1, textvariable=B )
nameE.grid(row=1, column=1)
pwordE.grid(row=2, column=1)
signupButton = Button(root1, text='Signup')
signupButton.grid(columnspan=2, sticky=W)
root1.mainloop()
username = A
password = B
with open('user_accounts.json', 'r+') as user_accounts:
users = json.load(user_accounts)
if username in users.keys():
print('error')
else:
users[username] = [password, "PLAYER"]
user_accounts.seek(0)
json.dump(users, user_accounts)
user_accounts.truncate()
print("success")
I tried to convert username and password into string by using tk.StringVar()
But a error is displayed
Plz provide any appropriate solution
tkinter labels don't use normal Python variables types. Instead, they use tcl types, such as StringVar. to get the values of such variables, you can call their .get() method, which returns a native Python value. Now you can convert, change and use it as you like :)
I am trying to create a standard user ID/PASS login. When I use the next function to check if the entered password and name are right, I always get the "wrong values entered" message. Basically, the variables entry_1 and entry_2 are not storing the input text and I want a solution for that. Maybe any of you guys might propose a solution for that?
I have tried to assign entry_1 and entry_2 to variables but it did'nt work out.
from tkinter import *
root = Tk() # creates a window and initializes the interpreter
root.geometry("500x300")
name = Label(root, text = "Name")
password = Label(root, text = "Password")
entry_1 = Entry(root)
entry_2 = Entry(root)
name.grid(row = 0, column = 0, sticky = E) # for name to be at right use sticky = E (E means east)
entry_1.grid(row = 0, column =1)
x = "Taha"
password.grid(row = 1, column = 0)
entry_2.grid(row = 1, column =1)
y = "123"
c = Checkbutton(root, text = "Keep in logged in").grid(columnspan = 2 ) # mergers the two columns
def next():
if a == entry_1 and b == entry_2:
print ("Proceed")
else:
print("wrong values entered")
def getname():
return name
Next = Button(root, text = "Next", command=next).grid(row = 3, column = 1)
root.mainloop() # keep runing the code
I want the program to return "Proceed" once correct values are entered.
in your code you're not checking for the user input anywhere. You should use get() to return user input. I've modified your code accordingly. Now if you enter Taha as username and 123 as password, you'll get the "Proceed" message.
from tkinter import *
root = Tk() # creates a window and initializes the interpreter
root.geometry("500x300")
name = Label(root, text="Name")
password = Label(root, text="Password")
entry_1 = Entry(root)
entry_2 = Entry(root)
name.grid(row=0, column=0, sticky=E) # for name to be at right use sticky = E (E means east)
entry_1.grid(row=0, column=1)
x = "Taha"
password.grid(row=1, column=0)
entry_2.grid(row=1, column=1)
y = "123"
c = Checkbutton(root, text="Keep in logged in").grid(columnspan=2) # mergers the two columns
def next_window():
user_name = entry_1.get()
user_pass = entry_2.get()
if x == user_name and y == user_pass:
print("Proceed")
else:
print("wrong values entered")
def get_name():
return name
Next = Button(root, text="Next", command=next_window).grid(row=3, column=1)
root.mainloop()
thanks to the people who helped, with your help i could find the missing part in the code. i should have used .get() funtion in order to get the entered text back.
here is the upgraded code with some improvements.
from tkinter import *
from tkinter import messagebox
root = Tk() # creates a window and initializes the interpreter
root.geometry("500x300")
name = Label(root, text = "Name")
password = Label(root, text = "Password")
entry_1 = Entry(root)
entry_2 = Entry(root)
name.grid(row = 0, column = 0, sticky = E) # for name to be at right use sticky = E (E means east)
entry_1.grid(row = 0, column =1)
x = "Taha"
password.grid(row = 1, column = 0)
entry_2.grid(row = 1, column =1)
y = "123"
c = Checkbutton(root, text = "Keep in logged in").grid(columnspan = 2 ) # mergers the two columns
def next():
a = entry_1.get()
b = entry_2.get()
if a == "Taha" and b =="123":
messagebox.showinfo("Login", "successfuly logged in ")
root.destroy()
print ("Proceed")
else:
messagebox.showerror("Error", "wrong values entered")
print("wrong values entered")
root.destroy()
Next = Button(root, text = "Next", command=next).grid(row = 3, column = 1)
root.mainloop() # keep runing the code
Im trying to create a Python tkinter login registeration but running into a small issue.
The error message is:
self.Label_Name = Label(top, text="What is your username: ")
AttributeError: Label instance has no __call__ method
Please can you proof read my code:
from Tkinter import *
class Register:
def __init__(self, parent):
top = self.top = Toplevel(parent)
# Variables to store the entries
self.VarEntUser = StringVar()
self.VarEntPass = StringVar()
self.VarEntRetype = StringVar()
self.Label_Name = Label(top, text="What is your username: ")
self.Label_Password = Label(top, text="Enter a password: ")
self.Label_Retype = Label(top, text="Retype Password: ")
# Entry fields for the user to enter there details
self.Ent_Name = Entry(top, textvariable=self.VarEntUser)
self.Ent_Password = Entry(top, textvariable=self.VarEntPass)
self.Ent_Retype = Entry(top, textvariable=self.VarEntRetype)
# Puts all the fields ^, into the window
self.Label_Name.grid(row=0, sticky=W)
self.Label_Password.grid(row=1, sticky=W)
self.Label_Retype.grid(row=2, sticky=W)
self.Ent_Password.grid(row=1, column=1)
self.Ent_Retype.grid(row=2, column=1)
self.Ent_Name.grid(row=0, column=2)
# Run the RegisterCheck function
# submit button which Checks the Entered details then writes the user and pass to a .txt file
self.MySubmitButton = Button(top, text='Submit', command=RegisterCheck)
self.MySubmitButton.pack()
self.U = raw_input(self.VarEntUser.get())
self.P = raw_input(self.VarEntPass.get())
self.R = raw_input(self.VarEntRetype.get())
class LogIn:
def __init__(self, parent):
top = self.top = Toplevel(parent)
self.a = StringVar()
self.b = StringVar()
self.Label_Log_User1 = Label(top, text='Username:')
self.Label_Log_Pass = Label(top, text='Password: ')
self.Ent_User_Log = Entry(top, textvariable=self.a)
self.Ent_Pass_Log = Entry(top, textvariable=self.b)
self.Label_Log_User1.grid(row=1)
self.Pass_Log.grid(row=2)
self.EntUserLog.grid(row=1, column=1)
self.EntPassLog.grid(row=2, column=1)
self.User = raw_input(self.EntUserLog.get())
self.Pass = raw_input(self.EntUserLog.get())
# runs the 'LoginCheck' function
self.LogInButton = Button(top, text="Log In", command=LogInCheck)
self.LogInButton.pack()
def LogInCheck(self):
# Checks if the fields are blanking displaying an error
if len(self.User) <= 0 and len(self.Pass) <= 0:
print "Please fill in all fields."
else:
pass
# Checks to see if the user and pass have been created
if self.User in 'username.txt' and self.Pass in 'password':
print 'You are now logged in!'
else:
print "Log in Failed"
def RegisterCheck(self):
# Checks if the fields are blank
if len(self.P) <= 0 and len(self.U) <= 0:
print "Please fill out all fields."
else:
pass
# Check is the password and the retype match
if self.P == self.R:
pass
else:
print "Passwords do not match"
# After registering write the user and pass to a .txt file
with open('username.txt', 'a') as fout:
fout.write(self.U + '\n')
with open('password.txt', 'a') as fout:
fout.write(self.P + '\n')
# Depending on what the user chooses, either log in or register than opens the specific window
def launch_Register():
inputDialog = Register(root)
root.wait_window(inputDialog.top)
def launch_LogIn():
inputdialog2 = LogIn(root)
root.wait_window(inputdialog2.top)
root = Tk()
label = Label(root, text='Choose an option')
label.pack()
loginB = Button(root, text='Log In', command=launch_LogIn)
loginB.pack()
registerB = Button(root, text='Register', command=launch_Register)
registerB.pack()
root.mainloop()
The problem is that in this line
Label = Label(root, text='Choose an option')
you define a Label called Label, thus shadowing the Label constructor. Then, then you create the several labels in your Register and Login classes (triggered by those two buttons), the name Label is no longer bound to the constructor, but to that specific label.
Change the name of the label, then it should work. Also, I would advise you to use lower-case names for variables and methods. This alone might help prevent many such errors.
root = Tk()
label = Label(root, text='Choose an option')
label.pack()
loginB = Button(root, text='Log In', command=launch_LogIn)
loginB.pack()
registerB = Button(root, text='Register', command=launch_Register)
registerB.pack()
root.mainloop()
Note that there are a few many more problems with your code:
StringVar a and b should probably be self.a and self.b
You are trying to use raw_input to get the user input in the Entry widgets; this is wrong! Instead, just read the value of the variables to get the values, e.g. instead of self.User, use self.a.get()
do not mix grid and pack layout
if self.User in 'username.txt' will not check whether that name is in that file
loginCheck and registerCheck should be methods of the respective class
Once I'm at it, here's (part of) my version of your code, to help you getting started:
class Register:
def __init__(self, parent):
top = self.top = Toplevel(parent)
self.var_user = StringVar()
self.var_pass = StringVar()
self.var_retype = StringVar()
Label(top, text="What is your username: ").grid(row=0, sticky=W)
Label(top, text="Enter a password: ").grid(row=1, sticky=W)
Label(top, text="Retype Password: ").grid(row=2, sticky=W)
Entry(top, textvariable=self.var_user).grid(row=0, column=1)
Entry(top, textvariable=self.var_pass).grid(row=1, column=1)
Entry(top, textvariable=self.var_retype).grid(row=2, column=1)
Button(top, text='Submit', command=self.registerCheck).grid(row=3)
def registerCheck(self):
u, p, r = self.var_user.get(), self.var_pass.get(), self.var_retype.get()
if p and u:
if p == r:
logins[u] = p
else:
print "Passwords do not match"
else:
print "Please fill out all fields."
class LogIn:
# analogeous to Register; try to figure this out xourself
def launch_Register():
inputDialog = Register(root)
root.wait_window(inputDialog.top)
def launch_LogIn():
inputDialog = LogIn(root)
root.wait_window(inputDialog.top)
logins = {}
root = Tk()
Label(root, text='Choose an option').pack()
Button(root, text='Log In', command=launch_LogIn).pack()
Button(root, text='Register', command=launch_Register).pack()
root.mainloop()
Note that I changed the login "database" from files to a dictionary to keep things simple and to focus on the Tkinter problems. Of course, neither a simple dictionary nor a plain-text file is an appropriate way to store login information.
Also, I put the creation and the layout of the GUI widgets on one line. In this case this is possible since we do not need a reference to those widgets, but beware never to do e.g. self.label = Label(...).grid(...), as this will bind self.label to the result of grid, and not to the actual Label.
Finally, this will still print all the messages to the standard output. Instead, you should add another Label for that, or open a message dialogue, but this is left as an excercise to the reader...
I am writing a subnetting program in Python and I have come across a problem.
So far everything is working minus one thing. I dont know how to change a label in a method. in the code below, SubnetM is the variable being used to show the subnet mask. It is set to 0 by default but when you select HOSTS and enter 6 as Quantity. The 0 does not change to 255.255.255.248. PLEASE HELP
from Tkinter import *
SubnetM = 0
def beenclicked():
radioValue = relStatus.get()
return
def changeLabel():
if radio1 == 'HOSTS':
if Quantity == 6:
SubnetM = "255.255.255.248"
return
app = Tk()
app.title("SUBNET MASK CALCULATOR")
app.geometry('400x450+200+200')
labelText = StringVar()
labelText.set("WELCOME!")
label1 = Label(app,textvariable=labelText, height=4)
label1.pack()
relStatus = StringVar()
relStatus.set(None)
radio1 = Radiobutton(app, text="HOSTS", value="HOSTS", variable=relStatus, command=beenclicked).pack()
radio1 = Radiobutton(app, text="NETWORKS", value="NETWORKS", variable=relStatus, command=beenclicked).pack()
label2Text = StringVar()
label2Text.set("~Quantity~")
label2 = Label(app, textvariable=label2Text, height=4)
label2.pack()
custname = IntVar(None)
Quantity = Entry(app,textvariable=custname)
Quantity.pack()
label3Text = StringVar()
label3Text.set("Your Subnet Mask is...")
label3 = Label(app, textvariable=label3Text, height=4)
label3.pack()
label4Text = StringVar()
label4Text.set(SubnetM)
label4 = Label(app, textvariable=label4Text, height=4)
label4.pack()
button1 = Button(app, text="GO!", width=20, command=changeLabel)
button1.pack(padx=15, pady=15)
app.mainloop()
To fix your problem, make changeLabel like this:
def changeLabel():
# Get the radiobutton's StringVar and see if it equals "HOSTS"
if relStatus.get() == 'HOSTS':
# Get the entrybox's IntVar and see if it equals 6
if custname.get() == 6:
# Set the label's StringVar to "255.255.255.248"
label4Text.set("255.255.255.248")
Also, the .pack method of a Tkinter widget returns None. So, you should make the part that defines the radiobuttons like this:
radio1 = Radiobutton(app, text="HOSTS", value="HOSTS", variable=relStatus, command=beenclicked)
radio1.pack()
radio2 = Radiobutton(app, text="NETWORKS", value="NETWORKS", variable=relStatus, command=beenclicked)
radio2.pack()