I created with Tkinter a Menubutton with an image. And I added in the Menubutton a label and two buttons.When I run my application, it works fine until I click two times the Menubutton. It starts to lag and it crashes.
Here is my code :
from Tkinter import *
from PIL import ImageTk
def donothing():
print("NOTHING")
root = Tk()
root.geometry("300x300")
Menu_Photo = Menubutton(root)
Photo = ImageTk.PhotoImage(file="image.png")
Menu_Photo.config(image=Photo)
Menu_Photo.image = Photo
Menu_Photo.menu = Menu(Menu_Photo, tearoff=0, relief=FLAT)
Menu_Photo["menu"] = Menu_Photo.menu
Menu_Photo.menu.label = Label(Menu_Photo.menu, text="Menu")
Menu_Photo.menu.Settings = Button(Menu_Photo.menu, text="Settings", relief=FLAT, command=donothing)
Menu_Photo.menu.Sign_Out = Button(Menu_Photo.menu, text="Sign Out", relief=FLAT, command=donothing)
Menu_Photo.menu.label.pack()
Menu_Photo.menu.Settings.pack(fill=BOTH)
Menu_Photo.menu.Sign_Out.pack(fill=BOTH)
Menu_Photo.pack(side=RIGHT, anchor=N)
root.mainloop()
I really don't understand what is the problem ?
This is what happens for me :
The menu opens and closes every second.
Related
I want to open new window with button click and close the old one. I tried to do that with this code, but when I click button it's also open another mini window with title 'tk'. How can I prevent that?
from tkinter import *
from PIL import Image, ImageTk
def openOperationsGui():
window.destroy()
operationsWindow = Toplevel()
operationsWindow.title("Operations")
operationsWindow.geometry("1366x768")
window = Tk()
logo = Image.open("resources\logo.png")
logoImg = ImageTk.PhotoImage(logo)
logoLbl = Label(image=logoImg)
logoLbl.place(x=450, y=120, height=500, width=500)
conn = Button(window, text="Connect", fg='blue', command=openOperationsGui)
conn.place(x=450, y=533, height=87, width=490)
passw = Entry(window, text="", bd=5)
passw.place(x=620, y=480, height=50, width=150)
window.title('Title')
window.geometry("1366x768")
window.mainloop()
I have been struggling with merging my code.
I am creating an arcade game on Python and have a main file where I have an image and clickable assets which link to a game I have imported.
Now I am working on creating constant features in the game, including a menu bar which displays reminders, can change the volume and brightness setting etcetura.
However, I don't know how to make this on my main project file.
Could you help me out?
I am using Python Pygame, Tkinter and Turtle.
It sounds like you need to think some more about what exactly you want. Your question is rather vague and tkinter has several options that can be presented in various ways to a user for making selections. Below is a quick example of some ideas you may want to explore.tkinter widgets
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.geometry("400x400")
root.resizable(False, False)
root.title("Sample")
menu = tk.Menu(root)
root.config(menu=menu)
file_menu = tk.Menu(menu, tearoff=0)
menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="Quit", command=quit)
file_menu.add_command(label="Something", command=quit)
file_menu.add_command(label="New Option", command=quit)
label = tk.Label(root, text="Choose an option below", font="times 14")#label
label.pack()
items = ["Hi", "Something", "Bye"]
combo_box = ttk.Combobox(root, font=20, state="normal")#combo box
combo_box['values'] = items
combo_box.pack()
lblabel = tk.Label(root, text="Choose an item from the list", font="times 14")#label
lblabel.pack()
items_var = tk.StringVar(value=items)
list_box = tk.Listbox(root, listvariable=items_var, selectmode=tk.BROWSE)#list box
list_box.pack()
rblabel = tk.Label(root, text="Choose an option below", font="times 14")#label
rblabel.pack()
choices = tk.StringVar(value=items)
radio_button = ttk.Radiobutton(root, text="some_option")#radio button
radio_button.pack()
root.mainloop()
How do I use user input to retrieve an image, and display it on the screen? I have the user input field in Tkinter, and I do not know how to get that to go to my folder, and get the image to show up on the same screen.
EDIT: I was able to get the path to show on the screen, but now I don't know how to get the actual image to show up from that path inside the frame... I do not know how to link this path to have the program display the image, i.e. myimage1 from my folder.
from tkinter import *
from PIL import ImageTk,Image
from tkinter import ttk
from tkinter import filedialog
root = Tk()
root.title("Images")
root.iconbitmap(r'C:\Users\hadhikari\example\Takumi_Logo.ico')
button_quit = Button(root, text= "Exit Program", command=root.quit)
button_quit.pack()
e= Entry(root, width=50, bg="light blue", borderwidth=3)
e.pack()
e.insert(0," ")
def myClick():
link = "r'C:\\Users\\hadhikari\\example\\" + e.get()
myLabel1 = Label(root, text=link)
myLabel1.pack()
myButton = Button(root, text="Scan Part Number", command=myClick,
bg="pink", fg="white")
myButton.pack()
#my_img = ImageTk.PhotoImage(Image.open('link'))
#my_Label = Label(image=my_img)
#my_Label.pack()
#def getText():
# inputtedtext = entrybox.get()
#entrybox = Entry(root)
#entrybox.pack()
#btn = Button(root, text="Submit", command=getText)
#btn.pack()
frame = LabelFrame(root, padx=100, pady=100)
frame.pack(padx=50, pady=50)
root.mainloop()
As suggested in the comments, maybe using Tkinter's askopenfilename dialog might be a better choice than hardcoding the filepath, and letting the user input the filename.
But, first of all, let's fix the code following your intentions. Most of the part on how to properly set up a Tkinter Label, and dynamically place images in that, can be easily found, for example in this Q&A. Basically, you want to put the "update routine" inside your myClick method:
from tkinter import *
from PIL import Image, ImageTk
# How to properly set up Tkinter label with dynamically changeable
# ImageTk.PhotoImage: https://stackoverflow.com/a/3482156/11089932
root = Tk()
button_quit = Button(root, text='Exit Program', command=root.quit)
button_quit.pack()
e = Entry(root, width=50, bg='light blue', borderwidth=3)
e.pack()
e.insert(0, '')
my_label = Label()
my_label.pack()
def myClick():
link = r'your/path/goes/here' + e.get()
my_img = ImageTk.PhotoImage(Image.open(link))
my_label.configure(image=my_img)
my_label.image = my_img
myButton = Button(root, text='Scan Part Number', command=myClick,
bg='pink', fg='white')
myButton.pack()
root.mainloop()
Program at startup:
Opening the first image:
Opening another image:
Now, regarding the askopenfilename dialog, we get rid of the Entry widget, and simply open the dialog inside the myClick method:
from tkinter import *
from tkinter.filedialog import askopenfilename
from PIL import Image, ImageTk
# How to properly set up Tkinter label with dynamically changeable
# ImageTk.PhotoImage: https://stackoverflow.com/a/3482156/11089932
root = Tk()
button_quit = Button(root, text='Exit Program', command=root.quit)
button_quit.pack()
my_label = Label()
my_label.pack()
def myClick():
link = askopenfilename()
my_img = ImageTk.PhotoImage(Image.open(link))
my_label.configure(image=my_img)
my_label.image = my_img
myButton = Button(root, text='Scan Part Number', command=myClick,
bg='pink', fg='white')
myButton.pack()
root.mainloop()
Program at startup:
Opening the first image (dialog):
Opening the first image (display):
----------------------------------------
System information
----------------------------------------
Platform: Windows-10-10.0.19041-SP0
Python: 3.9.1
PyCharm: 2021.1.3
Pillow: 8.3.1
----------------------------------------
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.
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.