如何从类中继承小部件并在我的顶层使用它们而无需重写代码

时间:2018-03-18 07:15:10

标签: class tkinter

嗨我正在开发一个应用程序,但我决定查看类,所以我重新编写了一些,因为我必须使用与主要Tk完全相同的小部件。但我对使用继承的期望似乎并不奏效。

顺便说一句,这不是缩进错误,这是我第一次使用Stackoverflow提出问题,所以当我复制时它似乎无意中发现了一些代码

    from tkinter import *
    from tkinter import ttk

    class MainWindow(Tk):
       def __init__(self,master,*args):
    self.master = master
    Tk.__init__(self,master,*args)
    self.notebook=ttk.Notebook(self)
    self.notebook.grid()

    #Creating frames
    def widgets(self):
        self.main_frame=Frame(self.notebook)
        self.searching_frame=Frame(self.notebook)
        self.img_frame=Frame(self.main_frame,height=60,width=40)
        self.data_frame=Frame(self.main_frame,height=90,width=90)
        self.data_frame.config(bg='orange')
        self.notebook.add(self.main_frame,text='main frame')
        self.notebook.add(self.searching_frame,text='Frame two')

        #Pack frames

        self.data_frame.grid(row=0,column=1)
        self.img_frame.grid(row=0,column=0)


        #Creating widgets
        self.canvas=Canvas(self.img_frame,height=200,width=300,bg='skyblue')
        self.f_name_lbl=Label(self.data_frame,text="First name:")
        self.l_name_lbl=Label(self.data_frame,text="Last name:")
        self.email_lbl=Label(self.data_frame,text="Email:")
        self.phone_lbl=Label(self.data_frame,text="Phone:")
        self.address_lbl=Label(self.data_frame,text="Address:")
        self.price_lbl=Label(self.data_frame,text="Price:")
                self.description_entry=Text(self.img_frame,height=10,width=45,relief=SUNKEN,wrap=WORD)

        #Pack the widgets
        self.canvas.grid(row=0,column=0)
        self.f_name_lbl.grid(row=0,column=0,padx=20)
        self.l_name_lbl.grid(row=2,column=0,padx=20)
        self.email_lbl.grid(row=4,column=0,padx=20)
        self.phone_lbl.grid(row=0,column=1,padx=20)
        self.address_lbl.grid(row=2,column=1,padx=20)
        self.price_lbl.grid(row=4,column=1,padx=20)
        self.description_entry.grid(row=2,column=0)


        #Creating entries
        self.f_name_entry=Entry(self.data_frame) #creates an entry for 'first name'
        self.l_name_entry=Entry(self.data_frame)
        self.email_entry=Entry(self.data_frame)
        self.phone_entry=Entry(self.data_frame)
        self.address_entry=Entry(self.data_frame)
        self.price_entry=Entry(self.data_frame)
         self.description_entry=Text(self.img_frame,height=10,width=45,relief=SUNKEN,wrap=WORD)

        #Pack the entries
        self.f_name_entry.grid(row=1,column=0,padx=5,pady=5)
        self.l_name_entry.grid(row=3,column=0,padx=5,pady=5)
        self.email_entry.grid(row=5,column=0,padx=5,pady=5)
        self.phone_entry.grid(row=1,column=1,padx=5)
        self.address_entry.grid(row=3,column=1)
        self.price_entry.grid(row=5,column=1)

        self.newwindow=Button(self.data_frame,text='New window',command=window_2)
        self.newwindow.grid(row=6,column=0,columnspan=2)

    widgets(self)



      app=MainWindow(None)
      app.mainloop()

1 个答案:

答案 0 :(得分:0)

这可以胜任吗?您必须填写window_2方法,但除此之外,它就在那里。编辑 - 通过让类直接调用自己,我已经删除了对window_2方法或类的需求:

from tkinter import *
from tkinter import ttk

class MainWindow(Tk):
    def __init__(self,master,*args):
        self.master = master
        Tk.__init__(self,master,*args)
        self.notebook=ttk.Notebook(self)
        self.notebook.grid()
        self.widgets ()
        self.focus_force()

    #Creating frames
    def widgets(self):
        self.main_frame=Frame(self.notebook)
        self.searching_frame=Frame(self.notebook)
        self.img_frame=Frame(self.main_frame,height=60,width=40)
        self.data_frame=Frame(self.main_frame,height=90,width=90)
        self.data_frame.config(bg='orange')
        self.notebook.add(self.main_frame,text='main frame')
        self.notebook.add(self.searching_frame,text='Frame two')

        #Pack frames

        self.data_frame.grid(row=0,column=1)
        self.img_frame.grid(row=0,column=0)


        #Creating widgets
        self.canvas=Canvas(self.img_frame,height=200,width=300,bg='skyblue')
        self.f_name_lbl=Label(self.data_frame,text="First name:")
        self.l_name_lbl=Label(self.data_frame,text="Last name:")
        self.email_lbl=Label(self.data_frame,text="Email:")
        self.phone_lbl=Label(self.data_frame,text="Phone:")
        self.address_lbl=Label(self.data_frame,text="Address:")
        self.price_lbl=Label(self.data_frame,text="Price:")
        self.description_entry=Text(self.img_frame,height=10,width=45,relief=SUNKEN,wrap=WORD)

        #Pack the widgets
        self.canvas.grid(row=0,column=0)
        self.f_name_lbl.grid(row=0,column=0,padx=20)
        self.l_name_lbl.grid(row=2,column=0,padx=20)
        self.email_lbl.grid(row=4,column=0,padx=20)
        self.phone_lbl.grid(row=0,column=1,padx=20)
        self.address_lbl.grid(row=2,column=1,padx=20)
        self.price_lbl.grid(row=4,column=1,padx=20)
        self.description_entry.grid(row=2,column=0)


        #Creating entries
        self.f_name_entry=Entry(self.data_frame) #creates an entry for 'first name'
        self.l_name_entry=Entry(self.data_frame)
        self.email_entry=Entry(self.data_frame)
        self.phone_entry=Entry(self.data_frame)
        self.address_entry=Entry(self.data_frame)
        self.price_entry=Entry(self.data_frame)
        self.description_entry=Text(self.img_frame,height=10,width=45,relief=SUNKEN,wrap=WORD)

        #Pack the entries
        self.f_name_entry.grid(row=1,column=0,padx=5,pady=5)
        self.l_name_entry.grid(row=3,column=0,padx=5,pady=5)
        self.email_entry.grid(row=5,column=0,padx=5,pady=5)
        self.phone_entry.grid(row=1,column=1,padx=5)
        self.address_entry.grid(row=3,column=1)
        self.price_entry.grid(row=5,column=1)

        self.newwindow=Button(self.data_frame,text='New window',command=lambda:MainWindow(None))
        self.newwindow.grid(row=6,column=0,columnspan=2)

app=MainWindow(None)
app.mainloop()