简体   繁体   English

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

[英]How do I access and use the variable in one python class in another class

I have two Python classes created with tkinter to start with. 我有两个使用tkinter创建的Python类。 I have three variables storing different data in the first class, UserLogin. 我在第一个类UserLogin中有三个存储不同数据的变量。 I want to be able access and use the values in two of those variable in another class, HomePage, that is also in the same file. 我希望能够访问和使用另一个类HomePage中的两个变量中的值,该类也位于同一文件中。 The goal is that based on the user level, I want to display certain widgets. 目的是基于用户级别,我要显示某些小部件。 In other words, each user when logged in can only see certain number of widgets which is determined by his user level. 换句话说,每个用户登录后只能看到由其用户级别确定的特定数量的窗口小部件。 The class HomePage is the window containing the widgets to be displayed, while the first class is the login widow. HomePage类是包含要显示的小部件的窗口,而第一类是登录寡妇。 I have a shorter version of my code here. 我在这里有较短的代码版本。 I'm having trouble posting all the codes, hence this shorter version. 我在发布所有代码时遇到了麻烦,因此版本较短。

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''')

The question is not so clear to me, I see you defining variables but hard-coding the values. 这个问题对我来说不太清楚,我看到您在定义变量,但对值进行了硬编码。 Please try to fix the provided code for better understanding the code. 请尝试修复提供的代码,以更好地理解代码。 One way to do what you want is by making HomePage class inherits from UserLogin , in this way you can get Userlogin's variables inside Homepage. 一种方法是通过使HomePage类继承自UserLogin ,从而可以在Homepage中获取Userlogin的变量。 Example: 例:

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)

now if we defined test = Child() then test.show_info() , this will print the values defined in the Parent class. 现在,如果我们定义了test = Child()然后定义了test.show_info() ,则将打印在Parent类中定义的值。

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

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