Я только начинаю с python. Я прошел курс Удеми. Я пытаюсь создать приложение GUI с помощью модуля Tkinter
, но я остановился на одном моменте и понятия не имею, как это сделать. Я хочу использовать self.Email_Id
от Login class
до Main_Window class
. и я хочу повторно использовать MainLoop function
с Login class
до Main_Window class
. В противном случае мне придется переписать эту функцию, что не является условием повторного использования кода.
И, если возможно, предложите, какие могут быть различные способы сделать это или работать на многоуровневом windows. Ниже мой код.
Заранее спасибо.
from tkinter import *
from tkinter import messagebox, ttk
from PIL import Image, ImageTk # For resizing Images
import sqlite3
from win32api import GetSystemMetrics # For getting screen resolution
Title_Font = ("roboto", 30, "bold")
Label_Font = ("roboto", 17, "bold")
Entry_Font = ("roboto", 14)
Button_Font = ("roboto", 20, "bold")
#------------------------------------------- Resizing Background Image to fit in window -------------------------------------------#
BG_Image = Image.open("Image/BG_Image.jpg")
Resized_BG_Image = BG_Image.resize((GetSystemMetrics(0), GetSystemMetrics(1)))
Resized_BG_Image.save("Image/BG_Image_Resized.jpg")
#------------------------------------------- Resizing Background Image to fit in window -------------------------------------------#
Space=(" "*(int(GetSystemMetrics(0))//7)) # Adding space befor window title to be displayed in center of window
class Login:
def __init__(self, Login_Page = Tk()):
self.Login_Page = Login_Page # Initializing login page
self.Login_Page.title(Space+"COMPASS Referral Form")
self.Login_Page.geometry(f"{GetSystemMetrics(0)}x{GetSystemMetrics(1)}+0+0")
self.Login_Page.wm_iconbitmap('Image/Icon.ico')
self.Login_Page.protocol("WM_DELETE_WINDOW",self.Exit_Function)
self.BG_Image = ImageTk.PhotoImage(file="Image/BG_Image_Resized.jpg")
BG = Label(self.Login_Page, image=self.BG_Image).pack()
#------------------------------------------- Login Frame Designer -------------------------------------------#
Login_Frame = Frame(self.Login_Page, bg="white")
Login_Frame.place(x=20, y=50, width=580, height=400)
self.Email_Id = StringVar()
self.Password = StringVar()
Title = Label(Login_Frame, text="Login Here", font=Title_Font, bg="white", fg='#08A3D2').place(x=250, y=0)
Email_Label = Label(Login_Frame, text="Username or Email Address", font=Label_Font, bg="white", fg='grey').place(x=100, y=100)
Password_Label = Label(Login_Frame, text="Password", font=Label_Font, bg="white", fg='grey').place(x=100, y=190)
Email_Entry= Entry(Login_Frame, textvariable=self.Email_Id, font=Entry_Font, bg="light grey").place(x=100, y=140, width=350, height=30)
Password_Entry = Entry(Login_Frame, textvariable=self.Password, font=Entry_Font, bg="light grey", show="*").place(x=100, y=230, width=350, height=30)
Login_Button = Button(Login_Frame, command=self.Login_Function, text="Login", font=Button_Font, fg="white", bg="#B00857", cursor="hand2").place(x=100, y=300, width=150, height=40)
Exit_Button = Button(Login_Frame, command=self.Exit_Function, text="Exit", font=Button_Font, fg="white", bg="dark red", cursor="hand2").place(x=300, y=300, width=150, height=40)
Forgot_Button = Button(Login_Frame, command=self.Forget_Password_Window, text="Forgot Password?", font=Entry_Font, bg="white", fg="#B00857", bd=0, cursor="hand2").place(x=270, y=190)
def Login_Function(self):
if self.Email_Id.get()=="" or self.Password.get()=="":
messagebox.showerror("Error", "All fields are required", parent=self.Login_Page)
else:
try:
con = sqlite3.connect('Database/COMPASS_PRF.db')
cur = con.cursor()
cur.execute("SELECT * FROM Login_Authentication where Email_Id=? AND Password=?", (self.Email_Id.get(), self.Password.get()))
row = cur.fetchone()
if row ==None:
messagebox.showerror("Error","Invalid email or password", parent=self.Login_Page)
else:
messagebox.showinfo("Login Success","Welcome, You have successfully logged in.!!!")
self.Login_Page.destroy()
con.close()
except Exception as es:
messagebox.showerror("Error", f"Error Due to {str(es)}", parent=self.Login_Page)
def Exit_Function(self):
if messagebox.askokcancel("Exit", "Are you sure you want to exit?"):
exit()
def Forget_Password_Window(self):
pass
def MainLoop(self):
self.Login_Page.mainloop()
Login_Window = Login()
Login_Window.MainLoop()
class Main(Login):
def __init__(self, Main_Window=Tk()):
self.Main_Window = Main_Window
self.Main_Window.title(Space+"Referral Form - Main Window")
self.Main_Window.geometry(f"{GetSystemMetrics(0)}x{GetSystemMetrics(1)}+0+0")
self.Main_Window.wm_iconbitmap('Image/Icon.ico')
# self.Main_Window.protocol("WM_DELETE_WINDOW",self.Exit_Function)
# BG = Label(self.Main_Window, text=f"{Login.Email_Id.get()}").pack()
def MainLoop(self):
self.Main_Window.mainloop()
Main_Window = Main()
Main_Window.MainLoop()