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. So I'm trying to use PhotoImage
but when I run the program it just gives me a white screen with nothing on it. I've been trying to do this for the last 4 hrs and advice would be helpful.
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.
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.
image = PhotoImage(file='C:/Users/plarkin2020334/Pictures/DQ_Logo.png')
Just write this code after the 'var18 = IntVar()' line. And also remember not to use pack() with label when you use grid().
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! :-)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.