Accessing a variable within another python file

Question:

I have this code in loginPage.py:

import data
from tkinter import *
import tkinter as tk
import os

class login(Tk):
    def __init__(self):
        
        super().__init__()
        self.geometry("700x500") #size of the window
        self.resizable(False,False) #dont let the user resize (might mess up layout of GUI)
    
    def Label(self):
        
        self.backGroundImage = PhotoImage(file = "LibraryBG.png") #photoimage for background
        self.backGroundImageLabel = Label(self, image = self.backGroundImage) #import the image as a label
        self.backGroundImageLabel.place(x=0, y=0) #placement
        
        self.canvas = Canvas(self, width=400,height = 330) #show a canvas in front of background
        self.canvas.place(x=150, y=70)
        
        self.titleLabel = Label(self, text = "Login", font = "Bold 30") #login text
        self.titleLabel.place(x=300, y=90)
        
        self.userNameLabel = Label(self, text = "Username", font = '8') #username text
        self.userNameLabel.place(x=200, y=180)
        
        self.passwordLabel = Label(self, text = "Password", font = '8') #password text
        self.passwordLabel.place(x=200, y=220)

    def Entry(self):  
        # these are the 2 entryboxes for the password and username
        self.userNameEntry = Entry(self, borderwidth=0, highlightthickness=0)
        self.userNameEntry.place(x=320, y=185, width=175, height=20)
        
        self.passwordEntry = Entry(self, borderwidth=0, show ='*', highlightthickness=0)
        self.passwordEntry.place(x=320, y=225 , width=175, height=20)
    
    def Button(self):
        #this button calls the login function once pressed
        self.loginButtonImage = PhotoImage(file = "LoginButton.png")
        self.loginButton = Button(self, image = self.loginButtonImage, command = self.Login, border = 0)
        self.loginButton.place(x=290,y=280)
        
    def Login(self):
        if self.userNameEntry.get() in data.employees.index:
            if int(self.passwordEntry.get()) == data.employees.loc[self.userNameEntry.get(),'password']:
                self.withdraw()
                os.system('main.py')
            else: 
                self.titleLabel = Label(self, text = "Your username and/or password doesn't exist.n Please try again or contact the admin.", fg = '#FF0000', font = "8")
                self.titleLabel.place(x=200, y=100)
        else:
            self.titleLabel = Label(self, text = "Your username and/or password doesn't exist.n Please try again or contact the admin.", fg = '#FF0000', font = "8")
            self.titleLabel.place(x=200, y=100) 
        
if __name__=="__main__":
    Login = login()
    Login.Label()
    Login.Entry()
    Login.Button()
    Login.mainloop()

In my other file main.py, how can I use the Entry that is created by the line self.userNameEntry = Entry(self, borderwidth=0, highlightthickness=0)?

Answers:

All you need to do is import the variable. But to do this, you first need the Login variable to be global. You also need to run the methods needed initalize Login. To do this, just change the end of loginPage.py to this:

Login = login()
Login.Label()
Login.Entry()
Login.Button()

if __name__=="__main__":
    Login.mainloop()

Note that Login.mainloop() is only run if you directly run python loginPage.py.

Then, from main.py, you can import Login and access it’s userNameEntry property. Like this:

from loginPage import Login

print(Login.userNameEntry)
Answered By: Michael M.
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.