You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TKinter生成练习按钮文字不显示问题求助

解决方案

按钮文本不显示的核心原因是背景图片覆盖了按钮,同时存在语法错误需要修正:

  • 背景层级问题:用place()添加的背景标签默认在控件最上层,完全遮挡了按钮。给背景标签调用lower()方法,将其置于所有控件底层。
  • 全局变量缩进错误:generate_random_exercise函数内的global exercises_shown未缩进,会触发语法错误,需和函数内代码对齐。
  • 属性名规范:TKinter中控件背景属性标准写法是bg,建议替换background保持统一,避免潜在兼容问题。

关键修正点

  1. 调整背景标签层级:
background_label = Label(bicep, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
background_label.lower()  # 将背景置于底层
  1. 修正全局变量缩进:
def generate_random_exercise():
    global exercises_shown  # 缩进对齐函数内代码
    # 后续逻辑...

完整修正代码

import tkinter as tk
import sqlite3
from tkinter import *
import os
import customtkinter
from PIL import ImageTk, Image
import io
import random


def generate_random_exercise():
    global exercises_shown

    # Check if all exercises have been shown
    if len(exercises_shown) == len(exercises):
        exercise_label.config(text="All exercises have been shown.")
        reps_label.config(text="")
        image_label.config(image="")
        return

    # Get a random exercise that hasn't been shown yet
    exercise = random.choice(exercises)
    while exercise in exercises_shown:
        exercise = random.choice(exercises)

    exercise_name = exercise[0]
    reps = exercise[1]
    image_data = exercise[2]

    # Display the exercise and reps
    exercise_label.config(text="Exercise: " + exercise_name)
    reps_label.config(text="Reps: " + str(reps))

    # Load and display the image
    image = Image.open(io.BytesIO(image_data))
    image = image.resize((400, 300))  # Adjust the size as needed
    photo = ImageTk.PhotoImage(image)
    image_label.config(image=photo)
    image_label.image = photo

    # Add the exercise to the list of shown exercises
    exercises_shown.append(exercise)

# Create a database connection
connection = sqlite3.connect("db/workout.db")
cursor = connection.cursor()

# Create the main application window
bicep = tk.Tk()
bicep.title("Bicep Exercise")
bicep.geometry("500x800")
bicep.resizable(width=False, height=False)

# Create background image
background_image = PhotoImage(file="assets/Aim high.png")
background_label = Label(bicep, image=background_image)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
background_label.lower()  # 将背景置于底层

# Get all exercises from the database
cursor.execute("SELECT exercise, reps, image FROM Biceps")
exercises = cursor.fetchall()

# Create an empty list to track the shown exercises
exercises_shown = []

# Create labels for exercise and reps
exercise_label = tk.Label(bicep, text="Exercise:", bg="black", fg="white")
exercise_label.pack()

reps_label = tk.Label(bicep, text="Reps:", bg="black", fg="white")
reps_label.pack()

# Create an empty label for the image
image_label = tk.Label(bicep)
image_label.pack()

# Create a button to generate random exercise
generate_button = tk.Button(bicep, text="Generate Exercise", 
command=generate_random_exercise, bg="black", fg="white")
generate_button.pack()

# Close the database connection
connection.close()

bicep.mainloop()

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 00:15:07