Python-如何使tkinter输入框在选择更改后消失

时间:2018-11-16 10:41:03

标签: python tkinter

当我运行Python GUI脚本时,它将为您提供一个下拉框以选择网站。下拉框从称为“站点”的列表中读取。 当我选择“ Fareham”按钮时,如果我按自定义日期,则会出现“自定义日期”,“上周”,“到当前周”两个选项,如果我通过按“ week to当前” 1输入框是要出现的。

当我按下“自定义日期”后立即按下“当前星期”时,“自定义日期”创建的2个输入框不会消失。如果我的选择发生更改,如何使输入框消失?

代码:

import os
import tkinter
from tkinter import ttk

sites = [
    "Fareham",
    "Hants",
    "Southampton",
    "Eastleigh",
    "Havant",
    "Gosport",
]

win = tkinter.Tk()

win.geometry("500x500")

win.title("UK Council Web Scraping GUI | Sites Loaded: %s" % len(sites))

tkinter.Label(win, text="Database Path").grid(row=0, sticky="W")

entry1 = tkinter.Entry(win)
entry1.insert(0, "")
entry1.grid(row=0, column=1, sticky="W")

tkinter.Label(win, text="Log File Path").grid(row=1, sticky="W")

entry2 = tkinter.Entry(win)
entry2.insert(0, "")
entry2.grid(row=1, column=1, sticky="W")

tkinter.Label(win, text="Chrome Driver Path").grid(row=2, sticky="W")

entry3 = tkinter.Entry(win)
entry3.insert(0, "")
entry3.grid(row=2, column=1, sticky="W")

variable = tkinter.StringVar(win)
variable.set("Select Website")

homepath = "%homepath%"
choice = 0

def check(*args):
    global choice
    def custom():
        global choice
        choice = 1
        tkinter.Label(win, text="Start Date").grid(row=15, column=0)
        entry4 = tkinter.Entry(win)
        entry4.insert(0, "")
        entry4.grid(row=16, column=0)
        tkinter.Label(win, text="Stop Date").grid(row=15, column=1)
        entry5 = tkinter.Entry(win)
        entry5.insert(0, "")
        entry5.grid(row=16, column=1)

    def week():
        global choice
        choice = 2

    def wtc():
        global choice
        choice = 3
        tkinter.Label(win, text="Weeks").grid(row=15, column=0)
        entry4 = tkinter.Entry(win)
        entry4.insert(0, "")
        entry4.grid(row=16, column=0)

    if args[0] == "Fareham" or args[0] == "Southampton" or args[0] == "Eastleigh" or args[0] == "Havant" or args[0] == "Gosport":
        button1 = tkinter.Button(win, text="Custom Date", height=2, width=12, command=custom).grid(row=3, column=0, sticky="W")
        button2 = tkinter.Button(win, text="Last Week", height=2, width=12, command=week).grid(row=4, column=0, sticky="W")
        button3 = tkinter.Button(win, text="Week to Current", height=2, width=12, command=wtc).grid(row=5, column=0, sticky="W")
    else:
        choice = 0

drop = tkinter.OptionMenu(win, variable, *sites, command=check).grid(row=0, column=2, sticky="E")

def scrape(*args):
    global choice
    if choice == 0:
        os.system(r"cd %s\Anaconda3 && python %s\Desktop\code\urls\%s.py %s %s %s" % (homepath, homepath, variable.get().lower(), entry1.get(), entry2.get(), entry3.get()))

    if choice == 1:
        os.system(r"cd %s\Anaconda3 && python %s\Desktop\code\urls\%s.py %s %s %s 1 %s %s" % (homepath, homepath, variable.get().lower(), entry1.get(), entry2.get(), entry3.get(), entry4.get(), entry5.get()))

    if choice == 2:
        os.system(r"cd %s\Anaconda3 && python %s\Desktop\code\urls\%s.py %s %s %s 2" % (homepath, homepath, variable.get().lower(), entry1.get(), entry2.get(), entry3.get()))

    if choice == 3:
        os.system(r"cd %s\Anaconda3 && python %s\Desktop\code\urls\%s.py %s %s %s 3 %s" % (homepath, homepath, variable.get().lower(), entry1.get(), entry2.get(), entry3.get(), entry4.get()))

button4 = tkinter.Button(win, text="Scrape Website", height=2, width=12, command=scrape).grid(row=6, column=0, sticky="W")

win.mainloop()

1 个答案:

答案 0 :(得分:1)

Entry小部件不会消失,因为您没有告诉他们。

您需要发出命令以删除小部件,以使其消失。

使用.destroy()函数破坏小部件。为此,您将需要一种在声明它们的函数之外引用两个小部件entry4entry5的方法。有两种 easy 方法可以实现这一目标。

第一个(也是我推荐的)是在程序中转换为OOP样式。布莱恩(Bryan)的答案here是实现这一目标的良好起点。

第二个是使变量全局化,这将涉及在custom()函数中添加以下代码段:

global entry4
global entry5

无论哪种方式,您都可以引用函数外部的小部件,然后可以像下面这样对它们调用.destroy()


方法1:

CLASS_NAME.entry4.destroy()
CLASS_NAME.entry5.destroy()

方法2:

global entry4
global entry5
entry4.destroy()
entry5.destroy()

但是请注意,这不会破坏您还创建的标签。由于这些是匿名窗口小部件,因此将很难删除它们。请查看this answer了解更多信息。