I have been searching for an answer to this question for a couple days and all I can find is info about how to create a form for logging in. I am able to this just fine, my issue is a little more granular. I know how to capture data entered into the entry fields in tkinter with the .get() function.
I have multiple classes that serve as different parts of my program with various functions etc, but currently I have hard coded username, password, host, and database into the necessary portions of the program. This is a security risk and I would like to pass the login information to those locations allowing a user to login once and be able to enter their SQL Queries and be done, but upon closing the program/logging out these fields revert to being empty.
I have been reading about the use of setting a variable to be global, but this doesn't seem to be the best way to go about doing so since many people are saying that you should avoid globals if at all possible.
I think it should look like this:
import mysql.connector as mysql
from tkinter import *
class Login:
def__init__(self, login):
self.login=login
login.title("Login Screen")
global self.cnxn
self.cnxn=mysql.connect(
host=entry1.get(),
user=entry2.get(),
passwd=entry3.get(),
database=entry4.get())
self.entry1=Entry(login)
self.entry2=Entry(login)
self.entry3=Entry(login)
self.entry4=Entry(login)
self.entry1.pack()
self.entry2.pack()
self.entry3.pack()
self.entry4.pack()
class Other:
def__init__(self, other):
self.login=other
other.title("Login Screen")
def add():
c=self.cnxn
c.cursor()
self.cnxn.commit()
self.cnxn.close()
main=Tk()
Login(main)
main.mainloop()
My understanding of using globals is very limited, but I believe I would be able to pass the global variable to other classes and functions.
Is there a better way to do what I am trying to do?