首页 文章

我在tkinker中有一个Toplevel按钮,我要关闭窗口然后执行一个功能

提问于
浏览
0

到目前为止,我创建了一个带有一些功能按钮/菜单的框架 . 我的一个菜单按钮打开一个新的Toplevel窗口 . 这个新的Toplevel窗口基本上是一个论坛填写页面 . 在页面的末尾是一个完成按钮 . 这个完成按钮在离开根窗口时关闭了tkiniter Toplevel窗口 . 我希望在按下按钮后执行代码行 .

我的第一个想法是设置完成按钮命令以使窗口“撤回” . 如果我这样做,它确实撤销了窗口但是如果我尝试运行if检查,则在按下按钮之前已经检查过检查 . 对此最简单的解决方案是创建一个函数来撤销窗口并执行其他我想要的东西,但是,当我尝试传递函数作为Toplevel完成按钮的命令时,它不会调用该函数 .

任何建议如何使按钮关闭窗口,然后执行其他代码(仅在按下按钮后)将不胜感激 . 我已经附上了下面的代码部分 . 预先感谢您的帮助 . (旁注计算机类在另一个文件中处理) . (我使用的是Python 3.x)

def draw_new_computer_entry(self):
    t = Toplevel(self)
    t.resizable(0,0)
    t.wm_title("Solution Center: Add New Computer")
    t.geometry("300x300")
    nameLabel = Label(t, text="Computer Name:")
    nameLabel.place(x=0,y=10)

    a = computer()

    comp_nameEntry = Entry(t, width=30)
    comp_nameEntry.place(master=None, x=100, y=10)
    a.name = comp_nameEntry.get()

    comp_makerEntry= Entry(t,width=30)
    comp_makerEntry.place(x=100, y=50)
    a.maker = comp_makerEntry.get()

    makerLabel= Label(t, text="Maker:")
    makerLabel.place(x=55,y=50)

    graphics_cardEntry= Entry(t, width=30)
    graphics_cardEntry.place(x=100,y=90)
    a.gpu = graphics_cardEntry.get()

    graphics_cardLabel= Label(t, text="Graphics Card:")
    graphics_cardLabel.place(x=15, y=90)

    processorEntry= Entry(t, width=30)
    processorEntry.place(x=100, y=130)
    a.processor = processorEntry.get()

    processorLabel= Label(t, text="Processor:")
    processorLabel.place(x=38, y=130)

    hard_driveEntry= Entry(t, width=30)
    hard_driveEntry.place(x=100, y=170)
    a.hard_drive = hard_driveEntry.get()

    hard_driveLabel= Label(t, text="Hard Drive:")
    hard_driveLabel.place(x=30, y=170)

    ramEntry= Entry(t, width=30)
    ramEntry.place(x=100, y=210)

    ramLabel= Label(t,text="Ram:")
    ramLabel.place(x=65,y=210)
    doneButton = Button(t, text="done", command=t.withdraw)
    doneButton.place(x=265, y=275)

1 回答

  • 1

    您所要做的就是创建一个关闭窗口然后执行其他代码的函数 .

    以下是您的代码片段:

    def donePressed():
        t.withdraw() # Or t.destroy(), depending if you need to open this window again
        # Enter Code Here
        print('code after window close')
    doneButton = Button(t, text="done", command=donePressed)
    doneButton.place(x=265, y=275)
    

相关问题