Python tkinter functions with multiply Tk() - python

I'm very new. Haven't gotten to "Classes" yet. Just trying to understand functions before I move forward. Trying to make a basic login widget that when the register button is clicked, it destroys the root window, launches the register window, then once the submit button is clicked, it gets the entry info and for right now, just prints it. I have one file that works but only has one Tk() window. Once you add another like the register function, the code no longer works. I figure it must be the order of operation that I am not understanding the concept of so I wanted some help. I have even tried to put the variables, "username_var, password_var into the register function since they are originally outside of the function but that didn't work either. Also tried calling global variables inside the register function but no luck there either. I worked on this for two days, so please don't think I didn't try all I could on my own. If you know of any documentation that I could read to better understand this, please let me know. I couldn't find anything on this topic.
from tkinter import *
# The first widget/container, not resizable
root = Tk()
root.resizable(False, False)
# variables to store the usernames and passwords
username_var = StringVar()
password_var = StringVar()
password2_var = StringVar()
''' Submit logic, if submit button is clicked or enter is pressed and username does not exist in variable list user_name and both password entries match, save username and password in variable list. '''
def submit():
print('Your username is ' + username_var.get())
if password_var == password2_var:
print('Your password is ' + password_var.get())
else:
print('Passwords do not match')
''' register button logic(if register button is clicked, destroy root window, load register window. Register window will have one username and two password labels and entries, entries have textvariables, rw = register window '''
def register_user():
root.destroy()
register_window = Tk()
rw_user_label = Label(register_window, text='Username')
rw_user_label.grid(row=0, column=0)
rw_pass_label = Label(register_window, text='Password')
rw_pass_label.grid(row=1, column=0)
rw_pass_label = Label(register_window, text='Password')
rw_pass_label.grid(row=2, column=0)
rw_user_entry = Entry(register_window, textvariable=username_var)
rw_user_entry.grid(row=0, column=1)
rw_pass_entry = Entry(register_window, textvariable=password_var, show='*')
rw_pass_entry.grid(row=1, column=1)
rw_pass_entry2 = Entry(register_window, textvariable=password2_var, show='*')
rw_pass_entry2.grid(row=2, column=1)
submit_button = Button(register_window, text='Submit', command=submit)
submit_button.grid(row=3, column=1, sticky='ew')
# username and password labels with grid locations
user_label = Label(root, text='Username')
user_label.grid(row=0, column=0)
pass_label = Label(root, text='Password')
pass_label.grid(row=1, column=0)
# username and password entries with grid locations
user_entry = Entry(root)
user_entry.grid(row=0, column=1)
pass_entry = Entry(root, show='*')
pass_entry.grid(row=1, column=1)
# Login and Register buttons with grid locations, both call functions
login_button = Button(root, text='Login')
login_button.grid(row=2, column=1, sticky='ew')
register_button = Button(root, text='Register', command=register_user)
register_button.grid(row=3, column=1, sticky='ew')
# creates an infinite loop for main root widget until destroy function is called or the widget is exited out of
root.mainloop()

In the register function the entry textvariables are used as initial values to be placed as default in this code. Any changes to the textvariables in the entry don't happen unless they have been declared global inside register.
def register():
global username_var, password_var, password2_var
so you are on the right track in terms of thinking about scope - a global variable has to be specified as global if it is changed inside a function scope. Otherwise it is simply referenced.

Related

Python password validation without reading file

