简体   繁体   English

尝试将图像添加到 tkinter 应用程序,但它不会加载图像

[英]Trying to add an image to a tkinter app but it won't load the image

I am trying to add an image to an application without using PIL because I don't have access to it on my school pc.我正在尝试在不使用PIL情况下向应用程序添加图像,因为我无法在我的学校电脑上访问它。 So I'm trying to use PhotoImage but when I run the program it just gives me a white screen with nothing on it.因此,我正在尝试使用PhotoImage但是当我运行该程序时,它只会给我一个白色的屏幕,上面没有任何内容。 I've been trying to do this for the last 4 hrs and advice would be helpful.过去 4 小时我一直在尝试这样做,建议会有所帮助。

from tkinter import *
from tkinter import PhotoImage

master = Tk()

var1 = IntVar()
var2 = IntVar()
var3 = IntVar()
var3 = IntVar()
var4 = IntVar()
var5 = IntVar()
var6 = IntVar()
var7 = IntVar()
var8 = IntVar()
var9 = IntVar()
var10 = IntVar()
var11 = IntVar()
var12 = IntVar()
var13 = IntVar()
var14 = IntVar()
var15 = IntVar()
var16 = IntVar()
var17 = IntVar()
var18 = IntVar()


def var_state1():
    if var1.get() == TRUE and var2.get() == TRUE and var3.get() == TRUE:
        print("it workd1")

def Before_Day():

    # this is the image
    image = PhotoImage(file='C:/Users/plarkin2020334/Pictures/DQ_Logo.png')
    Label(master, image=image).grid(row=0, sticky=W)

    Label(master, text="Check When Complete:").grid(row=1, sticky=W)
    Checkbutton(master, text="Turn On Icecream Mashine", variable=var1).grid(row=2, sticky=W)
    Checkbutton(master, text="Turn On Radio", variable=var2).grid(row=3, sticky=W)
    Checkbutton(master, text="Turn On Oven", variable=var3).grid(row=4, sticky=W)
    crl1 = Button(master, text="Done", command=var_state1).grid(row=5, sticky=W, pady=4)


Before_Day()
mainloop()

Your code is okay except for a few things.除了几件事之外,您的代码还可以。 The 'image' you are creating gets lost when the program reaches mainloop(), which is because you created it inside the Before_Day() function.当程序到达 mainloop() 时,您正在创建的“图像”会丢失,这是因为您在 Before_Day() 函数中创建了它。

One easy change you can make is move this line at the top (ie outside the Before_Day() function) and then use it directly as global variable.您可以进行的一项简单更改是将此行移至顶部(即 Before_Day() 函数之外),然后将其直接用作全局变量。

image = PhotoImage(file='C:/Users/plarkin2020334/Pictures/DQ_Logo.png')

Just write this code after the 'var18 = IntVar()' line.只需在 'var18 = IntVar()' 行之后编写此代码。 And also remember not to use pack() with label when you use grid().还要记住,在使用 grid() 时不要将 pack() 与标签一起使用。

To be more precise, you can see the exact code below:更准确地说,您可以看到下面的确切代码:

from tkinter import *
from tkinter import PhotoImage

master = Tk()

var1 = IntVar()
var2 = IntVar()
var3 = IntVar()
var3 = IntVar()
var4 = IntVar()
var5 = IntVar()
var6 = IntVar()
var7 = IntVar()
var8 = IntVar()
var9 = IntVar()
var10 = IntVar()
var11 = IntVar()
var12 = IntVar()
var13 = IntVar()
var14 = IntVar()
var15 = IntVar()
var16 = IntVar()
var17 = IntVar()
var18 = IntVar()
image = PhotoImage(file='C:/Users/plarkin2020334/Pictures/DQ_Logo.png')

def var_state1():
    if var1.get() == TRUE and var2.get() == TRUE and var3.get() == TRUE:
        print("it workd1")

def Before_Day():
    # this is the image
    Label(master, image=image).grid(row=0, sticky=W)

    Label(master, text="Check When Complete:").grid(row=1, sticky=W)
    Checkbutton(master, text="Turn On Icecream Mashine", variable=var1).grid(row=2, sticky=W)
    Checkbutton(master, text="Turn On Radio", variable=var2).grid(row=3, sticky=W)
    Checkbutton(master, text="Turn On Oven", variable=var3).grid(row=4, sticky=W)
    crl1 = Button(master, text="Done", command=var_state1).grid(row=5, sticky=W, pady=4)


Before_Day()
mainloop()

This should work fine!这应该可以正常工作! I hope this helped.我希望这有帮助。 Best of luck!祝你好运! :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM