I've written a bunch of code that produces a GUI. Now whenever I run the code it produces the main GUI window and one additional small window with nothing in it. When I close the smaller window the large main window disappears.
Now I've been reading other posts with similar problems, but I haven't been able to identify where the error is in my code.
Please help :)
Follow up question: How would I add a background image rather than the grey boring color?
Here is what it looks like.
#%% GUI Interface
import Tkinter as tk
from tkFont import Font
from PIL import ImageTk, Image
from Tkinter import END
#This creates the main window of an application
window = tk.Toplevel()
window.title("Sat Track")
window.geometry("1200x800")
window.configure(background='#f0f0f0')
#Imports the pictures.
pic1 = "Globeview.png"
pic2 = "MercatorView.png"
pic3 = "currentweathercroppedsmall.png"
pic4 = "GECurrentcroppedsmall.png"
#Creates a Tkinter-compatible photo image, which can be used everywhere Tkinter expects an image object.
img1 = ImageTk.PhotoImage(Image.open(pic1))
img2 = ImageTk.PhotoImage(Image.open(pic2))
img3 = ImageTk.PhotoImage(Image.open(pic3))
img4 = ImageTk.PhotoImage(Image.open(pic4))
header = tk.Label(window, text="Satellite Control Center", font=Font(size=40))
header.pack()
toprow = tk.Frame(window)
infobox = tk.Text(toprow, width=50, height=7, font=("Calibri",12))
infobox.pack(side = "left")
infobox.insert(END,"Current information for:"+spacer+name +'\n'+
"Time:" +space+times+ '\n'+
"Longitude:"+space +x_long+ '\n'+
"Latitude:" +space+x_lat+ '\n'+
"Altitude:" +space+alt+space+ "[km]"+'\n'+
"Velocity:" +space+vel+space+ "[km/s]" + '\n'+
"Spatial Resolution: "+space +spat+space+ "[Pixels pr. m]"
)
toprow.pack()
midrow = tk.Frame(window)
globeview = tk.Label(midrow, image = img1)
globeview.pack(side = "left") # the side argument sets this to pack in a row rather than a column
mercatorview = tk.Label(midrow, image = img2)
mercatorview.pack(side = "left")
midrow.pack() # pack the toprow frame into the window
bottomrow = tk.Frame(window)
currentweather= tk.Label(bottomrow, image = img3)
currentweather.pack(side = "left")
gearth = tk.Label(bottomrow, image = img4)
gearth.pack(side = "left")
bottomrow.pack()
#Start the GUI
window.mainloop()
Every tkinter application needs exactly one instance of Tk class. In your code you don't create one but mainloop seem to create one automatically it's still created (See Bryan's comment below), even though you can't(easily) refer to it later.
If you will use additional Toplevel widgets to that of your curent one go:
root = tk.Tk()
root.withdraw() # You can go root.iconify(), root.deiconify() later if you
# want to make this window visible again at some point.
# MAIN CODE HERE
root.mainloop()
if not simply replace:
window = tk.Toplevel()
with:
window = tk.Tk()
Note: Also note that if you're working using IDLE keep in mind that it creates its own Tk object which may hide the fact that your application will need one when used standalone.
Remove Toplevel from window = tk.Toplevel(). I don't have a python2 dist available -- I'm on python3 but when I removed TopLevel from my code, it only brought up one window. So, the python3 way is....
import tkinter as tk
#This creates the main window of an application
window = tk.Tk()
#Start the GUI
window.mainloop()
I think the only difference would be that python2's tkinter is actually Tkinter (as you have already done).
Related
I want to put an image inside my dice function I would really appreciate if someone can tell me what I need to add so I can have the image as the background of the page
def dice():
tk = Tk()
tk.geometry('300x300')
img = PhotoImage(file='dicee.gif')
lb5 = Label(tk,image=img)
lb5.pack()
btn4=Button(tk,text="Roll The Dice",command=dice)
btn4.place(x=110,y=130)
tk.mainloop()
The error it shows me is:
self.tk.call(
_tkinter.TclError: image "pyimage1" doesn't exist
Actually there are two separate problems with your code. One is that you're creating multiple instances of Tk() which is problematic as #Bryan Oakley mentioned in a comment — create a Toplevel window widget instead.
The other issue is that you're creating the PhotoImage in a function, and since it's a local variable it will be garbage collected when the function returns (see Why does Tkinter image not show up if created in a function?)
Here's code showing how to fix both issues:
from tkinter import *
def dice():
tk = Toplevel() # Create new window.
tk.geometry('300x300')
img = PhotoImage(file='dicee.gif')
lb5 = Label(tk, image=img)
lb5.img = img # Save reference to image.
lb5.pack()
tk = Tk()
btn4 = Button(tk, text="Roll The Dice", command=dice)
btn4.place(x=110, y=130)
tk.mainloop()
I'm trying to create a python program tkinter that, upon the pressing of a button, opens a new full screen tkinter window containing an image and plays an audio file - here's my code:
from tkinter import *
from PIL import Image, ImageTk
from playsound import playsound
def play():
window = Toplevel()
window.attributes('-fullscreen', True)
img = ImageTk.PhotoImage(Image.open("pic.png"))
label = Label(window, image=img).pack()
playsound("song.mp3")
buttonWindow = Tk()
b = Button(buttonWindow, text="Press Button", command=play)
b.pack()
(my image and audio file are both on the desktop with my python file)
However, when I run my code, when I press the button, the audio plays but no second tkinter window opens.
I've tried to destroy() the buttonWindow and have tried many different ways of including an image on a tkinter window - if I remove the line of code using PhotoImage(), the window appears (obviously I then get a syntax error stating that 'img' is not defined).
How could I solve this?
Thanks,
Louis
I had similar problem.
But i resolve this trial and error method.
First of all i don't use pillow library and put image into Label
You should try define Photoimage object NOT IN FUNCTION
Example:
It will work:
import tkinter as tk
root = tk.Tk()
root.geometry("600x400")
def popUp():
popff = tk.Toplevel(root)
popff.geometry("400x200")
labelImgff = tk.Label(popff, image=imgff, bg="white")
labelImgff.grid(row=0, column=0)
imgff = tk.PhotoImage(file="/home/user/image.png")
btnff = tk.Button(text="startPOP", command=popUp)
btnff.grid(column=0,row=0)
root.mainloop()
effect of action
Your playsound() command is blocking execution. The playsound() command has an optional field 'block', which is True by default. Changing this to False will continue execution and allow mainloop() to continue.
Second, just call label.draw() to draw your image to the TopLevel window.
Here's the code:
from tkinter import *
from PIL import Image, ImageTk
from playsound import playsound
def play():
window = Toplevel()
window.attributes('-fullscreen', True)
img = ImageTk.PhotoImage(Image.open("pic.jpeg"))
label = Label(window, image=img).pack()
playsound("song.mp3",block=False)
label.draw()
buttonWindow = Tk()
b = Button(buttonWindow, text="Press Button", command=play)
b.pack()
buttonWindow.mainloop()
Cheers!
I started a project a few days ago but unfortunately I'm stuck. I would like to make an image editor (a very simple one ;D) where a choose an image by using the filedialog then I would have the possibilty to make few modifications like rotations. My problem is that I can choose the image but once I did, I can't show the image on the canvas.
It says : "name 'image' is not define"
I think my problem is that the program want to show the image on the canvas but I haven't selected it yet.
from tkinter import *
from PIL import Image, ImageTk
from tkinter import filedialog
root = Tk()
#function to select my image by using the filedialog
def select_image():
file_path = filedialog.askopenfilename()
image = Image.open(file_path)
#button to press to open filedialog
select = Button(root, text="select an image", command=select_image)
select.pack()
#the canvas where the image will be display
canvas = Canvas(root, width= 100, height=100, bg="grey")
canvas.pack()
image_tk = ImageTk.PhotoImage(image)
canvas.create_image(0,0, image= image_tk)
root.mainloop()
It is possible to create an image object before you have an image file to display, but not in the way you're doing it. You simply need to create an empty image object and keep track of the image object id, and then reconfigure that object inside of select_image.
For example, don't define image_tk in the main program. Change the line that creates the image item on the canvas to this:
image_id = canvas.create_image(0,0, anchor="nw")
(note: without the anchor option, the center of the image will be at 0,0. I'm guessing you want the upper-left corner of the image to be at 0,0).
Next, in select_image is where you do all of the work of getting the image, saving a reference to it (to avoid it being deleted when the function returns), and showing it in the canvas. It would look something like this:
def select_image():
# ask the user for the filename
file_path = filedialog.askopenfilename()
# only show the image if they chose something
if file_path:
# open the file
image = Image.open(file_path)
# create the image object, and save it so that it
# won't get deleted by the garbage collector
canvas.image_tk = ImageTk.PhotoImage(image)
# configure the canvas item to use this image
canvas.itemconfigure(image_id, image=canvas.image_tk)
You’ve got a couple of problems here:
You are never calling your function! Your code ignores
select_image() after it is defined
The variable image is only defined inside your (uncalled) function, so when you try to use it via ImageTk.PhotoImage(), it is
undefined.
Try returning the image object this way:
from tkinter import *
from PIL import Image, ImageTk
from tkinter import filedialog
root = Tk()
#function to select my image by using the filedialog
def select_image():
file_path = filedialog.askopenfilename()
return Image.open(file_path)
#button to press to open filedialog
select = Button(root, text="select an image", command=select_image)
select.pack()
#the canvas where the image will be display
canvas = Canvas(root, width= 100, height=100, bg="grey")
canvas.pack()
image_tk = ImageTk.PhotoImage(select_image())
canvas.create_image(0,0, image= image_tk)
root.mainloop()
Note that you are not checking for errors or cancellation in your select_image() function. You’d be better off handling cancel or error inside the function too
I have loaded an image to tkinter label and that image is diplayed in that label.When i press the button i need to change that image.When the button is pressed older image is gone but the new image is not displayed
My code is
import Tkinter as tk
from PIL import Image, ImageTk
root = tk.Tk()
def change_pic(labelname):
photo1 = ImageTk.PhotoImage(Image.open("demo.jpg"))
labelname.configure(image=photo1)
print "updated"
vlabel=tk.Label(root)
photo = ImageTk.PhotoImage(Image.open('cardframe.jpg'))
vlabel.configure(image=photo)
vlabel.pack()
b2=tk.Button(root,text="Capture",command=lambda:change_pic(vlabel))
b2.pack()
root.mainloop()
In def change_pic(labelname), you need to add labelname.photo = photo1 to make sure photo1 not being garbage collected.
def change_pic(labelname):
photo1 = ImageTk.PhotoImage(Image.open("demo.jpg"))
labelname.configure(image=photo1)
labelname.photo = photo1
print "updated"
P.S. Looks like both labelname.photo = photo1 and labelname.image = photo1 work.
Check this out for more details: http://effbot.org/tkinterbook/label.htm
You can use the label to display PhotoImage and BitmapImage objects.
When doing this, make sure you keep a reference to the image object,
to prevent it from being garbage collected by Python’s memory
allocator. You can use a global variable or an instance attribute, or
easier, just add an attribute to the widget instance.
The following edits were made:
I have organised your code layout and simplified its
syntax where possible. These are to make your code easier to read.
Commonly we make the PIL objects a subset/children of tk.
So long it is a part of root (i.e. it is a child of the root
or any of its child widgets), your PIL objects will work.
Your working code is shown below:
import Tkinter as tk
from PIL import Image, ImageTk
def change_pic():
vlabel.configure(image=root.photo1)
print "updated"
root = tk.Tk()
photo = 'cardframe.jpg'
photo1 = "demo.jpg"
root.photo = ImageTk.PhotoImage(Image.open(photo))
root.photo1 = ImageTk.PhotoImage(Image.open(photo1))
vlabel=tk.Label(root,image=root.photo)
vlabel.pack()
b2=tk.Button(root,text="Capture",command=change_pic)
b2.pack()
root.mainloop()
I got it working with one more line of code:
import tkinter as tk # I am using python 3 on windows so the tkinter is lowercased
from PIL import Image, ImageTk
root = tk.Tk()
def change_pic(labelname):
global photo1 # This is the only new line you need, I believe
photo1 = ImageTk.PhotoImage(Image.open("demo.jpg"))
labelname.configure(image=photo1)
print("updated") # Again I'm using python 3 on windows so syntax may differ.
root.update() # You don't need this statement in this case, but it never hurts
vlabel=tk.Label(root)
photo = ImageTk.PhotoImage(Image.open('cardframe.jpg'))
vlabel.configure(image=photo)
vlabel.pack()
b2=tk.Button(root,text="Capture",command=lambda:change_pic(vlabel))
b2.pack()
root.mainloop()
I believe that the code changes the image locally, so the global statement will change it on the project scope.
I have added a image file to my code in tkinter but it basically fills my the whole frame so if its possible can you recommend a tutorial that shows or explains how to do this.... unless you can show me on here.
I havent added my full code but the code below should display a test image once you have it saved in the python directory.
I would like to create 'next' button which would open up a new frame with another image on it.
from Tkinter import *
root = Tk()
ButtonImage = PhotoImage(file='test.gif')
testButton = Button(root, image=ButtonImage)
testButton.pack()
root.mainloop()
You could try something like this:
from Tkinter import *
from glob import glob
class ImageFrame(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.images = glob("*.gif")
self.cur = 0
# label showing the image
self.image = PhotoImage()
imagelabel = Label(self, image=self.image)
imagelabel.grid(row=1, column=1)
# button cycling through the images
button = Button(self, text="NEXT", command=self.show_next)
button.grid(row=2, column=1)
# layout and show first image
self.grid()
self.show_next()
def show_next(self):
self.cur = (self.cur + 1) % len(self.images)
self.image.configure(file=self.images[self.cur])
ImageFrame().mainloop()
Some explanations:
glob is used to get a list of all files matching some pattern in the current directory
grid is a simple but quite flexible layout manager for Tkinter (see Tkinter reference)
the show_next method, which is bound to the button, cycles through the images and binds a new image to the PhotoImage using configure
The result is a simple frame showing a large image and a button, cycling though the gif images in the current directory.
There are numerous modules that would help you out with this. You can use the PIL module. Typically what I would do in a situation like yours is use the PIL module to load and paste the image onto the frame. This is how you do this.
from Tkinter import *
from PIL import Image, ImageTk
root = Tk()
Image = Image.open(path).resize((300, 300)), Image.ANTIALIAS
ButtonImage = ImageTk.PhotoImage(Image)
# If you are using image by itself. Without it being a button.
#Image_Label = Label(image = self.HomeImage, borderwidth=0, highlightthickness=0)
# Otherwise
testButton = Button(root, image=ButtonImage)
testButton.pack()
root.mainloop()
I believe that this would definitely help you with resizing the image and loading an image to the screen as a button. We used PIL to load the image onto the frame and resized the image. This is What you were also asking for earlier. I used the resize method on the Image.open() Function. This resizes the image to what you want. The standards are the actual sizes of that image.