0

How to click a button to open new window with image in the new window? I already check other post but nothing solve my problem, so, I'm making this question for the first time. So this is my code

    from tkinter import *
from tkinter.font import BOLD
from turtle import onclick, onscreenclick
from PIL import ImageTk,Image
mainwindow=Tk()

mainframe=Frame(mainwindow,bg="white")
mainframe.pack(fill="both",expand=True)

#Label
label=Label(mainframe,text="Lucky Draw",bg="blue",fg="white",padx=5,pady=5)
label.config(font=("Times New roman",20,BOLD))
label.pack(fill="x")
horizontal_frame=Frame(mainframe,bg="white")

#lottery
def ubt1():
    b1["text"]="Blender"
    b1["bg"]="red"
    newwin=Tk()
    newwin.geometry("800x800")
    c=Canvas(newwin,bg="blender",height=800,width=800)
    bgimage=PhotoImage(file="D://LuckyDraw//blender.png")
    background_label=Label(newwin,image=bgimage)
    background_label.place(x=0,y=0,relwidth=1,relheight=1)
    c.pack()
    newwin.mainloop()    
b1=Button(horizontal_frame,text="1",bg="green",fg="white",padx=10,pady=10,height=1,width=7,command=ubt1,font=("Poppins",10,BOLD))
b1.grid(row=0,column=0,padx=6,pady=10,sticky="nsew")

def ubt():
    b2["text"]="Stove"
    b2["bg"]="red"
b2=Button(horizontal_frame,text="2",bg="green",fg="white",padx=10,pady=10,height=1,width=7,command=ubt,font=("Poppins",10,BOLD))
b2.grid(row=0,column=1,padx=6,pady=10,sticky="nsew")

horizontal_frame.pack(fill="x")

mainwindow.mainloop()

I'm testing a lucky draw program.

So when start running the program user can choose 1 or 2.

1 is a big prize and 2 is a small price.

So if the user choose 2,it will just show what prize the user win but just by changing the color and text of what the user win.

But if the user choose 1,I want to open a new window with the blender picture in the new window to let the user know that he wins the big prize.

The problem here is when clicking the 1 button the new window pop up but without the image.

I try running with separate file and that show the image but when I put that code in the upper file the image doesn't show up anymore.

from tkinter import *
from tkinter import messagebox

root=Tk()
root.geometry("800x800")
c=Canvas(root,bg="white",height=200,width=200)
filename=PhotoImage(file="D://LuckyDraw//blender.png")
background_label=Label(root,image=filename)
background_label.place(x=0,y=0,relwidth=1,relheight=1)
c.pack()
root.mainloop()

I found a way to disply the image by destroying the buttons but even after the 1st user choose 1 and win the big prize by seeing the image in the new window, I also want to let the 2nd user choose 2 to know what he wins.

So how can I display the image if the user press 1 and see the prize.

After that we want to come back to the main program without losing the first progress of choosing the 1 button.

I'm really really sorry if my question is complicated because this is my first time asking question on stakoverflow and my english is not very good.

1 Answers1

0

This happens due to the reason that Tkinter was only made to handle one window at once, but you passed two. The accurate way to do this would be to use TopLevel() which is a command to create a new window in Tkinter.
Code Looks like this(Change only in the #lottery part)-

from tkinter import *           
from tkinter.font import BOLD
from turtle import onclick, onscreenclick
from PIL import ImageTk,Image
mainwindow=Tk()

mainframe=Frame(mainwindow,bg="white")
mainframe.pack(fill="both",expand=True)

#Label
label=Label(mainframe,text="Lucky Draw",bg="blue",fg="white",padx=5,pady=5)
label.config(font=("Times New roman",20,BOLD))
label.pack(fill="x")
horizontal_frame=Frame(mainframe,bg="white")

#lottery
# Changes made only in this part
global second
def ubt1():
    # Creating a second Level
    second = Toplevel()
    second.title("Child Window") # Rename this
    second.geometry("400x400")
    # Changing Label text
    b1["text"]="Blender"
    b1["bg"]="red"
    # Creation of Canvas
    c=Canvas(second,height=800,width=800)
    c.pack()

    # Creation of image object
    img=PhotoImage(file='D:\LuckyDraw\blender.png')
    labelz = Label(c, image=img) # Creating a label with the image on
                                 # Canvas c
    labelz.image = img # Required for reference
    labelz.pack() # Packing the label
    
b1=Button(horizontal_frame,text="1",bg="green",fg="white",padx=10,pady=10,height=1,width=7,command=ubt1,font=("Poppins",10,BOLD))
b1.grid(row=0,column=0,padx=6,pady=10,sticky="nsew")

def ubt():
    b2["text"]="Stove"
    b2["bg"]="red"
b2=Button(horizontal_frame,text="2",bg="green",fg="white",padx=10,pady=10,height=1,width=7,command=ubt,font=("Poppins",10,BOLD))
b2.grid(row=0,column=1,padx=6,pady=10,sticky="nsew")

horizontal_frame.pack(fill="x")

mainwindow.mainloop()
Anshumaan Mishra
  • 1,349
  • 1
  • 4
  • 19