单击按钮后如何获取子窗口?

时间:2014-02-12 07:28:54

标签: python tkinter

请帮助修复脚本:

import tkinter


class Application(tkinter.Frame):

    def __init__(self, parent):
        tkinter.Frame.__init__(self, parent, bg='yellow')
        self.pack(side = 'top', fill = 'x')
        self.make_elements()

    def make_elements(self):
        tollbarFrame=tkinter.Frame(self)
        tollbarFrame.pack(side='top', fill='x')

        tool3=tkinter.Button(tollbarFrame, text='Add record', command=self.add_record())
        tool3.pack(side='left')


        contentFrame=tkinter.Frame(self)
        contentFrame.pack(side='top', fill='x')

        butt = tkinter.Button(contentFrame, text='qwerer')
        butt.pack()

    def add_record(self):
        child = tkinter.Toplevel()
        bu = tkinter.Button(child, text='sdfsf')
        bu.pack()


if __name__ == '__main__':
    root = tkinter.Tk()
    root.title('dvd list')
    root.geometry('700x500')
    Application(root)
    root.mainloop()

加载子窗口后显示“child”。在用户点击“添加记录”按钮后,我想出了这个子窗口的想法。

1 个答案:

答案 0 :(得分:1)

创建按钮时,请勿调用回调函数。改为:

tool3=tkinter.Button(tollbarFrame, text='Add record', command=self.add_record)