My son is trying to learn python and wondered if someone could help on here?
The issue we are having is that we can not get password validation to work. Nothing fancy,
1 st window - enter name and password
if correct - 2nd window pops up
if incorrect - 3 window pops up
We have the following code, however the third window also pops up.
I suspect it's something to do with def main or variable.
from tkinter import*
window1=Tk()
window1.title("First window")
username="user"
password="password"
def main():
if eone == "user" and etwo == "password":
master=tk()
master.title("second window")
else:
master1=tk()
master1.title("third window")
Label(window1, text="username").grid(row=0)
Label(window1, text="password").grid(row=1)
eone=Entry(window1)
etwo=Entry(window1, show='*')
eone.grid(row=0, column=1)
etwo.grid(row=1, column=1)
b1 = Button(window1, text="login") ,bg='00c714',fg='#ffffff',command=main())
b1.grid(row=3, column=0, sticky=E)
b2=Button(window1, command=window1.destroy, text="exit", bg='#fc0303' ,fg='#ffffff')
b2.grid(row=3, column=1, sticky=E)
mainloop()
He has spent many hours on it yesterday and would appreciate any help
Thanks
First, the code you posted gave some errors so fixing them:
replacing tk() with ~~Tk()~~ Toplevel() (see the comments)
replacing '00c714' with '#00c714'
removing parantheses here "login")
now it becomes "compile" time error-free. As for your question, 2 things need changing:
When we need to give a callback / command to a button, we give the function itself and not call it! IOW, command=main() will lead to calling main right away when program runs (without pressing to button). Instead, we should do command=main (note the lack of parantheses). Actually this is what is done here also: command=window1.destroy - we need to give the function itself and tkinter will call it with parantheses when button is pressed.
eone == "user" This compares the tkinter Entry widget directly with the string "user"! What you meant is through get method of entries: eone.get() == "user". Same goes for etwo too.
Overall, here is the code with these modifications (and some PEP-8 compliant formatting):
from tkinter import*
window1 = Tk()
window1.title("First window")
username = "user"
password = "password"
def main():
# Change here: using `get` to get what is written in entries
if eone.get() == "user" and etwo.get() == "password":
master = Toplevel()
master.title("second window")
else:
master1 = Toplevel()
master1.title("third window")
Label(window1, text="username").grid(row=0)
Label(window1, text="password").grid(row=1)
eone = Entry(window1)
etwo = Entry(window1, show='*')
eone.grid(row=0, column=1)
etwo.grid(row=1, column=1)
# Change here: using function `main` itself instead of calling it
b1 = Button(window1, text="login", bg="#00c714", fg="#ffffff", command=main)
b1.grid(row=3, column=0, sticky=E)
b2 = Button(window1, command=window1.destroy, text="exit", bg="#fc0303", fg='#ffffff')
b2.grid(row=3, column=1, sticky=E)
mainloop()

Python tkinter error:'Variable is not defined" even though it is define

When I run this code is says "UserName is not defined even" though its defined in the function right below it. Do the functions have to be a certain order and if so is there a way to fix that.
from tkinter import *
def MasterLogin():
Name = UserName.get()
Email = RegisterEmail.get()
Password = RegisterPassword.get()
MasterLogin = Tk()
MasterLogin.title('Login')
MasterLogin.geometry('260x100')
LoginEmail = Entry(MasterLogin, width=30).grid(row=0, column=1)
LoginEmailText = Label(MasterLogin, text=Email).grid(row=0, column=0)
def MasterRegister():
MasterRegister = Tk()
MasterRegister.title('Register')
MasterRegister.geometry('260x100')
UserName = Entry(MasterRegister, width=30).grid(row=0, column=1)
UserNameText = Label(MasterRegister, text='Name ').grid(row=0, column=0)
RegisterEmail = Entry(MasterRegister, width=30).grid(row=1, column=1)
RegisterEmailText = Label(MasterRegister, text='Email ').grid(row=1, column=0)
RegisterPassword = Entry(MasterRegister, width=30).grid(row=2, column=1)
RegisterPasswordText = Label(MasterRegister, text='Password ').grid(row=2, column=0)
RegisterCont = Button(MasterRegister, text='Continue', width=25, bg='blue', fg='white',
command=MasterLogin).grid(row=3, column=1)
When looking at this code I would suggest the following. Create a class that handles windows. In this, you can easily have functions that do what you need to do, in this code it seems you are using functions as the end all be all for your code, this would be inefficient for your code and not very future proof.
Without a class you can still achieve what you want for an Entry box, the issues here with your example is that your variables are declared along a private scope making them inaccessible to the rest of the program, this can be fixed with declaring global within a certain area of your code but this can become messy and render functions almost useless for private functionality (this can lead to many errors)
Heres my simple example for an entry box with a button that gets the data in it
from tkinter import *
def ButtonPress(entry):
entry = entry
print(entry.get())
return entry.get()
F = Tk()
F.geometry("300x100")
F.config(bg="black")
myEntry = Entry()
myEntry.pack()
myButton = Button(text="Enter",command=lambda : ButtonPress(myEntry))
myButton.pack()
F.mainloop()

