You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Tkinter漫画阅读器开发:清除网格中重叠标签的方法

Fixing Overlapping Labels in Tkinter When Clicking a Button

Hey there, the issue you're facing happens because every time you click the "Get" button, you're creating entirely new Label widgets—the old ones don't go away, they just get covered up by the new labels, causing that overlapping effect.

Here are two straightforward solutions to fix this:

Solution 1: Create Labels Once, Update Their Text Later

This is the more efficient approach. Instead of making new labels every time, create them once when initializing your app, then just update their text content when the button is clicked.

Here's the revised full code:

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

app = tk.Tk()

# Initialize empty labels upfront
mangaid_label = tk.Label(text="")
mangaide_label = tk.Label(text="")
mangaidj_label = tk.Label(text="")

def get_button():
    mid = entry.get()
    if mid == "soap":
        mid = "176758"
    url = f"https://somewebsite/api/gallery/{mid}"
    
    # Update the ID label's text
    mangaid_label.config(text=f"ID : {mid}")
    mangaid_label.grid(column=0, row=4, columnspan=2,)
    
    print(url)
    uf = requests.request(method="get",url=url)
    j_result = uf.json()
    title = j_result['title']
    j_title = title['japanese']
    e_title = title['english']
    
    # Update the title labels' text
    mangaide_label.config(text=f"English Title : {e_title}")
    mangaidj_label.config(text=f"Japanese Title : {j_title}")
    mangaide_label.grid(column=0, row=5, columnspan=2,)
    mangaidj_label.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}/"
    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)

# Place the empty labels in the grid initially
mangaid_label.grid(column=0, row=4, columnspan=2,)
mangaide_label.grid(column=0, row=5, columnspan=2,)
mangaidj_label.grid(column=0, row=6, columnspan=2,)

app.mainloop()

Solution 2: Destroy Old Labels Before Creating New Ones

If you prefer to create new labels each time, you can store references to the old labels and destroy them before making new ones.

Here's how you'd adjust your code:

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

app = tk.Tk()

# Store references to labels, start with None
mangaid_label = None
mangaide_label = None
mangaidj_label = None

def get_button():
    global mangaid_label, mangaide_label, mangaidj_label
    
    # Destroy existing labels if they exist
    if mangaid_label:
        mangaid_label.destroy()
    if mangaide_label:
        mangaide_label.destroy()
    if mangaidj_label:
        mangaidj_label.destroy()
    
    mid = entry.get()
    if mid == "soap":
        mid = "176758"
    url = f"https://somewebsite/api/gallery/{mid}"
    
    # Create new labels and save their references
    mangaid_label = tk.Label(text=f"ID : {mid}")
    mangaid_label.grid(column=0, row=4, columnspan=2,)
    
    print(url)
    uf = requests.request(method="get",url=url)
    j_result = uf.json()
    title = j_result['title']
    j_title = title['japanese']
    e_title = title['english']
    
    mangaide_label = tk.Label(text=f"English Title : {e_title}")
    mangaidj_label = tk.Label(text=f"Japanese Title : {j_title}")
    mangaide_label.grid(column=0, row=5, columnspan=2,)
    mangaidj_label.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}/"
    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()

Quick Notes

  • Solution 1 is better for performance since you're reusing the same widgets instead of creating/destroying them repeatedly.
  • If you ever have dynamic content with variable numbers of widgets, consider using a Frame as a container—you can clear all widgets inside the frame with a loop before adding new content, which is super flexible.

内容的提问来源于stack exchange,提问作者SwapTail

火山引擎 最新活动