如何在另一个类的一个python类中访问和使用变量

时间:2019-05-25 08:40:36

标签: python python-3.x

我首先用tkinter创建了两个Python类。我在第一个类UserLogin中有三个存储不同数据的变量。我希望能够访问和使用另一个类HomePage中的两个变量中的值,该类也位于同一文件中。目的是基于用户级别,我要显示某些小部件。换句话说,每个用户登录后只能看到由其用户级别确定的特定数量的窗口小部件。 HomePage类是包含要显示的小部件的窗口,而第一类是登录寡妇。我在这里有较短的代码版本。我在发布所有代码时遇到了麻烦,因此版本较短。

import backend    
class UserLogin:
    def __init__(self, top):
        self.top = top
        '''This class configures and populates the toplevel window.
           top is the toplevel containing window.'''
        _bgcolor = '#d9d9d9'  # X11 color: 'gray85'
        _fgcolor = '#000000'  # X11 color: 'black'
        _compcolor = '#d9d9d9' # X11 color: 'gray85'
        _ana1color = '#d9d9d9' # X11 color: 'gray85'
        _ana2color = '#ececec' # Closest X11 color: 'gray92


    top.geometry("676x450+458+150")
    top.title("Some title here")
    top.configure(background="#d9d9d9")
    top.configure(highlightbackground="#d9d9d9")
    top.configure(highlightcolor="black")

    username = StringVar()
    password = StringVar()

    def user_login():
        if len(username.get()) != 0 and len(password.get()) != 0:
            result = backend.log(username.get())
            if not result:
                self.messageBox.delete('1.0', END)
                self.messageBox.insert(END, 'Invalid Login')
            else:
                level = result[0]
                user_name = result[1]
                pass_word = result[2]
                if user_name == username.get() and pass_word == password.get():
                    new_window()
                else:
                    self.messageBox.delete('1.0', END)
                    self.messageBox.insert(END, 'Invalid Login')

        else:
            self.messageBox.delete('1.0', END)
            self.messageBox.insert(END, 'Username and Password required')

    # This function opens the window created in Class HomePage
    def new_window():
        self.newWindow = Toplevel(self.top)
        login = HomePage(self.newWindow)

class HomePage
class HomePage:
    def __init__(self, top):
        self.top = top
        '''This class configures and populates the toplevel window.
           top is the toplevel containing window.'''
        _bgcolor = '#d9d9d9'  # X11 color: 'gray85'
        _fgcolor = '#000000'  # X11 color: 'black'
        _compcolor = '#d9d9d9' # X11 color: 'gray85'
        _ana1color = '#d9d9d9' # X11 color: 'gray85' 
        _ana2color = '#ececec' # Closest X11 color: 'gray92' 
        self.style = ttk.Style()
        if sys.platform == "win32":
            self.style.theme_use('winnative')
        self.style.configure('.',background=_bgcolor)
        self.style.configure('.',foreground=_fgcolor)
        self.style.configure('.',font="TkDefaultFont")
        self.style.map('.',background=
            [('selected', _compcolor), ('active',_ana2color)])

    top.geometry("903x568+392+150")
    top.title("Some title here")
    top.configure(background="#d9d9d9")
    top.configure(highlightbackground="#d9d9d9")
    top.configure(highlightcolor="black")

    self.linksFrame = Frame(top)
    self.linksFrame.place(relx=0.035, rely=0.052, relheight=0.875
                          , relwidth=0.272)
    self.linksFrame.configure(relief='groove')
    self.linksFrame.configure(borderwidth="2")
    self.linksFrame.configure(relief='groove')
    self.linksFrame.configure(background="#d9d9d9")
    self.linksFrame.configure(highlightbackground="#d9d9d9")
    self.linksFrame.configure(highlightcolor="black")
    self.linksFrame.configure(width=235)

    # Then I want to be able to say here that:
    self.registerPat = Button(self.linksFrame)
    self.registerPat.place(relx=0.043, rely=0.079, height=34, width=217)
    self.registerPat.configure(activebackground="#ececec")
    self.registerPat.configure(activeforeground="#000000")
    self.registerPat.configure(background="#d9d9d9")
    self.registerPat.configure(disabledforeground="#a3a3a3")
    self.registerPat.configure(foreground="#000000")
    self.registerPat.configure(highlightbackground="#d9d9d9")
    self.registerPat.configure(highlightcolor="black")
    self.registerPat.configure(pady="0")
    self.registerPat.configure(relief='groove')
    self.registerPat.configure(text='''Register Patient''')

1 个答案:

答案 0 :(得分:0)

这个问题对我来说还不是很清楚,我看到您定义变量但对值进行硬编码。请尝试修复提供的代码,以更好地理解代码。 一种实现您想要的方式的方法是,使HomePage类继承自UserLogin,这样您就可以在Homepage中获取Userlogin的变量。示例:

class Parent:
    def __init__(self):
        self.name = "Mart"
        self.age = 40
        self.height = 170

class Child(Parent):
    def show_info(self):
        print(self.name, self.age, self.height)

现在,如果我们先定义test = Child(),然后再定义test.show_info(),则将打印在Parent类中定义的值。