Transferring variables through functions

Another late night project. I was trying to make a simple login screen (the credentials will be used later).
Now I want to store the username and password as the variables USERNAME and PASSWORD in the "login screen".
For some reason it doesnt work. I tried so many things, like 'global', 'return' etc.
Is there a way to store the inputs in those variables without changing the code dramasticly? I will be modifying the code later on quite a bit and need to understand and explain this too numerous people.
EDIT:
in the dropdown menu there is a option called "-------". I never put it there, but it keeps popping up. Is there a reason why it always pops up? And how can I delete it?
import os
import smtplib
from tkinter import *
import tkinter.messagebox
#USERNAME
#PASSWORD
root = Tk()
root.geometry("500x300")
root.title("E-mail-Sending program EBF")
# *** FUNCTIONS ***
def setLoginCredentials():
USERNAME = entryLoginUsername.get()
PASSWORD = entryLoginPassword.get()
print(USERNAME)
print(PASSWORD)
def loginCredentials(event):
#Create another screen
loginScreen = Toplevel(root)
loginScreen.title("login-screen")
loginScreen.geometry("300x300")
#LABELS LOGIN SCREEN
labelLoginUsername = Label(loginScreen, text="E-mail:")
labelLoginUsername.grid(row=0,column=0, sticky=E)
labelLoginPassword = Label(loginScreen, text="Password:")
labelLoginPassword.grid(row=1,column=0, sticky=E)
#ENTRIES LOGIN SCREEN
entryLoginUsername = Entry(loginScreen)
entryLoginUsername.grid(row=0,column=1)
entryLoginPassword = Entry(loginScreen)
entryLoginPassword.grid(row=1,column=1)
#LOGIN BUTTON
loginButton1 = Button(loginScreen,text="Login",command=setLoginCredentials)
# loginButton1.bind("<Button-1>", setLoginCredentials)
loginButton1.grid(row=2,column=1, sticky=W)
def previewEmail():
tkinter.messagebox.showinfo('Email preview','Dear professor <NAME>\n\n\nThis email is on behalf of the Academy Committee of EBF Groningen, which is responsible for the booksale of the Economics and Business Faculty of the University of Groningen.\n\nSince you are the coordinator of the course <NAME>, we were wondering if any alterations were made regarding the compulsory literature that has not been listed on the latest version of Ocasys yet.\n\nWe would like the confirmation if the course literature on Ocasys is up to date or if any alterations are needed. This way we are able to contact the suppliers of these books and ensure that inconveniences, due to providing the wrong books, can be avoided.\n\n\nMet vriendelijke groet,\nKind Regard,\n\n<SENDER> - <FUNCTION>\nAcademy Committee\nEBF Groningen\n')
# *** LABELS HOMESCREEN ***
labelSender = Label(root, text="Sender:")
labelSender.grid(row=0,column=0, sticky=E)
labelFunction = Label(root, text="Function:")
labelFunction.grid(row=1,column=0, sticky=E)
labelEmail = Label(root, text="Email:")
labelEmail.grid(row=2,column=0, sticky=E)
labelProfessor = Label(root, text="Professor:")
labelProfessor.grid(row=3,column=0, sticky=E)
labelCourse = Label(root, text="Course:")
labelCourse.grid(row=4,column=0, sticky=E)
# *** ENTRIES MAINSCREEN***
entrySender = Entry(root)
entrySender.grid(row=0,column=2, columnspan=2)
entryFunction = Entry(root)
entryFunction.grid(row=1,column=2, columnspan=2)
entryEmail = Entry(root)
entryEmail.grid(row=2,column=2, columnspan=2)
entryProfessor = Entry(root)
entryProfessor.grid(row=3,column=2, columnspan=2)
entryCourse = Entry(root)
entryCourse.grid(row=4,column=2, columnspan=2)
# *** ENTRIES LOGINSCREEN ***
# *** BUTTONS ***
loginButton = Button(root, text="Login")
loginButton.bind("<Button-1>", loginCredentials)
loginButton.grid(row=6,column=0, sticky=E)
# *** MAIN MENU ***
menu= Menu(root)
root.config(menu=menu)
subMenu = Menu(root)
menu.add_cascade(label="Menu", menu=subMenu)
subMenu.add_command(label="Preview", command=previewEmail)
root.mainloop()
writing to a global variable inside a function works like this:
a = None
def foo():
global a
a = 42
a = 3
foo()
print(a)
Output:
42
The root of the problem is that you're using local variables everywhere but expecting them to be global.
If you want a variable to be accessible outside of the scope it was created in, you must define it as global1
def setLoginCredentials():
global USERNAME
global PASSWORD
global entryLoginUsername
global entryLoginPassword
...
def loginCredentials(event):
global entryLoginUsername
global entryLoginPassword
...
1 Strictly speaking this isn't true - global variables can be read without being declared global, but they can't be modified. However, since your intent is to use global variables, declaring them as global even when you don't need to will clarity to your code.
For more information see What are the rules for local and global variables in Python? in the official python documentation.

Trying to use a function to destroy tkinter frame that was created in another function. Unsuccessfully

I'm very new to python programming and I've been unable to find out how to get this to function properly. I'm using frames to show a login screen prior to loading the main inventory frame. I intend to record changes to inventory per user, so this is necessary for the program to function properly.
The program calls the Loginscreen() and LoginFrameGenerate() functions to generate the top menu and the frame including labels and entry fields. (Using Tkinter)
The "Log In" button, once pressed, calls the loginfun() function.
The loginfun() function is supposed to use an if statement to check the username and password and if they are correct, remove the login screen frame. Once the login screen frame has been removed, the mainframe and menu functions are called.
Unfortunately, the login screen frame will not go away when using the destroy() command. I can't move forward until I can get this working. Any guidance would be greatly appreciated.
I tried the function without re-initializing the frame inside the loginfun() function, but it generates a name error. I no longer get the name error, but the frame doesn't get destroyed. I've searched google and stack overflow for hours and the only thing I could find regarding Tkinter destroy() was in reference to classes. I'm beginning to think I've been coding this wrong and I should have made each frame a separate class.
from tkinter import *
import tkinter.messagebox
import sys
root = Tk()
root.iconbitmap('Favicon.ico')
testusername = "Admin"
testpassword = "Pass"
CurrentUser = StringVar()
Pass = StringVar()
root.state("zoomed")
def Exitbtnclick():
sys.exit(0)
def logout():
MainFrame=Frame(root)
MainFrame.destroy()
loginScreen
loginFrameGenerate
def loginScreen():
loginMenu = Menu(root)
root.configure(menu=loginMenu)
FileMenu=Menu(loginMenu)
loginMenu.add_cascade(label="File", menu=FileMenu)
FileMenu.add_cascade(label="Exit",command=Exitbtnclick)
def loginFrameGenerate():
#THIS CREATES THE LOGIN FRAME
logframe = Frame(root)
logframe.grid()
UL = Label(logframe, text="Username:", font="Arial 10 bold")
UL.grid(row=0, column=0, sticky="E")
UE = Entry(logframe, textvariable=CurrentUser)
UE.grid(row=0,column=1)
PL = Label(logframe, text="Password:", font="Arial 10 bold")
PL.grid(row=1, column=0, sticky="E")
PE = Entry(logframe, textvariable=Pass)
PE.grid(row=1,column=1)
loginbtn = Button(logframe, text="Log In", command=loginfun)
loginbtn.grid(row=3,columnspan=2)
def loginfun():
#THIS FUNCTION IS CALLED WHEN THE "LOG IN" BUTTON IS PRESSED
global testusername
global testpassword
logframe = Frame(root)
if (CurrentUser.get() == testusername) and (Pass.get() == testpassword):
logframe.destroy() #THIS IS THE PART THAT DOES NOT WORK
initializeMainMenu
initializeMainFrame
else:
tkinter.messagebox.showinfo("Error!", "Invalid Username/Password")
return
def initializeMainMenu():
mainMenu = Menu(root)
root.configure(menu=mainMenu)
FileMenu = Menu(mainMenu)
UserMenu = Menu(mainMenu)
ItemsMenu = Menu(mainMenu)
ReportMenu = Menu(mainMenu)
mainMenu.add_cascade(label="File", menu=FileMenu)
mainMenu.add_cascade(label="Users", menu=UserMenu)
mainMenu.add_cascade(label="Items", menu=ItemsMenu)
mainMenu.add_cascade(label="Reports", menu=ReportMenu)
FileMenu.add_separator()
FileMenu.add_command(label="Log Out/Switch User", command=logout)
FileMenu.add_command(label="Exit", command=Exitbtnclick)
UserMenu.add_command(label="Add/Remove Users",command=random)
def initializeMainFrame():
##Main Inventory Screen To Be Filled in once the user is logged in
MainFrame = Frame(root)
MainFrame.grid()
loginScreen()
loginFrameGenerate()
root.mainloop()

