为什么我的图像按钮没有出现? [英] Why my image buttons are not appearing?

查看:42
本文介绍了为什么我的图像按钮没有出现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的图像背景上的某个位置放置两个图像按钮,但我的按钮没有出现.我认为他们的图像在背景后面.

I am trying to place two image buttons on my image background in a certain position, but my buttons are not appearing. I think their images are behind the background.

我尝试使用 placepack,两者都不起作用.可能是什么问题?

I tried to use place and pack, both did not work. What could be the problem?

from tkinter import*
import tkinter as tk
import settings

class Application(Frame):
    def __init__ (self, master):
        Frame.__init__(self,master)
        self.grid()
        self.create_widgets()

    def create_widgets(self):
        button1 = PhotoImage(file ="button1.gif")
        button2 = PhotoImage(file ="button2.gif")
        settings_button = Button(self, image = button1, 
                                 command = self.mult_command, width = 15)
        settings_button.place(x=1, y=1)
        rules_button = Button(self, image = button2, 
                              command = self.the_rules, width = 15)
        rules_button.place(x=50, y=50)

def main_code():
    window = Tk()
    window.title("The Bouncer")
    bg_image = PhotoImage(file ="pic.gif")
    x = Label (image = bg_image)
    x.image = bg_image
    x.place(x = 0, y = 0, relwidth=1, relheight=1)
    window.geometry("600x300")
    app = Application(window)
    window.mainloop()

main_code()

谢谢

推荐答案

很可能您的图像在显示之前就被垃圾回收了.这是一个常见的 Tkinter 问题.尝试更改行:

It is likely that your image is being garbage collected before it is displayed. This is a common Tkinter gotcha. Try changing the lines:

button1 = PhotoImage(file ="button1.gif")
button2 = PhotoImage(file ="button2.gif")

self.button1 = PhotoImage(file ="button1.gif")
self.button2 = PhotoImage(file ="button2.gif")

并使用

settings_button = Button(self, image = self.button1, command = self.mult_command, width = 15)

这应该保留对您的图像的引用,防止它被垃圾收集.

This should keep a reference to your image, stopping it from getting garbage collected.

这篇关于为什么我的图像按钮没有出现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