Search code examples
pythonjsontkinter

How To Clear a Label Assigned to a Grid in Specific Scenario Tkinter


I'm currently writing a manga reader and viewer program using the tkinter library in python for a gui, Im trying to make it so it lists the titles currently, and as I do this I realize its overlapping them, ive searched far and wide but wasnt able to find a good procedure to lets say "remove/forget" them on the new button press.

My code is listed below:

import json, webbrowser, requests
import tkinter as tk
from tkinter.ttk import *
from urllib import *
import urlopen
from urllib.request import *
import os
os.system("chcp 65001")

app = tk.Tk()

def get_button():
    mid = entry.get()
    if mid == "soap":
        mid = "176758"
    url = f"https://somewebsite/api/gallery/{mid}"
    #label and pack for manga id
    mangaid = tk.Label(text=f"ID : {mid}")
    mangaid.grid(column=0, row=4, columnspan=2,)
    #prints the url
    print(url)
    #open url data
    uf = requests.request(method="get",url=url)
    j_result = uf.json()
    title = j_result['title']
    j_title = title['japanese']
    e_title = title['english']
    #shows the title text
    mangaide = tk.Label(text=f"English Title : {e_title}")
    mangaidj = tk.Label(text=f"Japanese Title : {j_title}")
    mangaide.grid(column=0, row=5, columnspan=2,)
    mangaidj.grid(column=0, row=6, columnspan=2,)

def on_open():
    mid = entry.get()
    if mid == "soap":
        mid = "176758"
    URL = f"https://somewebsite.net/g/{mid}/"
    #opens url
    webbrowser.open(URL, new=2)
    print(URL)
    

enterid = tk.Label(text="Enter ID or Name")
entry = tk.Entry()
button = tk.Button(text="Get", command=get_button)
button2 = tk.Button(text="Open", command=on_open)
enterid.grid(column=0, columnspan=2, pady=(10))
entry.grid(column=0, columnspan=2, padx=(50))
button.grid(row=3, column=0, pady=(10))
button2.grid(row=3,column=1)


app.mainloop()

If you look at lines 29-32 Im assigning a label and placing it on the grid, although when I press the button again, to get new data, it proceeds to do the following:

1st Data Grab

2nd Data Grab

In the first one you can see that it worked perfectly, but in the 2nd grab you can see that it took the previous answers and overlayed them behind, so in gist m trying to figure out a way to fix this, my main goal is to find a way to remove the overlayed text.


Solution

  • Create empty Labels above app.mainloop() like this:

    mangaide = tk.Label()
    mangaidj = tk.Label()
    mangaide.grid(column=0, row=5, columnspan=2,)
    mangaidj.grid(column=0, row=6, columnspan=2,)
    

    and put text on them inside get_button() function with the use of global keyword. Modify your get_button() function like this:

    def get_button():
        global mangaidj, mangaide # Using global keyword to access those Labels
        mid = entry.get()
        if mid == "soap":
            mid = "176758"
        url = f"https://somewebsite/api/gallery/{mid}"
        #label and pack for manga id
        mangaid = tk.Label(text=f"ID : {mid}")
        mangaid.grid(column=0, row=4, columnspan=2,)
        #prints the url
        print(url)
        #open url data
        uf = requests.request(method="get",url=url)
        j_result = uf.json()
        title = j_result['title']
        j_title = title['japanese']
        e_title = title['english']
        #shows the title text
        mangaide.config(text=f"English Title : {e_title}") # These lines will
        mangaidj.config(text=f"Japanese Title : {j_title}") # update the text each time
    

    Hpoe this helps :)