简体   繁体   English

调用类并从另一个类继承-Python 3

[英]Call class and inherit from another class - python 3

I am in a trouble. 我有麻烦了。 I wrote two classes, the first is the main gui for the software and the second is the login. 我写了两个类,第一个是软件的主要gui,第二个是登录名。 Now, in the login window I need to write a small validation function. 现在,在登录窗口中,我需要编写一个小的验证功能。 I wrote it but I need after I validate this function calls the main gui class and inherits from Tk . 我写了它,但是在验证此函数调用主gui类并从Tk继承后,我需要它。 I know my code is very poor but I need help please. 我知道我的代码很差,但是请帮忙。

class main_gui(Frame):
    def __init__(self,master, self.username, self.password):
        self.path = os.getcwd()
        self.master = master
        self.username = StringVar()
        self.password = StringVar()
        master.title("main menu")
        master.geometry("800x600")
        master.config(background="white")
        master.resizable(width="false", height="false")
        master.iconbitmap(self.path + r"/img/icon.ico")

        self.mmenu = Menu(master)
        self.menubar = Menu(self.mmenu, tearoff=0)
        self.menubar.add_command(label="about", command=aboutme)
        self.menubar.add_command(label="Exit!", command=master.destroy)
        self.mmenu.add_cascade(label="menu", menu=self.menubar)
        master.config(menu=self.mmenu)

class login(main_gui):
    def __init__(self):
        self.master.title("login")
        self.master.geometry("350x250")
        self.master.config(background="white")
        self.master.resizable(width="false", height="false")
        self.master.iconbitmap(self.path + r"/img/icon.ico")

        self.user_label = Label(master, text="user name")
        self.user_label.pack()
        self.user_value = Entry(master, textvariable=self.username)
        self.user_value.pack()

        self.password_label = Label(master, text="password")
        self.password_label.pack()
        self.password_value = Entry(master, textvariable=self.password)
        self.password_value.pack()

        self.login = Button(master, text="login" )
        self.login.pack()

    def validation(self):
        if self.username.get() == "admin" :
           main_gui()           
           root = Tk()
           hm = main_gui(root)
           root.mainloop()

The initialisation method of your main_gui class isn't quite right. 您的main_gui类的初始化方法不太正确。 You need to change it to something like the following. 您需要将其更改为以下内容。

def __init__(self, master, username, password):
    self.path = os.getcwd()
    self.master = master
    self.username = username
    self.password = password
    master.title("main menu")
    master.geometry("800x600")
    master.config(background="white")
    master.resizable(width="false", height="false")
    master.iconbitmap(self.path + r"/img/icon.ico")

    self.mmenu = Menu(master)
    self.menubar = Menu(self.mmenu ,tearoff=0)
    self.menubar.add_command(label="about",command=aboutme)
    self.menubar.add_command(label="Exit!", command=master.destroy)
    self.mmenu.add_cascade(label="menu", menu=self.menubar)
    master.config(menu=self.mmenu)

Do you see what changes I made? 您看到我做了什么更改吗? self is passed in to the function as the first argument, and then master and username are passed in without the self. self作为第一个参数传递给函数,然后在没有self.情况下传递masterusername self. . This allows you to assign their values to self.master and self.username in the body of the code. 这使您可以将它们的值分配给代码正文中的self.masterself.username

Hopefully that does the trick for you. 希望这对您有用。

I think your idea is wrong. 我认为您的想法是错误的。 You don't need to inherit from main_gui . 您不需要继承main_gui It make no sense for me. 这对我来说毫无意义。

It looks like you have two separated windows which you could use in two ways. 看来您有两个分开的窗口,可以用两种方式使用。

First: 第一:

  • create Login as main window, 创建“登录”作为主窗口,
  • get login/password, 获取登录名/密码,
  • destroy Login (so yo will not have main window), 销毁登录名(所以您将没有主窗口),
  • create MainGUI as main window (using login/password as parameters) 创建MainGUI作为主窗口(使用登录名/密码作为参数)
  • use MainGUI 使用MainGUI

Second: 第二:

  • create MainGUI as main window (you can hide it or not), 创建MainGUI作为主窗口(可以隐藏它,也可以隐藏它),
  • create Login as child/subwindow/dialog (not as main window) using Toplevel , 使用Toplevel登录名创建为子级/子窗口/对话框(而不是主窗口),
  • get login/password (and put in MainGUI which is Login`s parent) 获取登录名/密码(并输入作为Mainlog的父级的MainGUI)
  • destroy Login (so yo still have MainGUI as main window), 销毁登录名(所以您仍然将MainGUI作为主窗口),
  • use MainGUI (after you unhide it) 使用MainGUI(取消隐藏后)

Both situation doesn't need to inherit 两种情况都不需要继承


First version could look like this: 第一个版本可能如下所示:

from tkinter import *
import sys
import os

class MainGui():

    def __init__(self, username, password):
        self.master = Tk()

        self.path = os.getcwd()
        self.path = os.path.realpath(sys.argv[0])

        self.username = StringVar(value=username)
        self.password = StringVar(value=password)

        self.master.title("main menu")
        self.master.geometry("800x600")
        self.master.config(background="white")
        self.master.resizable(width="false", height="false")
        #master.iconbitmap(self.path + r"/img/icon.ico")

        self.mmenu = Menu(self.master)
        self.menubar = Menu(self.mmenu ,tearoff=0)
        self.menubar.add_command(label="about",command=self.aboutme)
        self.menubar.add_command(label="Exit!", command=self.master.destroy)
        self.mmenu.add_cascade(label="menu", menu=self.menubar)

        self.master.config(menu=self.mmenu)

        self.master.mainloop()

    def aboutme(self):
        pass


class Login():

    def __init__(self):
        self.master = Tk()

        self.path = os.path.realpath(sys.argv[0])

        self.master.title("login")
        self.master.geometry("350x250")
        self.master.config(background="white")
        self.master.resizable(width="false", height="false")
        #self.master.iconbitmap(self.path + r"/img/icon.ico")

        self.username = StringVar()
        self.password = StringVar()

        self.user_label = Label(self.master, text="user name")
        self.user_label.pack()
        self.user_value = Entry(self.master, textvariable=self.username)
        self.user_value.pack()


        self.password_label = Label(self.master, text="password")
        self.password_label.pack()
        self.password_value = Entry(self.master, textvariable=self.password)
        self.password_value.pack()

        self.login = Button(self.master, text="login", command=self.validation)
        self.login.pack()

        self.message = Label(self.master)
        self.message.pack()

        self.message.mainloop()

    def validation(self):
        if self.username.get() == "admin" :
            self.master.destroy()
            MainGui(self.username.get(), self.password)
        else:
            self.message['text'] = "Wrong !!!"

# --- start ---     

Login()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM