【问题标题】:How to access variables and functions from different classes in tkinter?如何从 tkinter 中的不同类访问变量和函数?
【发布时间】:2020-11-22 22:29:39
【问题描述】:

我刚刚开始使用 python。我有一个 Udemy 课程。我试图在Tkinter 模块的帮助下创建一个 GUI 应用程序,但我坚持了一点,我不知道如何做到这一点。 我想使用从Login classMain_Window classself.Email_Id。我想重用MainLoop functionLogin classMain_Window class。否则,我必须根据代码可重用性重写那个不符合约定的函数。

如果可能,请提出建议,有哪些不同的方法可以做到这一点或在多级窗口上工作。下面是我的代码。

提前致谢。

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。其实你可以在Login_Window关闭后通过Login_Window.Email_Id.get()收到邮件。

标签: python class user-interface tkinter functional-programming


【解决方案1】:

您可以访问您的变量,因为Main 是来自LoginChildMainLoop 函数应该有一个窗口输入,然后您还可以访问class Main 中的Mainloop(A Child 具有来自parent 的所有functions)。 Here 你可以了解更多关于Parent and Child classes 我希望我已经回答了你的问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-15
    • 1970-01-01
    • 2020-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多