topper = Toplevel()
topper.title("2nd Window")
topper.state('zoomed')
my_img = ImageTk.PhotoImage(Image.open("diamond.png"))
my_label = Label(topper, image=my_img, height = 100 , width = 100)
F21 = Frame(topper, borderwidth=83, bg="blue", relief=SUNKEN)
button1 = Button(topper, text="class", command=topper.destroy)
button1.pack()
my_label.pack()
I am running the code and I get no errors, button is working as well but I'm not able to see the picture in the window.
Welcome to Stackoverflow Shakti!
For the future - it is always good to supply a Minimal, Reproducible Example so others can replicate and understand your issue better in order to help you! It also helps you to understand where exactly the error originates.
When you call Toplevel() from another tkinter window to open a new window, you will need to call "mainloop()" on the second window as well in order to display an image - try my code with an example image and comment/uncomment the line with
topper.mainloop()
to see the difference in functionality.
The adapted code:
from tkinter import *
from PIL import Image, ImageTk # pip install pillow
def show_second_window():
topper = Toplevel()
topper.title("2nd Window")
topper.state('zoomed')
my_img = ImageTk.PhotoImage(Image.open("t1.png"))
my_label = Label(topper, image=my_img, height=100, width=100)
F21 = Frame(topper, borderwidth=83, bg="blue", relief=SUNKEN)
button1 = Button(topper, text="class", command=topper.destroy)
button1.pack()
my_label.pack()
topper.mainloop() # <---- this is needed to show the image!
root = Tk()
root.title("1st window")
button = Button(root, text="show second window", command=show_second_window)
button.pack()
root.mainloop()
Related
I'm building an app that's supposed to display various images from a local folder depending on which button is clicked in the app.
So far, I have started out with an example I've found and try to modify it, but I can't figure out how to summon a .jpg or .png via a button click in the first place. Here's my very basic code so far:
import tkinter as tk
def write_slogan():
print("Tkinter is easy to use!")
root = tk.Tk()
frame = tk.Frame(root)
frame.pack()
button = tk.Button(frame,
text="QUIT",
fg="red",
command=quit)
button.pack(side=tk.LEFT)
slogan = tk.Button(frame,
text="Hello",
command=write_slogan)
slogan.pack(side=tk.LEFT)
root.mainloop()
Essentially, instead of writing the slogan in the console, I would like the button click to trigger an image being displayed. How would I go about achieving that?
I have amended your code so when you click your button the image "test.png" will display on a label.
Here is the code for testing, in my case "test.png" was in the same directory as my Python script.
import tkinter as tk
render = None
def write_slogan():
# get image and display
image = tk.PhotoImage(file = "test.png")
imageLabel.configure(image = image)
imageLabel.image = image
root = tk.Tk()
frame = tk.Frame(root)
frame.pack()
button = tk.Button(frame,
text="QUIT",
fg="red",
command=quit)
button.pack(side=tk.LEFT)
slogan = tk.Button(frame,
text="Hello",
command=write_slogan)
slogan.pack(side=tk.LEFT)
imageLabel = tk.Label(frame)
imageLabel.pack(side=tk.LEFT)
root.mainloop()
Also, this thread was helpful.
You can pass the image filename to the function, so that different buttons show different images:
import tkinter as tk
from PIL import ImageTk
def show_image(imagefile):
image = ImageTk.PhotoImage(file=imagefile)
imagebox.config(image=image)
imagebox.image = image # save a reference of the image to avoid garbage collection
root = tk.Tk()
frame = tk.Frame(root)
frame.pack()
button = tk.Button(frame, text="QUIT", fg="red", command=quit)
button.pack(side=tk.LEFT)
slogan = tk.Button(frame, text="Hello", command=lambda: show_image("slogan.png"))
slogan.pack(side=tk.LEFT)
other = tk.Button(frame, text="World", command=lambda: show_image("other.jpg"))
other.pack(side=tk.LEFT)
# label to show the image
imagebox = tk.Label(root)
imagebox.pack()
root.mainloop()
Since tk.PhotoImage() does not support JPG image, external Pillow module is used instead.
I am trying to add an image to a button, but I have got some issues when I try to execute the current code. All it shows is an image with no words. I can't even see the button either. Is there some way of fixing my current code?
from tkinter import *
import tkinter as tk
root = tk.Tk()
root.geometry("960x600")
canvas = Canvas(root, width=500, height=500)
canvas.pack()
imagetest = PhotoImage(file="giftest.gif")
canvas.create_image(250, 250, image=imagetest)
button_qwer = Button(root, text="asdfasdf", image=imagetest)
root.mainloop()
You need to pack (or grid) your button in the window, here is how you could do:
import tkinter as tk
from tkinter import PhotoImage
def print_hello():
print('hello')
root = tk.Tk()
root.geometry("960x600")
imagetest = PhotoImage(file="giftest.gif")
button_qwer = tk.Button(root, text="asdfasdf", image=imagetest, command=print_hello)
button_qwer.pack() # <-- don't forget to place the button in the window
root.mainloop()
You can have both text and image displayed on your button, using the compound option, like this:
button_qwer = tk.Button(root, image=imagetest, text="asdfasdf", compound="top", command=print_hello)
compound options are bottom, center, left, none, right, or top
You are making the button successfully but you are not drawing it onto the screen/interface. Use pack , place or grid.
button_qwer = Button(root, text="asdfasdf", image=imagetest)
button_qwer.pack()
Your full code can be like:
from tkinter import *
import tkinter as tk
root = tk.Tk()
root.geometry("960x600")
canvas = Canvas(root, width=500, height=500)
canvas.pack()
imagetest = PhotoImage(file="giftest.gif")
canvas.create_image(250, 250, image=imagetest)
button_qwer = Button(root, text="asdfasdf", image=imagetest)
button_qwer.pack()
root.mainloop()
I have tried all ways to set tkinter message widget border, but still receive any effect. I wonder if it does not have a border attribute? But I can set label and Text border using the same code. Here is my code snippet:
from tkinter import *
root = Tk()
frame = Frame(root)
message = Message(frame, text="hello world", width=200)
message.config(fg="red", borderwidth=100, highlightcolor="green")
frame.pack()
message.pack()
root.minsize(300, 200)
root.mainloop()
this's result:
OS Version:OS X 10.11.4
Python version: 3.52
You are setting the borderwidth correctly. The issue is that you can't see the changes you are implementing, until your add background colour and change the relief of the Frame and Message widget. Now try changing the borderwidths with this revised code, and I trust you get the "gotcha" moment.
from tkinter import *
root = Tk()
frame = Frame(root, background='yellow', borderwidth=20, relief=RAISED)
message = Message(frame, text="hello world", width=200)
message.config(fg="red", borderwidth=50, highlightcolor="green",
background='light blue', relief=SUNKEN)
frame.pack()
message.pack()
root.minsize(300, 200)
root.mainloop()
How can I get sound effects when I press buttons on the GUI for Tkinter?
Here is my code:
from Tkinter import *
root = Tk() #root object for the buttons
from PIL import Image, ImageTk #python imaging library
#open the images and store them in photos
image = Image.open("Jolteon.PNG")
image1 = Image.open("Eevee.PNG")
image2 = Image.open("Vaporeon.PNG")
image3 = Image.open("Flareon.PNG")
photo = ImageTk.PhotoImage(image)
photo1 = ImageTk.PhotoImage(image1)
photo2 = ImageTk.PhotoImage(image2)
photo3 = ImageTk.PhotoImage(image3)
topFrame = Frame(root)
topFrame.pack()
bottomFrame = Frame(root) #some different frames
bottomFrame.pack(side=BOTTOM)
button1 = Button(topFrame, text="Eevee", fg="brown")
button2 = Button(topFrame, text="Jolteon", fg="yellow")
button3 = Button(topFrame, text="Vaporeon", fg="blue")
button4 = Button(topFrame, text="Flareon", fg="red")
button5 = Button(topFrame,image=photo)
button6 = Button(topFrame,image=photo1)
button7 = Button(topFrame,image=photo2) #sdbsdfbdfsbdfb
button8 = Button(topFrame,image=photo3)
#packages the buttons so that it can be produced
button1.pack()
button6.pack()
button2.pack() #sdbsdbsdbsdfbfdsn
button5.pack()
button3.pack()
button7.pack()
button4.pack()
button8.pack()
root.mainloop()
It displays names and pictures of the Eevee trios from Pokemon.
What I want is when I press the picture of the pokemon to make the pokemon cry.
How would I go on about implementing this?
You have to link the click of your mouse on the image, with an even handler (or simply method):
btn = tkinter.Button(root, text='Play Sound', width=16, bg='#2ff')
btn.bind('<Button-1>', on_click)
# binding the click of btn with the on_click function
btn.pack()
# note the parameter 'event', since this is a even handler
def on_click(event):
# play music
You could use the pygame module, and specifically you could use its mixer module for playing music. You could easily install the pygame module for your version of Python and for Windows from the official website.
Once the installation is finished, you have first to initialise the Pygame modules with this command pygame.init(). Remember to uninitialize the same modules once you stop using them with the command pygame.quit(). Note that you can initialise single modules, like the mixer module, which is the exact one that you should use.
I am trying to display an image in python using the tkinter canvas option. However, if I input it in a class, like below, it doesn't give an error but also doesn't show my image. The buttons are displayed correctly though. Also, if I take the code for generating this image out of the class it works correctly. I can't seem to find out what the problem is.
import Tkinter as tk
from Tkinter import *
class Board(tk.Frame):
def __init__(self,parent):
frame = Frame(parent)
frame.pack()
tk.Frame.__init__(self,parent)
frame2 = Frame(frame)
frame2.pack()
c=Canvas(frame2)
c.pack(expand=YES,fill=BOTH)
background=PhotoImage(file='Board.gif')
c.create_image(100,100,image=background,anchor='nw')
button = Button(frame, text="Next turn", command=self.next_turn)
button.pack()
button = Button(frame, text="Roll the dice", command=self.roll)
button.pack()
....
root = Tk()
board = Board(root)
board.pack()
root.mainloop()
You have to keep a reference to the PhotoImage. This is just and example (you can also use self.background instead of c.background):
c = Canvas(frame2)
c.pack(expand=YES,fill=BOTH)
c.background = PhotoImage(file='Board.gif')
c.create_image(100,100,image=c.background,anchor='nw')