so, i opened a question about it yesterday but it got closed saying its a dupe, let me explain my question more specificly then, hoping im not passinng any law here..
im trying to set a default message that will be set as gray and low opacity in the entry widget on tkinter, that after being focused in will dispear.
i managed to do that, but i got into a problem.
the default text is "enter username" for example, but if someone tries to do this user name it says that the username entry is empty which isnt what i wanna do..
i hope you understood my question.
edit: my question is how to do a placeholder text, while allowing the user to input the same text as the placeholder.
thanks in advance, this is my code:
from tkinter import *
from tkinter import messagebox
main = Tk()
main.title("ani gever")
checkbox_var = IntVar()
def delete_user_default(event):
if User_entry.get()!="enter username":
pass
else:
User_entry.delete(0, END)
def delete_password_default(event):
if password_entry.get() !="insert password":
pass
else:
password_entry.delete(0, END)
password_entry.config(show="*")
def return_user_default(event):
if User_entry.get()=="":
User_entry.insert(0, "enter username")
emptyuser=1
else:
pass
def return_password_default(event):
if password_entry.get()=="":
password_entry.insert(0,"insert password")
password_entry.config(show="")
else:
pass
def details(event):
if ((User_entry.get() == "enter username") or (password_entry.get() == "insert password")):
errorLabel.config(text="Username or password is empty!",fg="red")
elif ((" " in User_entry.get()) or " " in password_entry.get()):
errorLabel.config(text="dont use spaces in your password or username!", fg="red")
else:
print ("username:" + User_entry.get() + "\npassword:" + password_entry.get())
errorLabel.config(text="")
#============== define texts=======================
User_label= Label(main,text="Username:")
password_label= Label(main,text="Password:")
User_entry = Entry(main)
password_entry= Entry(main,show="*")
enterButton= Button(main,text="Log in")
errorLabel= Label(main,text="")
#=============default text on entry's=============
password_entry.config(show="")
User_entry.insert(0,"enter username")
password_entry.insert(0,"insert password")
User_entry.bind("<FocusIn>",delete_user_default)
password_entry.bind("<FocusIn>",delete_password_default)
User_entry.bind("<FocusOut>",return_user_default)
password_entry.bind("<FocusOut>",return_password_default)
#=============return user details ===========
User_entry.bind("<Return>",details)
password_entry.bind("<Return>",details)
enterButton.bind("<Button-1>",details)
#=============place everything on screen===========
User_label.grid(row=0,sticky= W)
User_entry.grid(row=0,column=1,sticky= W)
password_label.grid(row=1,sticky= W)
password_entry.grid(row=1,column=1,sticky= W)
enterButton.grid(row=2,sticky=W)
errorLabel.grid(row=3,columnspan=10,sticky= W)
main.mainloop()
Using this answer an is_empty function can be created. It prints True if validated entry is 'empty' and False if not.
import tkinter as tk
class EntryWithPlaceholder(tk.Entry):
def __init__(self, master=None, placeholder="PLACEHOLDER", color='grey'):
super().__init__(master)
self.placeholder = placeholder
self.placeholder_color = color
self.default_fg_color = self['fg']
self.bind("<FocusIn>", self.foc_in)
self.bind("<FocusOut>", self.foc_out)
self.put_placeholder()
def put_placeholder(self):
self.insert(0, self.placeholder)
self['fg'] = self.placeholder_color
def foc_in(self, *args):
if self['fg'] == self.placeholder_color:
self.delete('0', 'end')
self['fg'] = self.default_fg_color
def foc_out(self, *args):
if not self.get():
self.put_placeholder()
def is_empty(widget, empty_color):
if widget.get():
if widget['fg'] == empty_color:
print (True)
else:
print (False)
else:
print (True)
if __name__ == "__main__":
root = tk.Tk()
username = EntryWithPlaceholder(root, "username")
password = EntryWithPlaceholder(root, "password", 'blue')
username.pack()
password.pack()
tk.Button(root, text="Validate",
command=lambda wid=username, clr=username.placeholder_color:
is_empty(wid, clr)).pack()
root.mainloop()
Related
My placeholder works now but the problem lies with calling it in the SQL statement.
Without the placeholder, my SQL is working fine because each of my entry boxes have (example: textvariable="user") but when I place it in the configuration of my entry box the placeholder will also disappear. How can I make my placeholder works and also the SQL statement?
Thank you
class EntryWithPlaceholder(tk.Entry):
def __init__(self, master=None, placeholder="PLACEHOLDER", color='grey'):
super().__init__(master)
self.placeholder = placeholder
self.placeholder_color = color
self.default_fg_color = self['fg']
self.bind("<FocusIn>", self.foc_in)
self.bind("<FocusOut>", self.foc_out)
self.put_placeholder()
def put_placeholder(self):
self.insert(0, self.placeholder)
self['fg'] = self.placeholder_color
def foc_in(self, *args):
if self['fg'] == self.placeholder_color:
self.delete('0', 'end')
self['fg'] = self.default_fg_color
def foc_out(self, *args):
if not self.get():
self.put_placeholder()
def login(self, Event=None):
find_user = "SELECT * FROM employee WHERE ('username' : u OR email_address = %s) and password = %s"
cur.execute(find_user, [user.get(), user.get(), passwd.get()])
results = cur.fetchall()
if results:
if results[0][10] == "Admin":
messagebox.showinfo("Login Page", "The login is successful.")
page1.user_input.delete(0, END)
page1.pass_input.delete(0, END)
root.withdraw()
global adm
global page2
adm = Toplevel()
page2 = Admin_Page(adm)
# page2.time()
adm.protocol("WM_DELETE_WINDOW", exitt)
adm.mainloop()
else:
messagebox.showerror("Oops!!", "You are not an admin.")
else:
messagebox.showerror("Error", "Incorrect username or password.")
page1.pass_input.delete(0, END)
user_input = EntryWithPlaceholder(root, 'USERNAME OR EMAIL')
user_input.place(relx=0.18, rely=0.455, width=300, height=24)
user_input.configure(font=("Corbel", 15))
user_input.configure(relief="flat")
I want a placeholder entry that is done by the below code. but I need the input password which would be hidden from the user (which may be in the form of asterisks.)
from tkinter import *
root=Tk()
root.geometry("300x200+600+250")
root.config(background="#E0FFFF")
root.resizable(False,False)
def userText(event):
e1.delete(0,END)
usercheck=True
def passText(event):
e2.delete(0, END)
passcheck=True
a=StringVar()
b=StringVar()
usercheck=False
passcheck=False
Label(root,text="User name",bg="#E0FFFF").place(x=20,y=50)
e1= Entry(root,textvariable=a)
e1.place(x=100,y=50)
e1.insert(0,"Enter username")
e1.bind("<Button>",userText)
Label(root,text="Password",bg="#E0FFFF").place(x=20,y=95)
e2= Entry(root,textvariable=b)
e2.place(x=100,y=95)
e2.insert(0,"Enter password")
e2.bind("<Button>",passText)
root.mainloop()
Extended tkinter.Entry showing placeholder text and * on a password entry.
Live-Demo
import tkinter as tk
class Entry(tk.Entry):
def __init__(self, master, placeholder):
super().__init__(master)
self.placeholder = placeholder
self._is_password = True if placeholder == "password" else False
self.bind("<FocusIn>", self.on_focus_in)
self.bind("<FocusOut>", self.on_focus_out)
self._state = 'placeholder'
self.insert(0, self.placeholder)
def on_focus_in(self, event):
if self._is_password:
self.configure(show='*')
if self._state == 'placeholder':
self._state = ''
self.delete('0', 'end')
def on_focus_out(self, event):
if not self.get():
if self._is_password:
self.configure(show='')
self._state = 'placeholder'
self.insert(0, self.placeholder)
Usage:
if __name__ == "__main__":
root = tk.Tk()
username = Entry(root, "username")
username.pack()
password = Entry(root, "password")
password.pack()
root.mainloop()
to hide the password use:
e2.config(show='*')
This question already has answers here:
Why is my Button's command executed immediately when I create the Button, and not when I click it? [duplicate]
(5 answers)
Closed 2 years ago.
I am working on a program that lets users store their usernames and passwords, and hashes the plaintext where they are stored (hence the commented out hashing code). However, I am using tkinter for the GUI, and one of my buttons runs a defined function before it's called. (ignore all seemingly random comments please)
import basehash
import tkinter
import tkinter as tk
import os
from tkinter import*
from tkinter import messagebox
from string import ascii_lowercase
userPaths = ['./usernames2.txt', './username.txt', './usernames3.txt']
passPaths = ['./Passwords2.txt', './Passwords.txt', './Passwords3.txt']
#create new doc and assign doc array
# use an array for password of paths where the password is stored on a plaintext file
password = ("test")
usernameguess1 = ("")
passwordguess1 = ("")
loggedin = 0
activeUser = ""
activeUserNum = 0
hashbase = basehash.base52()
LETTERS = {letter: str(index) for index, letter in enumerate(ascii_lowercase, start=1)}
def alphabet_position(text):
text = text.lower()
numbers = [LETTERS[character] for character in text if character in LETTERS]
return ''.join(numbers)
def loginpage():
#Gui Formatting
root = tkinter.Toplevel()
root.resizable(width=FALSE, height=FALSE)
root.title("HasherTest_V0.1 Login")
root.geometry("300x150")
#Username and password boxes
usernametext = tkinter.Label(root, text="Username:")
usernameguess = tkinter.Entry(root)
passwordtext = tkinter.Label(root, text="Password:")
passwordguess = tkinter.Entry(root, show="*")
usernameguess1 = usernameguess
def trylogin():
print ("Logging In...")
for x in range(0,len(userPaths)):
#get path data here
file1 = open(userPaths[x], "r")
original = file1.read()
file1.close()
if original == usernameguess.get():
userPaths[x] = activeUser
x = activeUserNum
file1 = open(passPaths[activeUserNum], "r")
original = file1.read()
original = hashbase.unhash(original)
file1.close()
if usernameguess.get() == activeUser and alphabet_position(passwordguess.get()) == original:
print ("Success!")
messagebox.showinfo("Success ", "Successfully logged in.")
viewbutton = tkinter.Button(root, text="Veiw Saved Passwords", command=lambda:[root.withdraw()])
viewbutton.pack()
else:
print ("Error: (Incorrect value entered)")
messagebox.showinfo("Error", "Sorry, but your username or password is incorrect. Try again")
#login button
def viewtest():
if loggedin == 1:
messagebox.showinfo("Success ", "Loading Passwords")
else:
messagebox.showinfo("Error", "You need to sign in first!")
#login button
root.deiconify() #shows login window
attemptlogin = tkinter.Button(root, text="Login", command=trylogin)
attemptview = tkinter.Button(root, text="View Stored Passwords", command=viewtest)
usernametext.pack()
usernameguess.pack()
passwordtext.pack()
passwordguess.pack()
attemptlogin.pack()
attemptview.pack()
window.mainloop()
def signuppage():
root2 = tkinter.Toplevel()
root2.resizable(width=FALSE, height=FALSE)
root2.title("HasherTest_V0.1 Login")
root2.geometry("300x150")
#Gui Formatting (again)
root2 = tkinter.Tk()
root2.resizable(width=FALSE, height=FALSE)
root2.title("HasherTest_V0.1")
root2.geometry("300x150")
#Username and password boxes
masterusername = tkinter.Label(root2, text="Enter Master Username:")
masterusernamebox = tkinter.Entry(root2)
masterpassword = tkinter.Label(root2, text="Enter Master Password:")
masterpasswordbox = tkinter.Entry(root2, show="*")
def trysignup():
newuser = str(masterusernamebox.get())
length = len(userPaths)
namepath = './usernames3.txt'
userPaths[length-1] = namepath
newfile = open(namepath, 'w')
newfile.write(newuser)
newfile.close()
password = str(masterpasswordbox.get())
numPass = alphabet_position(password)
print(numPass)
"""hashbase = basehash.base52()
#not taking numPass as an int
#run test 328-i
hashval = hashbase.hash(numPass)
print(hashval)
unhashed = hashbase.unhash(hashval)
print(unhashed)"""
path = './passwords3.txt'
newfile = open(path, 'w')
newfile.write(numPass)
newfile.close()
if newuser == "":
messagebox.showinfo("Error code 0", "No input entered")
else:
return
#login button
mastersignupbutton = tkinter.Button(root2, text="Sign Up 1", command=trysignup())
mastersignupbutton.pack()
masterusername.pack()
masterusernamebox.pack()
masterpassword.pack()
masterpasswordbox.pack()
window.mainloop()
window = tkinter.Tk()
window.resizable(width=FALSE, height=FALSE)
window.title("HasherTest_V0.1")
window.geometry("300x150")
loginbutton = tkinter.Button(window, text="Login", command=lambda:[window.withdraw(), loginpage()])
signupbutton = tkinter.Button(window, text="Sign Up", command=lambda:[window.withdraw(), signuppage()])
loginbutton.pack()
signupbutton.pack()
window.mainloop()
If you run this code, and press the sign up button, then type in a master username and password, it should write them both to a notepad file, and if there is nothing in the box, give an error message. However, neither of these functions are working. When the user presses the first "sign up" button on the main window, it runs the wrong code (somehow) and gives an error code before it should. the error code should show if there is nothing there when "sign up 1" button is pressed on the "enter master username and password" window. I have no clue why the code runs early. Could somebody please reply with the reason why, or even some fixed code? thanks, Tf0R24.
When creating a button, a common error is including the parentheses after the function name. Instead of this:
mastersignupbutton = tkinter.Button(root2, text="Sign Up 1", command=trysignup())
Do this:
mastersignupbutton = tkinter.Button(root2, text="Sign Up 1", command=trysignup)
which is exactly the same but without the parentheses after command=trysingup. If you want to call a function that requires arguments, do this:
tkinter.Button(master, text="text", command=lambda: function_that_requires_arguments(x, y))
change to
mastersignupbutton = tkinter.Button(root2, text="Sign Up 1", command=trysignup)
Im having an issue with my logical/if/else statements it seems! the issue lies where if I type in a wrong password lets say but correct username, nothing happens, similar happens for both student and teacher, im just not really sure what to change. Thank you.
error:
File "/Users/Sebastian/Desktop/DQS/login.py", line 43, in _login_btn_clickked
if ( student_usernames.index(username) == student_passwords.index(password) ):
ValueError: tuple.index(x): x not in tuple
from tkinter import *
import tkinter.messagebox as tm
class LoginFrame(Frame):
def __init__(self, master):
super().__init__(master)
self.label_1 = Label(self, text="Username")
self.label_2 = Label(self, text="Password")
self.entry_1 = Entry(self)
self.entry_2 = Entry(self, show="*")
self.label_1.grid(row=0, sticky=E)
self.label_2.grid(row=1, sticky=E)
self.entry_1.grid(row=0, column=1)
self.entry_2.grid(row=1, column=1)
self.checkbox = Checkbutton(self, text="Keep me logged in")
self.checkbox.grid(columnspan=2)
self.logbtn = Button(self, text="Login", command = self._login_btn_clickked)
self.logbtn.grid(columnspan=2)
self.pack()
def _login_btn_clickked(self):
#print("Clicked")
username = self.entry_1.get()
password = self.entry_2.get()
#print(username, password)
student_usernames = ("C100", "C200", "C300")
student_passwords = ("PASS", "PASS1", "PASS2")
teacher_usernames = ("T100", "T200", "T300")
teacher_passwords = ("TPASS", "TPASS1", "TPASS3")
if username in student_usernames:
if ( student_usernames.index(username) == student_passwords.index(password) ):
tm.showinfo("Login info", "Welcome Student")
else:
tm.showerror("Login error", "Incorrect information")
elif username in teacher_usernames:
if ( teacher_usernames.index(username) == teacher_passwords.index(password) ):
tm.showinfo("Login info", "Welcome Teacher")
else:
tm.showerror("Login error", "Incorrect information")
else:
tm.showerror("Login error", "Incorrect information")
root = Tk()
lf = LoginFrame(root)
root.mainloop()
student_passwords.index(password) assumes that password actually exists in student_passwords. You could use if username in student_usernames and password in student_passwords: instead or surround the whole block with try: except ValueError:
E.g.
if username in student_usernames and password in student_passwords:
if ( student_usernames.index(username) == student_passwords.index(password) ):
tm.showinfo("Login info", "Welcome Student")
else:
tm.showerror("Login error", "Incorrect information")
elif username in teacher_usernames and password in teacher_passwords:
if ( teacher_usernames.index(username) == teacher_passwords.index(password) ):
tm.showinfo("Login info", "Welcome Teacher")
else:
tm.showerror("Login error", "Incorrect information")
else:
tm.showerror("Login error", "Incorrect information")
You're assuming that the input password will always be in one of the tuples you've defined to contain the password. You can easily break this, simply pass in something that's not contained in one of the tuples. You're condition breaks because as you see in the error message it's trying to find the index of that value but it's not even in the tuple. Hence, the ValueError exception.
You need to check if the password is in the respective student/teacher passwords tuple as well as username, then you can check the indexes.
So, this would be if username in student_usernames and password in student_passwords: as one example.
Or, you could reverse the logic by DeMorgan's Law(s) and use:
if not (username in student_usernames or password in student_passwords)
[Python 2.7] Hello. I'm working on a simple Tkinter calculator program, but can't seem to get the label to display any text after I push one of the buttons. Here is the code I'm using, some of the button functions are unfinished until I can get the label itself working:
from Tkinter import *
import ttk
"""Calculator"""
#Variables
Entered = ""
#Button Functions
def Natural_Log():
pass
def Exp():
Entered = "^"
def Sin():
pass
def Cos():
pass
def Tan():
pass
def LeftParentheses():
Entered = Entered + "("
def RightParentheses():
Entered = Entered + ")"
def Log():
pass
def XSquared():
Entered = Entered + "**2"
def InvX():
Entered = Entered + "**-1"
def Seven():
Entered = Entered + "7"
def Eight():
Entered = Entered + "8"
def Nine():
Entered = Entered + "9"
def DEL():
Entered = Entered[:1]
def AC():
Entered = ""
def Four():
Entered = Entered + "4"
def Five():
Entered = Entered + "5"
def Six():
Entered = Entered + "6"
def Mult():
Entered = Entered + "*"
def Div():
Entered = Entered + "/"
def One():
Entered = Entered + "1"
def Two():
Entered = Entered + "2"
def Three():
Entered = Entered + "3"
def Plus():
Entered = Entered + "+"
def Minus():
Entered = Entered + "-"
def Zero():
Entered = Entered + "0"
def Decimal():
Entered = Entered + "."
def Ex():
pass
def neg():
pass
def EXE():
pass
#Main Window Setup:
#Root setup
root = Tk()
root.title("Generic Calculator")
#Parent frame setup
mainframe = ttk.Frame(root,padding="8")
mainframe.grid(column=0,row=0,sticky=(N,S,E,W))
mainframe.columnconfigure(0,weight=1)
mainframe.rowconfigure(0,weight=1)
#Button setup
ttk.Button(mainframe,text="ln",command=Natural_Log).grid(column=1,row=2,sticky=W)
ttk.Button(mainframe,text="^",command=Exp).grid(column=2,row=2,sticky=W)
ttk.Button(mainframe,text="sin",command=Sin).grid(column=3,row=2,sticky=W)
ttk.Button(mainframe,text="cos",command=Cos).grid(column=4,row=2,sticky=W)
ttk.Button(mainframe,text="tan",command=Tan).grid(column=5,row=2,sticky=W)
ttk.Button(mainframe,text="(",command=LeftParentheses).grid(column=1,row=3,sticky=W)
ttk.Button(mainframe,text=")",command=RightParentheses).grid(column=2,row=3,sticky=W)
ttk.Button(mainframe,text="log",command=Log).grid(column=3,row=3,sticky=W)
ttk.Button(mainframe,text="x^2",command=XSquared).grid(column=4,row=3,sticky=W)
ttk.Button(mainframe,text="x^-1",command=InvX).grid(column=5,row=3,sticky=W)
ttk.Button(mainframe,text="7",command=Seven).grid(column=1,row=4,sticky=W)
ttk.Button(mainframe,text="8",command=Eight).grid(column=2,row=4,sticky=W)
ttk.Button(mainframe,text="9",command=Nine).grid(column=3,row=4,sticky=W)
ttk.Button(mainframe,text="DEL",command=DEL).grid(column=4,row=4,sticky=W)
ttk.Button(mainframe,text="AC",command=AC).grid(column=5,row=4,sticky=W)
ttk.Button(mainframe,text="4",command=Four).grid(column=1,row=5,sticky=W)
ttk.Button(mainframe,text="5",command=Five).grid(column=2,row=5,sticky=W)
ttk.Button(mainframe,text="6",command=Six).grid(column=3,row=5,sticky=W)
ttk.Button(mainframe,text="*",command=Mult).grid(column=4,row=5,sticky=W)
ttk.Button(mainframe,text="/",command=Div).grid(column=5,row=5,sticky=W)
ttk.Button(mainframe,text="1",command=One).grid(column=1,row=6,sticky=W)
ttk.Button(mainframe,text="2",command=Two).grid(column=2,row=6,sticky=W)
ttk.Button(mainframe,text="3",command=Three).grid(column=3,row=6,sticky=W)
ttk.Button(mainframe,text="+",command=Plus).grid(column=4,row=6,sticky=W)
ttk.Button(mainframe,text="-",command=Minus).grid(column=5,row=6,sticky=W)
ttk.Button(mainframe,text="0",command=Zero).grid(column=1,row=7,sticky=W)
ttk.Button(mainframe,text=".",command=Decimal).grid(column=2,row=7,sticky=W)
ttk.Button(mainframe,text="EXP",command=Ex).grid(column=3,row=7,sticky=W)
ttk.Button(mainframe,text="(-)",command=neg).grid(column=4,row=7,sticky=W)
ttk.Button(mainframe,text="EXE",command=EXE).grid(column=5,row=7,sticky=W)
#Label Setup:
EnteredSetup = StringVar()
ttk.Label(mainframe,textvariable=EnteredSetup).grid(column=1,row=1,columnspan=5)
EnteredSetup.set(Entered)
root.mainloop()
I believe there is a misunderstanding on how StringVar works. The line
EnteredSetup.set(Entered)
does not create some form of link between EnteredSetup and Entered, modifying Entered does not issue updates in EnteredSetup. Your code can be improved a lot too, and you should post something that is only long enough to describe the problem. Said that, consider this reduced version already fixed (note that it could be much smaller):
from Tkinter import Tk, StringVar
import ttk
class Calculator:
def __init__(self, state):
self.state = state
def ac(self):
self.state.set('')
def state_num(self, num):
self.state.set('%s%d' % (self.state.get(), num))
#Main Window Setup:
#Root setup
root = Tk()
root.title("Generic Calculator")
EnteredSetup = StringVar('')
calc = Calculator(EnteredSetup)
#Parent frame setup
mainframe = ttk.Frame(root, padding="8")
mainframe.grid(column=0, row=0)
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)
#Button setup
ttk.Button(mainframe, text="AC", command=calc.ac).grid(
column=5, row=4)
ttk.Button(mainframe, text="1", command=lambda: calc.state_num(1)).grid(
column=1, row=6)
ttk.Button(mainframe, text="0", command=lambda: calc.state_num(0)).grid(
column=1, row=7)
#Label Setup:
ttk.Label(mainframe, textvariable=EnteredSetup).grid(
column=1,row=1,columnspan=5)
root.mainloop()
I hope this guides you in the right direction for further adjusting your calculator.