How can I make windows in Tkinter that run in succession? (Python 2.7.11)

I want to have a login window pop up at first, then once the conditions are satisfied, it closes the login window and opens a new window.
from Tkinter import *
import tkMessageBox
#Not really sure what i'm doing here anymore
while True:
Login = Tk()
Login.title('Login')
Login.geometry('400x130')
NameLabel = Label(Login, text='Username')
NameLabel.place(bordermode=OUTSIDE, height=25, width=100, x=100)
NameEntryRaw = Entry(Login)
NameEntryRaw.place(bordermode=OUTSIDE, height=25, width=100, x=200)
CodeLabel = Label(Login, text='Code')
CodeLabel.place(bordermode=OUTSIDE, height=25, width=100, x=100, y=30)
CodeEntryRaw = Entry(Login)
CodeEntryRaw.place(bordermode=OUTSIDE, height=25, width=100, x=200, y=30)
def tbd():
tkMessageBox.showinfo('Congrats!', 'This program is not done yet')
def login():
Usernames=list()
Usernames.append('Mordecai')
Usernames.append('Ezekiel')
Usernames.append('Goliath')
Usernames.append('Abraham')
Usernames.append('Bartholomew')
Usernames.append('Jedediah')
Usernames.append('Solomon')
Usernames.append('Tobias')
Usernames.append('Yohanan')
Usernames.append('Lucifer')
NameEntry=NameEntryRaw.get()
CodeEntry=CodeEntryRaw.get()
CodeReal='116987'
if Usernames.count(NameEntry) == 0:
tkMessageBox.showinfo('Error!', 'Your Username is invalid! Try Again.')
else:
()
if CodeEntry != CodeReal:
tkMessageBox.showinfo('Error!', 'The Code entered is invalid! Try Again.')
else:
()
LoginButton = Button(Login, text="Login", command=login)
LoginButton.place(bordermode=OUTSIDE, height=50, width=200, x=100, y=60)
Login.mainloop()
else:
DataBase = Tk()
#this will be the data base
DataBase.mainloop()
You don't want to use two mainloop's. Generally speaking, your tkinter app should have a single .mainloop called.
As for how to get the login popup and then switch to the window... You can create your root window with the app and when you start your program have a Toplevel window be shown with the login stuff, maybe hide / withdraw the root additionally?, have the toplevel have a submit button or something that would validate the credentials. If the credentials are valid, then you can use the destroy() method and remove the toplevel widget / go to the root window for your main app.
Having a while True repeat the process of creating a GUI + mainloop is bad for obvious reasons.
Edit: static is probably a bad term for a mutable object, since it's mutable... Added tuple vs list for global.
You don't need to create a blank list and then use append to add your usernames to the usernames list. You can make this global. Since, username is a mutable object (it's a list) you could still perform operations elsewhere in your code on this global, say .append etc. Or, if you never have these change i'd make it a tuple, since they're immutable and this "fits" better with the intent.
Instead of using .count(*) to get the occurrences of an element in a list you can just use:
if (object) in (some list) #Object is an abstraction, not the type
If CodeReal is static, which it looks to be, you can also make this global.
Here's a quick edit to your code, you can do this without classes, but I used classes here to try to distinctly show the logical separation in the program.
I changed a few variable names as well, so that it was easier to read / understand. I also used .pack() and .grid() as this was quicker to code than having to use .place() everywhere this is an arbitrary choice.
import Tkinter as tk
import tkMessageBox as messagebox
import sys
#No need to do usernames = list() and then .append for each one.
#Just make a global list holding them all...
USERNAMES = ('Mordecai', 'Ezekiel', 'Goliath', 'Abraham', 'Bartholomew',
'Jedediah', 'Solomon', 'Tobias', 'Yohanan', 'Lucifer')
PASSWORD = '116987' #Was CodeReal, this can be global
#We overrode the closing protocol here.
def closing_protocol():
if messagebox.askokcancel("Quit", "Do you want to quit?"):
sys.exit()
#A container for our Login "app".
class Login(tk.Toplevel):
def __init__(self, *args, **kwargs):
#We init the toplevel widget.
tk.Toplevel.__init__(self, *args, **kwargs)
#We set the closing protocol to be the overridden version / func.
self.wm_protocol("WM_DELETE_WINDOW", closing_protocol)
tk.Label(self, text='Username').grid(row=0, column=0)
self.username = tk.Entry(self)
self.username.grid(row=0, column=1)
tk.Label(self, text='Password').grid(row=1, column=0)
#Show = '*' just hides the password instead of plain-text like
#you typically see
self.password = tk.Entry(self, text='Password', show='*')
self.password.grid(row=1, column=1)
#When the button is clicked the _callback function will be called
tk.Button(self, text='Login', command=self._callback).\
grid(row=2, column=0, columnspan=2, sticky="nsew")
def _callback(self):
#If the username or password is bad, raise an error message.
if (self.username.get() not in USERNAMES or
self.password.get() != PASSWORD):
messagebox.showerror("Validation Error!",
"You have entered invalid credentials.\n" +
"Please try again.")
#otherwise, we're good to go. Destroy login / show main app.
else:
root.deiconify()
app.pack()
self.destroy()
class Main(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
tk.Label(self, text="MAIN APP STUFF").pack()
if __name__ == '__main__':
root = tk.Tk()
root.withdraw() #Hides the root window initially.
app = Main(root)
Login()
root.mainloop()
I want to thank you for clarifying what you did for me. It proved to be very helpful! Here is what I've settled with. I put the names back and made it to work without classes, as I found it a little too difficult to work with.
from Tkinter import *
import tkMessageBox
import sys
import binascii #Not for this part
Codenames = ['Mordecai', 'Ezekiel', 'Goliath', 'Abraham', 'Bartholomew',
'Jedediah', 'Solomon', 'Tobias', 'Yohanan', 'Lucifer']
Password = '116987'
def close_protocol():
if tkMessageBox.askokcancel("Quit", "Do you want to quit?"):
sys.exit()
#Below for a later part...
def text_to_bits(text, encoding='utf-8', errors='surrogatepass'):
bits = bin(int(binascii.hexlify(text.encode(encoding, errors)), 16))[2:]
return bits.zfill(8 * ((len(bits) + 7) // 8))
def text_from_bits(bits, encoding='utf-8', errors='surrogatepass'):
n = int(bits, 2)
return int2bytes(n).decode(encoding, errors)
def int2bytes(i):
hex_string = '%x' % i
n = len(hex_string)
return binascii.unhexlify(hex_string.zfill(n + (n & 1)))
#Above for a later part...
SDB = Tk()
SDB.title("SMEGJALBYT DATA BASE")
SDB.withdraw()
SDBLogin = Toplevel()
SDBLogin.title("Login")
SDBLogin.wm_protocol("WM_DELETE_WINDOW", close_protocol)
CodenameLabel = Label(SDBLogin, text="Codename")
CodenameLabel.grid(row=0, column=0)
CodenameEntry = Entry(SDBLogin)
CodenameEntry.grid(row=0, column=2)
PasswordLabel = Label(SDBLogin, text="Password")
PasswordLabel.grid(row=1, column=0)
PasswordEntry = Entry(SDBLogin, show='*')
PasswordEntry.grid(row=1, column=2)
def login_operation():
if CodenameEntry.get() not in Codenames:
tkMessageBox.showinfo("INVALID CODENAME!", "Please verify input in the 'Codename' field")
elif PasswordEntry.get() != Password:
tkMessageBox.showinfo("INVALID PASSWORD!", "Please verify input in the 'Password' field")
else:
SDB.deiconify()
SDBLogin.destroy()
LoginButton = Button(SDBLogin, text="Login", command=login_operation)
LoginButton.grid(row=2, column=1)
#Code continues...
#Code finisles....
SDB.mainloop()
This has the same basic functionality, just organised the way I wanted it.

Categories