[英]How to access variables and functions from different classes in tkinter?
我刚开始使用 python。我有一个 Udemy 课程。 我试图在Tkinter
模块的帮助下创建一个 GUI 应用程序,但我停留在一点上,我不知道如何做到这一点。 我想使用self.Email_Id
从Login class
到Main_Window class
。 我想重用MainLoop function
从Login class
到Main_Window class
。 否则,我必须根据代码可重用性重写 function 这不是约定俗成的。
如果可能的话,请提出建议,有哪些不同的方法可以做到这一点或在多级 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()
您可以访问您的变量,因为Main
是Login
的Child
。 MainLoop
function 应该有一个 window 输入,然后您也可以在class Main
中访问Mainloop
(A Child
具有parent
的所有functions
)。 在这里您可以了解更多关于Parent and Child classes
的信息我希望我已经回答了您的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.