tkinter中的tkinter使值消失(python 3.4.3)

2024-04-19 19:57:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我编写了这段代码,它应该询问用户是否所有的文件都有相同的日期,如果是,他应该将日期写入网格。 输入日期后,两个窗口都应该消失,我想保留日期。 不幸的是,我没有设法让第一个输入框消失,在整个过程之后,输入日期又是[]。你知道吗

 from tkinter import *

    entry_date = []
            if amountfiles == 1:
                def moredates():
                    master.destroy()

                def setdate():
                    def entry_date():
                        entry_date = e1.get()
                        entry_date = str(entry_date)
                        print("Date for all files is: ",entry_date)
                        master.destroy()

                    def quit():
                        sys.exit()

                    master = Tk()
                    Label(master, text="Please enter date (format YYYYMMDD, i.e. 20160824): ").grid(row=0)
                    e1 = Entry(master)
                    e1.grid(row=0, column=1)
                    Button(master, text='Quit', command=master.destroy).grid(row=3, column=1, sticky=W, pady=4)
                    Button(master, text='Insert', command=entry_date).grid(row=2, column=1, sticky=W, pady=4)
                    mainloop( )

                master = Tk()
                Label(master, text="Do all files have the same date?").grid(row=0)
                Button(master, text='No...', command=moredates).grid(row=2, column=0, sticky=W, pady=4)
                Button(master, text='Yes!', command=setdate).grid(row=1, column=0, sticky=W, pady=4)
                Button(master, text='Close & Contiune', command=master.destroy).grid(row=3, column=0, sticky=W, pady=4)
                mainloop( )

Tags: textmasterdatedefcolumnbuttoncommandgrid
1条回答
网友
1楼 · 发布于 2024-04-19 19:57:17

由于在函数setdate()中重新分配了外部master变量,因此调用master.destroy()只会关闭新的master,而不会关闭外部master。尝试修改函数setdate(),如下所示:

def setdate():
    def append_date():
        date = e1.get() # get the input entry date
        entry_date.append(date) # save the input date
        print("Date for all files is: ", date)
        master.destroy()

    top = Toplevel() # use Toplevel() instead of Tk()
    Label(top, text="Please enter date (format YYYYMMDD, i.e. 20160824): ").grid(row=0)
    e1 = Entry(top)
    e1.grid(row=0, column=1)
    Button(top, text='Quit', command=master.destroy).grid(row=3, column=1, sticky=W, pady=4)
    Button(top, text='Insert', command=append_date).grid(row=2, column=1, sticky=W, pady=4)
    master.wait_window(top) # use Tk.wait_window()

相关问题 更多 >