Python类-从父类继承属性到顶层

我正在尝试使用 OOP 方法来创建从父类继承到顶层的类。我想得到上课的self.txtofName时间。但它会引发“AttributeError:类型对象'Window1'没有属性'txtofName'”。谁能帮我?Window1Window2


from tkinter import *

from PIL import Image, ImageTk

import time

from tkinter import ttk


class Window1(object):

    def __init__(self, master):

        self.master = master

        self.master.geometry = ('1600x750+0+0')

        self.master.config(bg='powder blue')

        self.master.title('ALVO HOTEL')

        

        # ==========Framing============

        self.ftop = Frame(master, width=1600, height=100, bg='powder blue', relief=RIDGE, pady=20)

        self.ftop.grid(columnspan=3, column=0, row=0)

        self.f1 = Frame(master,width=800,height=700,relief=SUNKEN, bg='powder blue', pady=50, bd=3)

        self.f1.grid(column=1, row=2, sticky="nsew")

        self.f4 = Frame(master, width=100, height=700, relief=SUNKEN, bg='powder blue', pady=50, padx=20, bd=3)

        self.f4.grid(column=2, row=2, sticky="nsew")



        self.labelofName = Label(self.f1, font=('arial', 16, 'bold'), text='Name: ', bg='powder blue', bd=10, anchor=W)

        self.labelofName.grid(row=0, column=0, pady=(20, 10))

        self.txtofName = Entry(self.f1, font=('arial', 16, 'bold'), bd=10, insertwidth=4, bg='white', justify='left')

        self.txtofName.grid(row=0, column=1, pady=(20, 10))


        self.btnNext = Button(self.f1, padx=10, pady=8, bd=10, fg='white', font=('arial', 10, 'bold'), width=10, text='Next Page', bg='green', command=self.new_window_pop)

        self.btnNext.grid(row=6, column=1, sticky=E)

        

    def new_window_pop(self):

        Window2(Toplevel(self.master))



慕田峪9158850
浏览 130回答 2
2回答

HUWWW

嗨詹姆斯我认为问题是你让 Windows2 类继承 Windows1 类是错误的。我做了一些改变,比如def new_window_pop(self):        #avoid declaring the class type here        #Window2(Toplevel(self.master))        #and pass self because you can thought to self as master...        Window2(self)在windows2中,请注意我们将geometry、config、title声明为self,这里self是新窗口,self.parent是window1#declare class type directly...class Window2(Toplevel):    def __init__(self, parent):    super().__init__()    #Windows2 recive a parent....    self.parent = parent    self.geometry = ('1350x750+0+0')    self.config(bg='powder blue')    self.title('User Information and Payment')最重要的是 #here the reference is wrong    #name = Window1.txtofName.get()    #the right reference is self.parent....    name = self.parent.txtofName.get()    self.FirstName = StringVar()    #here, if you need to use the textvariable you must first assign it    self.FirstName.set(name)这是完整的故事,尝试一下。from tkinter import *from PIL import Image, ImageTkimport timefrom tkinter import ttkclass Window1(object):    def __init__(self, master):        self.master = master        self.master.geometry = ('1600x750+0+0')        self.master.config(bg='powder blue')        self.master.title('ALVO HOTEL')                # ==========Framing============        self.ftop = Frame(master, width=1600, height=100, bg='powder blue', relief=RIDGE, pady=20)        self.ftop.grid(columnspan=3, column=0, row=0)        self.f1 = Frame(master,width=800,height=700,relief=SUNKEN, bg='powder blue', pady=50, bd=3)        self.f1.grid(column=1, row=2, sticky="nsew")        self.f4 = Frame(master, width=100, height=700, relief=SUNKEN, bg='powder blue', pady=50, padx=20, bd=3)        self.f4.grid(column=2, row=2, sticky="nsew")        self.labelofName = Label(self.f1, font=('arial', 16, 'bold'), text='Name: ', bg='powder blue', bd=10, anchor=W)        self.labelofName.grid(row=0, column=0, pady=(20, 10))        self.txtofName = Entry(self.f1, font=('arial', 16, 'bold'), bd=10, insertwidth=4, bg='white', justify='left')        self.txtofName.grid(row=0, column=1, pady=(20, 10))        self.btnNext = Button(self.f1, padx=10, pady=8, bd=10, fg='white', font=('arial', 10, 'bold'), width=10, text='Next Page', bg='green', command=self.new_window_pop)        self.btnNext.grid(row=6, column=1, sticky=E)            def new_window_pop(self):        #avoid declaring the class type here        #Window2(Toplevel(self.master))        #and pass self...self = master        Window2(self)#declare class type directly...class Window2(Toplevel):    def __init__(self, parent):        super().__init__()        #Windows2 recive a parent....        self.parent = parent        self.geometry = ('1350x750+0+0')        self.config(bg='powder blue')        self.title('User Information and Payment')        #notice this Frame(self,....        self.ftitle = Frame(self, bd=10, width=1350, bg='powder blue', relief=RIDGE, padx=20)        self.ftitle.grid(columnspan=3, column=0, row=0)        self.ftitle2 = Frame(self, bd=10, width=1350, height=100, bg='powder blue', relief=RIDGE, padx=20)        self.ftitle2.grid(columnspan=3, column=0, row=3, pady=(0,20))        self.fbutton = Frame(self, width=1350, height=50, bg='powder blue', relief=RIDGE, padx=10, pady=20)        self.fbutton.grid(columnspan=3, column=0, row=2)        self.finfomain = Frame(self, bd=10, width=1350, height=400, bg='powder blue', relief=RIDGE)        self.finfomain.grid(columnspan=3, column=0, row=1)        self.finfo1 = Frame(self.finfomain, bd=5, width=900, height=300, bg='powder blue', relief=RIDGE, padx=10)        self.finfo1.grid(column=0, row=0)        self.finfo1a = Frame(self.finfomain, bd=5, width=900, height=100, bg='powder blue', relief=RIDGE, padx=20)        self.finfo1a.grid(column=0, row=1)        self.finfo2 = Frame(self.finfomain, bd=5, width=450, height=400, bg='powder blue', relief=RIDGE, padx=20)        self.finfo2.grid(column=1, row=0, rowspan=2)        #here the reference is wrong        #name = Window1.txtofName.get()        #the right reference is self.parent....        name = self.parent.txtofName.get()        self.FirstName = StringVar()        #here, if you need to use the textvariable you must first assign it        self.FirstName.set(name)        self.lblfirstname = Label(self.finfo1, font=('arial', 16, 'bold'), bg='powderblue', text='First Name:', padx=2)        self.lblfirstname.grid(row=0, column=0, sticky=W)        self.txtfirstfname = Entry(self.finfo1, font=('arial', 16, 'bold'), textvariable=self.FirstName, bd=3, insertwidth=5, bg='white', justify='left')        self.txtfirstfname.grid(row=0, column=1)                def main():    root = Tk()    app = Window1(root)    root.mainloop()if __name__ == '__main__':    main()

