我在标签中有一个图像,我希望在按下按钮时更改图像,但不是更改图像,而是窗口变为空白:
from tkinter import *
from PIL import Image,ImageTk
import os
root = Tk()
root.state("zoomed")
def chng():
photo2 = ImageTk.PhotoImage(Image.open("upload.jpg"))
img.config(image = photo2)
img.grid()
photo = ImageTk.PhotoImage(Image.open("cat.jpg"))
img = Label(root,image = photo)
upload = Button(root, text= "Upload" ,height = 3, width = 12, command =
chng)
upload.grid( )
for col_num in range(root.grid_size()[1]):
root.columnconfigure(col_num, minsize=600)
for row_num in range(root.grid_size()[1]):
root.rowconfigure(row_num, minsize=60)
img.grid()
root.mainloop()
答案 0 :(得分:3)
您必须保留对图像的引用。
def chng():
photo2 = ImageTk.PhotoImage(Image.open("upload.jpg"))
img.config(image = photo2)
img.photo_ref = photo2 # keep a reference
您不需要额外的grid()
电话。
答案 1 :(得分:0)
试试这个:
<?php
define('cute', TRUE);
?>
答案 2 :(得分:-1)
import Tkinter as tk
from PIL import Image
class Application(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.grid(row=5,column=2)
self.createWidgets()
top = self.winfo_toplevel()
def createWidgets(self):
self.grid()
app = Application()
simge = tk.PhotoImage(file="bg.png")
hira1 = tk.Label(image=simge)
hira1.grid(row=0,column=0,rowspan=5)
def manu1(event):
simen1=tk.PhotoImage(file="bg1.png")
hira1.configure(image=simen1)
hira1(image=simen1)
hira1.bind("<Button 1>",manu1)
app.mainloop()