-1

When I run my Python GUI script it will give you a drop down box to choose a website. The drop down box reads from a list called "sites". When I choose "Fareham" buttons come up giving me a choice of "Custom Date", "Last Week", "Week to Current" if I press custom date 2 entry boxes come up, if I change my choice by pressing "week to current" 1 entry box is meant to come up.

When I press "week to current" just after pressing "custom date" the 2 entry boxes "custom date" creates don't disappear. How can I make the entry boxes disappear if my choice changes?

Code:

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()
Ethan Field
  • 4,646
  • 3
  • 22
  • 43
  • Read about [destroying-a-dynamically-created-widget](https://stackoverflow.com/questions/17512297/destroying-a-dynamically-created-widget) – stovfl Nov 16 '18 at 11:34

1 Answers1

1

The Entry widgets don't disappear because you haven't told them to.

You need to give a command to remove the widgets in order to make them go away.

Widgets are destroyed with the .destroy() function. To do this, you will need a way to refer to the two widgets entry4 and entry5 outside of the function where they are declared. There are two easy methods to achieve this.

The first (and one I would recommend) is to move to an OOP style in your program. Bryan's answer here is a good starting point for achieving this.

The second is to make the variables global, this would involve adding the below snippet in the custom() function:

global entry4
global entry5

Either way, you will then be able to refer to the widgets outside of the function and can then call .destroy() on them like below:


Method 1:

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

Method 2:

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

Note, however, that this does not destroy the labels you have also created. As these are anonymous widgets they will be harder to remove. Check out this answer for some information.

Ethan Field
  • 4,646
  • 3
  • 22
  • 43