白猪掌柜的

按着这些次序不要这样做 app = Window1(root) 因为你已经在 Window2 中继承了 Window1所以,使用super在Window2中初始化super().__init__(master)现在您可以为 Window1 执行任何操作,为按钮分配命令 self.btnNext.config(command=self.new_window_pop)现在为顶级窗口创建一个方法:这是您的完整代码:from tkinter import *from PIL import Image, ImageTkimport timefrom tkinter import ttkclass Window1(object):    def __init__(self, master):        self.master = master        self.master.geometry = ('1600x750+0+0')        self.master.config(bg='powder blue')        self.master.title('ALVO HOTEL')        # ==========Framing============        self.ftop = Frame(master, width=1600, height=100, bg='powder blue', relief=RIDGE, pady=20)        self.ftop.grid(columnspan=3, column=0, row=0)        self.f1 = Frame(master, width=800, height=700, relief=SUNKEN, bg='powder blue', pady=50, bd=3)        self.f1.grid(column=1, row=2, sticky="nsew")        self.f4 = Frame(master, width=100, height=700, relief=SUNKEN, bg='powder blue', pady=50, padx=20, bd=3)        self.f4.grid(column=2, row=2, sticky="nsew")        self.labelofName = Label(self.f1, font=('arial', 16, 'bold'), text='Name: ', bg='powder blue', bd=10, anchor=W)        self.labelofName.grid(row=0, column=0, pady=(20, 10))        self.txtofName = Entry(self.f1, font=('arial', 16, 'bold'), bd=10, insertwidth=4, bg='white', justify='left')        self.txtofName.grid(row=0, column=1, pady=(20, 10))        self.btnNext = Button(self.f1, padx=10, pady=8, bd=10, fg='white', font=('arial', 10, 'bold'), width=10,                              text='Next Page', bg='green')        self.btnNext.grid(row=6, column=1, sticky=E)class Window2(Window1):    def __init__(self, master):        super().__init__(master)        self.btnNext.config(command=self.new_window_pop)    def new_window_pop(self):        self.master = Toplevel(self.master)        self.master.geometry = '1350x750+0+0'        self.master.config(bg='powder blue')        self.master.title('User Information and Payment')        self.ftitle = Frame(self.master, bd=10, width=1350, bg='powder blue', relief=RIDGE, padx=20)        self.ftitle.grid(columnspan=3, column=0, row=0)        self.ftitle2 = Frame(self.master, bd=10, width=1350, height=100, bg='powder blue', relief=RIDGE, padx=20)        self.ftitle2.grid(columnspan=3, column=0, row=3, pady=(0, 20))        self.fbutton = Frame(self.master, width=1350, height=50, bg='powder blue', relief=RIDGE, padx=10, pady=20)        self.fbutton.grid(columnspan=3, column=0, row=2)        self.finfomain = Frame(self.master, bd=10, width=1350, height=400, bg='powder blue', relief=RIDGE)        self.finfomain.grid(columnspan=3, column=0, row=1)        self.finfo1 = Frame(self.finfomain, bd=5, width=900, height=300, bg='powder blue', relief=RIDGE, padx=10)        self.finfo1.grid(column=0, row=0)        self.finfo1a = Frame(self.finfomain, bd=5, width=900, height=100, bg='powder blue', relief=RIDGE, padx=20)        self.finfo1a.grid(column=0, row=1)        self.finfo2 = Frame(self.finfomain, bd=5, width=450, height=400, bg='powder blue', relief=RIDGE, padx=20)        self.finfo2.grid(column=1, row=0, rowspan=2)        name = self.txtofName.get()        self.FirstName = StringVar()        self.lblfirstname = Label(self.finfo1, font=('arial', 16, 'bold'), bg='powderblue', text='First Name:', padx=2)        self.lblfirstname.grid(row=0, column=0, sticky=W)        self.txtfirstfname = Entry(self.finfo1, font=('arial', 16, 'bold'), textvariable=self.FirstName, bd=3,                                   insertwidth=5, bg='white', justify='left')        self.txtfirstfname.grid(row=0, column=1)def main():    root = Tk()    app = Window2(root)    root.mainloop()if __name__ == '__main__':    main()如果您需要任何解释,请在评论中询问。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python