I just started using python tkinter and I have a button that opens a new window. One the new window there is an image, but the image won't show up.Can you please help me solve my problem?
from tkinter import *
def nwindow():
nwin = Toplevel()
nwin.title("New Window")
btn.config(state = 'disable')
photo2 = PhotoImage(file = 'funny.gif')
lbl2 = Label(nwin, image = photo2)
lbl2.pack()
def quit():
nwin.destroy()
btn.config(state = 'normal')
qbtn = Button(nwin, text = 'Quit', command = quit)
qbtn.pack()
main = Tk()
main.title("Main Window")
main.geometry("750x750")
photo = PhotoImage(file = 'funny.gif')
lbl = Label(main, image = photo)
lbl.pack()
btn = Button(main, text = "New Winodw", command = nwindow)
btn.pack()
main.mainloop()
your coding doesn't work but putting .mainloop() should fix your issue
def nwindow():
nwin = Toplevel()
nwin.title("New Window")
btn.config(state = 'disable')
photo2 = PhotoImage(file = 'funny.gif')
lbl2 = Label(nwin, image = photo2)
lbl2.pack()
nwin.mainloop()
Related
I make a program that choose a picture in pythons window and enter a key, then the size of picture will be double in size. But when I enter the key, the program is going to stop. And window of python is closed.
How can I make it work?
I think part of keyEvent makes errors.
from tkinter import *
from tkinter.filedialog import*
import keyboard
def func_open():
global filename
filename = askopenfilename(parent = window, filetypes = (("GIF 파일", "*gif"), ("모든 파일", "*.*")))
photo = PhotoImage(file = filename)
pLabel.configure(image = photo)
pLabel.image = photo
def func_exit():
window.quit()
window.destroy()
def keyEvent(event):
global photo
if keyboard.read_key() == "up":
photo = PhotoImage(file = filename)
photo = photo.zoom(2, 2)
pLabel.configure(image = photo)
pLabel.image = photo
window = Tk()
window.geometry("500x500")
window.title("명화 감상하기")
window.bind("<Key>", keyEvent)
photo = PhotoImage()
pLabel = Label(window, image = photo)
pLabel.pack(expand = 1, anchor = CENTER)
mainMenu = Menu(window)
window.config(menu = mainMenu)
fileMenu = Menu(mainMenu)
mainMenu.add_cascade(label = "파일", menu = fileMenu)
fileMenu.add_command(label = "파일 열기", command = func_open)
fileMenu.add_separator()
fileMenu.add_command(label = "프로그램 종료", command = func_exit)
window.bind("<Key>", keyEvent)
window.mainloop()
Output
nothing work
Expected
the picture will grow up twice
Just a simple example of a problem I experienced:
from tkinter import *
root = Tk()
frame = Frame(root)
label = Label(frame, text = "Hey")
label.pack()
def packframe():
frame.pack()
def destroyframe():
frame.destroy()
pack_button = Button(root, text = "pack", command = packframe)
pack_button.pack()
des_button = Button(root, text = "destroy", command = destroyframe)
des_button.pack()
Once I press the destroy button, I cannot pack it back again on the screen with the pack_button. Im not sure why is it so, but I would appreciate both explanaition and a solution
I am trying to display some text after a button is pressed but all I seem to be able to do make it so that text is displayed before the button is pressed or not at all.
here is my code so far:
import tkinter
def label1():
label2 = tkinter.Label(window1, text = "correct")
label.pack()
def Window2():
window1 = tkinter.Tk()
window1.title("start")
label = tkinter.Label(window1, text= "how do you spell this Sh--ld")
label.pack()
points = 0
i = points + 1
button = tkinter.Button(window1, text = "ou", command = label1)
button.pack()
window = tkinter.Tk()
window.title("menu")
button = tkinter.Button(window, text = "start", command = Window2)
button.pack()
I am trying to get the button in the Window2 subroutine to display the text
Here is how you can do it
import tkinter
def label1(root):
label = tkinter.Label(root, text = "correct")
label.pack()
def Window2():
window1 = tkinter.Tk()
window1.title("start")
label = tkinter.Label(window1, text= "how do you spell this Sh--ld")
label.pack()
points = 0
i = points + 1
button = tkinter.Button(window1, text = "ou", command = lambda root = window1: label1(root))
button.pack()
window = tkinter.Tk()
window.title("menu")
button = tkinter.Button(window, text = "start", command = Window2)
button.pack()
window.mainloop()
I want to display the image in a new window, but I get an error.
This is my error code
photo = PhotoImage(file='img/dog')
File "C:\Users\Hyojae\Anaconda3\lib\tkinter\__init__.py", line 3542, in __init__
Image.__init__(self, 'photo', name, cnf, master, **kw)
File "C:\Users\Hyojae\Anaconda3\lib\tkinter\__init__.py", line 3486, in __init__
raise RuntimeError('Too early to create image')
RuntimeError: Too early to create image
And this is my code sample.
I would appreciate your help.
from tkinter import *
def messageWindow():
win = Tk()
win.geometry('300x200')
root.destroy()
photo2 = PhotoImage(file="img/dog1.gif")
label1 = Label(win, image=photo2)
label1.grid(row=6)
Button(win, text='OK', command=win.destroy).grid(row = 5, columnspan = 2)
win.mainloop()
root = Tk()
photo = PhotoImage(file="img/dog2.gif")
label1 = Label(root, image=photo)
label1.pack()
Button(root, text='Bring up Message', command=messageWindow).pack()
root.mainloop()
You are getting such area because you call root.destroy before the image is loaded to the window.Also you can not use two TK instance you have to use Toplevel check the link to understand better.
Aside that to display image in toplevel you need to create reference for it so it will not be garbage collected Display image in Toplevel window
which i did this way label1.image = sub.
I also use image subsample to demonstrate how to resize image sub = photo2.subsample(5, 5) check this link to read about it
from tkinter import *
def messageWindow():
win = Toplevel()
win.geometry('300x200')
root.withdraw() # THIS HIDE THE WINDOW
photo2 = PhotoImage(file="img/dog1.gif")
sub = photo2.subsample(5, 5)
label1 = Label(win, image=sub)
label1.image = sub
label1.grid(row=6)
Button(win, text='OK', command=win.destroy).grid(row = 5, columnspan = 2)
root = Tk()
photo = PhotoImage(file="img/dog1.gif")
sub1 = photo.subsample(3,3)
label1 = Label(root, image=sub1)
label1.pack()
B = Button(root, text='Bring up Message', command=messageWindow)
B.place(x=200, y=300)
root.mainloop()
I have been creating a file that displays a image on a canvas. I created the 'PhotoImage' file that I use to store my picture.
d = PhotoImage(file="pic.gif")
canvas = Canvas(root, 500, 500)
pic = canvas.create_image(20, 20, image=d)
But I just produce an error each time I run the program...for some reason, PhotoImage will never work for me. How do I get this to work?
This is a an example that works for me.
#----------------------------------------------------------------------
import Tkinter
#----------------------------------------------------------------------
root = Tkinter.Tk()
frame = Tkinter.Frame(root)
frame = Tkinter.LabelFrame(root, text="LabelFrame text", padx=5, pady=5)
frame.pack(side=Tkinter.LEFT)
Label1 = Tkinter.Label(frame, text="Label1 text")
Label1.pack()
#----------------------------------------------------------------------
photo1 = Tkinter.PhotoImage(file = 'Chart_Example.gif')
#
width_1 = photo1.width()
height_1 = photo1.height()
#
x_center_1 = width_1 / 2.0
y_center_1 = height_1 / 2.0
#---------------------------------
iframe1 = Tkinter.Frame(frame, bd=2, relief=Tkinter.RAISED)
iframe1.pack(expand=1, fill=Tkinter.X, pady=5, padx=5, side=Tkinter.LEFT)
c1 = Tkinter.Canvas(iframe1, width=width_1, height=height_1)
c1.create_image(x_center_1, y_center_1, image=photo1, anchor = Tkinter.CENTER)
c1.pack(side=Tkinter.LEFT)
#----------------------------------------------------------------------
root.mainloop()
#----------------------------------------------------------------------