如何在顶层 window 中引入图像按钮?

How can I introduce a image button in a toplevel window?

我正在 tkinter 中创建一个简单的程序,我想在顶层放置一个图像按钮 window,但是当我这样做时,程序会给我这个回调:

self.tk.call(_flatten((self._w, cmd)) + self._options(cnf))
_tkinter.TclError: image ".!toplevel.!button" doesn't exist

我已经使用了一个全局变量来保存图像,它适用于标签

有些东西是葡萄牙语,但我相信不会打扰

from tkinter import *


def average_opt():
    root2 = Toplevel()
    root2.title('Calculadora')
    root2.geometry('500x500+20+60')

    Label(root2, image=secondary_background, borderwidth=0).place(x=0, y=0, relwidth=1, relheight=1)

    def calculate():
        media = (2 * grade1.get() + 2 * grade2.get() + grade3.get()) / 5
        Label(root2, text='Sua média é {}'.format(media), font=('comic sans ms', 16, 'bold')).place(x=55, y=250)

    # Labels: Show informations for user
    Label(root2, image=parcial_label).place(x=60, y=39)
    Label(root2, image=bimestral_label).place(x=60, y=87)
    Label(root2, image=work_label).place(x=60, y=133)

    # Entrys: Colect the informations from users
    grade1 = IntVar()
    grade2 = IntVar()
    grade3 = IntVar()

    Entry(root2, textvariable=grade1, width=30).place(x=200, y=42)
    Entry(root2, textvariable=grade2, width=30).place(x=200, y=90)
    Entry(root2, textvariable=grade3, width=30).place(x=200, y=137)

    # Buttons: Execute a command
    calculate_button = Button(root2, command=calculate, borderwidth=0)
    calculate_button.config(image=calculate_button, width=100, height=20)
    calculate_button.place(x=300, y=200)


def necessarygrade_opt():
    root4 = Toplevel()
    root4.title('Nota Necessária')
    root4.geometry('400x400+1050+60')

    def calculate2():
        necessarygrade = (5 * wanted_grade.get() - 2 * grade1.get() - grade2.get()) / 2

        if necessarygrade <= 10:
            Label(root4, text='Para obter média {} \n  é necessário a nota {}'.format(wanted_grade.get(), necessarygrade),font=('comic sans ms', 13, 'bold')).place(x=45, y=250)

        else:
            Label(root4, text='É impossível alcançar a \n média com essas notas', font=('comic sans ms', 14, 'bold')).place(x=45, y=250)

    # Labels: Show informations for user
    Label(root4, text='Nota Parcial:', font=('comic sans ms', 13, 'bold')).place(x=30, y=30)
    Label(root4, text='Nota de Trabalho:', font=('comic sans ms', 13, 'bold')).place(x=25, y=80)
    Label(root4, text='Média Desejada:', font=('comic sans ms', 13, 'bold')).place(x=20, y=130)

    # Entry: Colect the informations from users
    grade1 = IntVar()
    grade2 = IntVar()
    wanted_grade = IntVar()

    Entry(root4, textvariable=grade1, width=30).place(x=180, y=37)
    Entry(root4, textvariable=grade2, width=30).place(x=180, y=87)
    Entry(root4, textvariable=wanted_grade, width=30).place(x=180, y=137)

    # Buttons: Execute a command
    Button(root4, text='Calcular', command=calculate2, width=50).place(x=20, y=180)


def yourgrade_opt():
    root3 = Toplevel()
    root3.title('Sua Nota')
    root3.geometry('400x400+600+360')

    def calculate2():
        grade = (achieved_scores.get() / total_scores.get()) * 10
        if achieved_scores.get() > total_scores.get():
            Label(root3, text='É impossivel ultrapassar o\nnúmero máximo de scores',
                  font=('comic sans ms', 12, 'bold'), width=30).place(x=60, y=260)
        else:
            Label(root3, text='A nota alcançada é {}'.format(grade), font=('comic sans ms', 13, 'bold'),
                  width=25, height=5).place(x=57, y=220)

    # Labels: Show informations for user
    Label(root3, text='Scores Totais:', font=('comic sans ms', 13, 'bold')).place(x=25, y=30)
    Label(root3, text='Scores Obtidos:', font=('comic sans ms', 13, 'bold')).place(x=15, y=80)

    # Entry: Colect the informations from users
    total_scores = IntVar()
    achieved_scores = IntVar()

    Entry(root3, textvariable=total_scores, width=30).place(x=180, y=37)
    Entry(root3, textvariable=achieved_scores, width=30).place(x=180, y=87)

    # Buttons: Execute a command
    Button(root3, text='Calcular', command=calculate2, width=50).place(x=20, y=180)


root = Tk()
root.title('Calculadora Escolar')
root.geometry('470x270+550+50')

# Images for backgrounds are loaded here
main_background = PhotoImage(file='background.gif').subsample(1, 1)
secondary_background = PhotoImage(file='secondary.gif').subsample(1, 1)
parcial_label = PhotoImage(file='nota_parcial.gif'). subsample(1, 1)
bimestral_label = PhotoImage(file='nota_bimestral.gif').subsample(1, 1)
work_label = PhotoImage(file='nota_trabalho.gif').subsample(1, 1)
calculate_label = PhotoImage(file='calcular.gif').subsample(1, 1)

# Background
label_background = Label(root, image=main_background).place(x=0, y=0, relwidth=1, relheight=1)


# button for open average window
media_button = Button(root, command=average_opt, borderwidth=0, justify=LEFT)
photo_media = PhotoImage(file="media.gif")
media_button.config(image=photo_media, width='96', height='36')
media_button.place(x=20, y=120)

# button for open necessary grade window
necessarygrade_button = Button(root, command=necessarygrade_opt, borderwidth=0,  justify=LEFT)
photo_necessarygrade = PhotoImage(file='nota_nec.gif')
necessarygrade_button.config(image=photo_necessarygrade, width='150', height='36')
necessarygrade_button.place(x=150, y=119)

# button for open you grade window
yourgrade_button = Button(root, command=yourgrade_opt, borderwidth=0, justify=RIGHT)
photo_yourgrade = PhotoImage(file='sua_nota.gif')
yourgrade_button.config(image=photo_yourgrade, width='96', height='36')
yourgrade_button.place(x=330, y=120)


root.mainloop()

错误可能出在这里:

# Buttons: Execute a command
calculate_button = Button(root2, command=calculate, borderwidth=0)
calculate_button.config(image=calculate_button, width=100, height=20)
calculate_button.place(x=300, y=200)

这是问题所在:

calculate_button = Button(root2, command=calculate, borderwidth=0)
calculate_button.config(image=calculate_button)

calculate_button 是一个小部件,您正试图在需要图像的地方使用它。您必须给 image 选项一个图像对象,而不是按钮对象。