tkinter - display random image from array on button click - python-3.x

I would like a label to display a random image when a button is clicked.
This is my approach, but it does not work. Any ideas on how to solve welcome.
from tkinter import *
import random
window = Tk()
filechoices = ["image1.png", "image2.png", "image3.png"]
filename = PhotoImage(file = random.choice[filechoices])
def press():
image = Label(window, image=filename).pack()
button1 = Button(window, text="click to see image", command = press)
button1.pack()

random.choice is a function,not a list.
It should be:
filename = PhotoImage(file = random.choice(filechoices))
Read random module
random.choice(seq)
Return a random element from the non-empty sequence seq.
Also,in your code,you haven't used mainloop()

Related

How to remove a button in a tkinter

The problem is that if I want to remove these buttons, which have been allocated space by x and y, it cannot be and does not generate any error or anything else shows as if it did not do any comedy, and when the button is normal it works as it should
import tkinter as t
root = t.Tk()
def click():
button.pack_forget()
root.maxsize(375, 912)
img = t.PhotoImage(file="background.png")
background = t.Label(root, image=img)
background.pack()
button = t.Button(root, text ="Click\nme", command=click)
button.pack()
button.place(x=157, y=600)
root.mainloop()

Insert an image in a labelFrame title with tkinter

I wonder if it's possible to insert an image or icon in the title of a Labelframe in tkinter ?
Maybe something like this lf = Labelframe(root, image='my_img'), the same as we can do with a button.
It would be very useful.
You can insert any widget as labelwidget option of LabelFrame.
If you want an image instead of text displayed in LabelFrame:
import tkinter as tk
from PIL import Image, ImageTk
w = tk.Tk()
w.geometry("400x500")
img_open = Image.open("example_1.png")
img = ImageTk.PhotoImage(img_open)
label_title = tk.Label(w, image=img) #important Do Not grid/pack/place it!
label_frame = tk.LabelFrame(w, labelwidget=label_title)
label_frame.grid(row=0, column=0)
#fill LabelFrame with something to make it visible
bt = tk.Button(label_frame, text="Press")
bt.grid(padx=20, pady=20)
w.mainloop()
Output:
Or like I said before you can use any widget.
An example label with image and a label with text.
import tkinter as tk
from PIL import Image, ImageTk
w = tk.Tk()
w.geometry("400x500")
img_open = Image.open("example_1.png")
img = ImageTk.PhotoImage(img_open)
frame_labelwidget = tk.Frame(w) #important Do Not grid/pack/place it!
label_image = tk.Label(frame_labelwidget, image=img)
label_image.grid(row=0, column=0)
label_text = tk.Label(frame_labelwidget, text="Teamwork")
label_text.grid(row=0, column=1)
label_frame = tk.LabelFrame(w, labelwidget=frame_labelwidget)
label_frame.grid(row=0, column=0)
#fill LabelFrame with something to make it visible
bt = tk.Button(label_frame, text="Press")
bt.grid(padx=20, pady=20)
w.mainloop()
Output:
Of course that makes less sense because you can have text and an image combined in tk.Label by using compound option.
I added it only for demonstration purpose, you can add any widget you want. Instead of label_text you could add an Entry, Canvas...

how to add image in tkinter?

I can't add image (gif image) to tkinter window.
from tkinter import *
from tkinter import filedialog
from PIL import ImageTk, Image
root = Tk()
def open_image():
qr_select = filedialog.askopenfilename(title = "open")
im = PhotoImage(file=qr_select)
w1 = Label(window, image = im)
w1.image = im
w1.config(image=im)
w1.pack(side="right")
def window_function():
global window
window=Tk()
window.geometry("800x550+650+250")
window.title("QR_Scanner")
btn = Button(window,text = "open a gif picture",command = open_image)
btn.pack()
root.iconify()
window.mainloop()
btn = Button(root,text = "open window",command = window_function)
btn.pack()
root.mainloop()
my error is (_tkinter.TclError: image "pyimage1" doesn't exist)
The reason you can't see your gif in the window is that you haven't made a reference to the image so it is collected in Tkinters garbage collector. Read More About This Here. To Add a reference to the image you can do this:
w1.image = im
And add it in your code here:
def open_image():
qr_select = filedialog.askopenfilename(title = "open")
im = PhotoImage(file=qr_select)
w1 = Label(root, image = im)
w1.image = im #Keep A Reference To The Image
w1.config(image=im)
w1.pack(side="right")
The reason you are getting pyimage1 doesn't exist is because you have more than one instance the Tk and there is only meant to be 1. You have to make your window a Toplevel() by replacing: window=Tk() with window=TopLevel()

tkinter GUI- label image not showing but is still there (kinda)

I'm trying to show a die at random to a tkinter GUI, but it does not work as expected.
from tkinter import *
from random import choice
def change_pic():
die1 = PhotoImage(file=("dice-1.png"))
die2 = PhotoImage(file=("dice-2.png"))
die3 = PhotoImage(file=("dice-3.png"))
die4 = PhotoImage(file=("dice-4.png"))
die5 = PhotoImage(file=("dice-5.png"))
die6 = PhotoImage(file=("dice-6.png"))
faces=[die1, die2, die3, die4, die5, die6]
label.config(image=choice(faces))
label.grid(row=1, column=1)
root = Tk()
label = Label(root)
label.grid(row=1, column=1)
change_button = Button(root, text="change", command =change_pic)
change_button.grid(row=1, column=2)
root.mainloop()
this is my code
instead of showing the die image, it just show the place where it should be, and its size.
I tried a lot of things but I cannot fix it. please help.
You choose the image for the label inside a function which puts the image in the function namespace. When the function ends the reference to the image is garbage collected.
You can fix this by saving a reference to the image in the label widget:
faces=[die1, die2, die3, die4, die5, die6]
img = choice(faces)
label.config(image=img)
label.image = img # Save a reference to the image
label.grid(row=1, column=1)

tkinter messagebox link to notebook page

referwork = ttk.Notebook(root, style =".TNotebook")
f1 = ttk.Frame(referwork)
f2 = ttk.Frame(referwork)
referwork.add(f1, text="Search", padding = 1)
referwork.add(f2, text="Add/Delete", padding = 1)
#referwork.configure (height = 500, width = 800)
referwork.grid(row=0, column=0, sticky=(N,W,S,E))
I have used the above to create a two-tab notebook. On the first a search is performed. What I want to do is to have an alert in a message box appear messagebox.askyesno and when 'yes' is selected that the focus moves to the second page of the notebook
messagebox.askyesno(0.0,'"{0}"{1} \n {2}\n'.format(search_analyte.get(),' is not in the database.','Add,if appropriate'))
if True:
is as far as I have got. I cannot figure out how to 'open' the second page using this dialogue and conditional. Many thanks for any help
Use Notebook.select(tab) method, where tab is one of the notebook child widgets.
from tkinter import *
from tkinter.ttk import *
from tkinter.messagebox import askyesno
def open_first():
referwork.select(f1)
def open_second():
if askyesno('Title', 'Press "Yes" to open second page') == YES:
referwork.select(f2)
root = Tk()
referwork = Notebook(root, style =".TNotebook")
f1 = Frame(referwork)
f2 = Frame(referwork)
Button(f1, text='Go =>', command=open_second).pack(padx=100, pady=100)
Button(f2, text='<= Go', command=open_first).pack(padx=100, pady=100)
referwork.add(f1, text="Search", padding = 1)
referwork.add(f2, text="Add/Delete", padding = 1)
referwork.grid(row=0, column=0, sticky=(N,W,S,E))
root.mainloop()

Resources