Tkinter:如何在画布中更改图像

时间:2018-05-20 09:11:15

标签: python tkinter

我知道已经有thread与此问题相关。但答案不起作用,从那以后没有跟进。

这是我用来更改图片的代码。单击按钮后,画布变为空白。

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        alert($("input:text").val());
    });
});
</script>
</head>
<body>

Firstname: <input type="text" name="fname" value="Peter"><br>
Lastname: <input type="text" name="lname" value="Griffin"><br><br>

<button>Return the value of the first input field</button>

</body>
</html>

然后我尝试使用以下代码对其进行调试,其中我将函数的代码移动到主代码中。

def change_image():
    photo2 = tk.PhotoImage(file="I.png")
    canvas.itemconfig(myimg,image=photo2)


root =tk.Tk()
root.title('Image show case')

canvas = Canvas(root,width=300, height=200, bg='white')
canvas.pack(expand=tk.YES, fill=tk.BOTH)
photo = tk.PhotoImage(file="V.png",master=root)
myimg = canvas.create_image(0,0,image=photo,anchor=tk.NW)

num1Entry = tk.Entry(root) #User choose the photo by typing names e.g. "cats", "cars"
num1Entry.pack(side=tk.LEFT)

Button = tk.Button(root,text='Clickme',command=change_image) #Button to change image
Button.pack(side = tk.LEFT )

第二张图片无需点击按钮即可正确显示。

单击按钮后会出现问题。但是,我无法确定确切的问题。

0 个答案:

没有答案