代码:
from tkinter import*
root = Tk()
root.title("Mood Control")
root.geometry("500x500")
label1 = Label(root, text="From 1-5 how would you rate your mood today? ", font=("arial",15, "bold"), fg="black").place(x=10, y=40)
def sad():
photo = PhotoImage(file="happyturtle.gif")
label = Label(image=photo).place(x=135, y=200)
label.image = photo
label.pack()
def happy():
photo = PhotoImage(file="sadcat.gif")
label = Label(image=photo).place(x=100, y=250)
label.image = photo
label.pack()
label2 = labe
button1 = Button(root, text="1", width=3, height=3, bg="lightgrey", command=sad).place(x=20, y=100)
button2 = Button(root, text="2", width=3, height=3, bg="white", command=sad).place(x=60, y=100)
button3 = Button(root, text="3", width=3, height=3, bg="lightgrey", command=happy).place(x=100, y=100)
button4 = Button(root, text="4", width=3, height=3, bg="white", command=happy).place(x=140, y=100)
button5 = Button(root, text="5", width=3, height=3, bg="lightgrey", command=happy).place(x=180, y=100)
root.mainloop()
基本上我试图制作一个有趣的小应用程序来控制你的心情,你永远不会太开心或悲伤。因此,如果我要按1或2,它会显示一张快乐的乌龟来为你加油。 如果我要按3-5,它会显示一张伤心猫的照片,以确保你不太开心。我已经弄明白了但是我遇到了一个问题,我希望在显示图像后方有文字,例如“你是 太开心了,这里有一只悲伤的猫咪。“但是当我试图这样做时,它从不显示文字。我试图这样做:
def happy():
photo = PhotoImage(file="sadcat.gif")
label = Label(image=photo).place(x=100, y=250)
label.image = photo
label.pack()
label2 = Label(root, text="You are too happy, here's a picture of a sad cat", font=("arial",10, "bold"), fg="black").place(x=100, y=40)
# above line is what is supposed to display the text, it doesnt.
同样我鼠标悬停在“label2”上,它表示“局部变量”label2“未被使用”。我该如何解决这个问题?
任何答案都是适当的
答案 0 :(得分:0)
快乐功能存在一些问题。
def happy():
photo = PhotoImage(file="sadcat.gif")
label = Label(image=photo).place(x=100, y=250)
label.image = photo
label.pack()
label2 = Label(root, text="You are too happy, here's a picture of a sad cat", font=("arial",10, "bold"), fg="black").place(x=100, y=40)
# above line is what is supposed to display the text, it doesnt.
行label = Label(image=photo).place(x=100, y=250)
将label变量设置为place()的返回值。按地点返回的对象将没有图像属性,因此行label.image = photo
将导致错误。如果您想要引用标签,请尝试:
photo = PhotoImage(file="sadcat.gif")
label = Label(image=photo)
label.place(x=100, y=250)
现在label.image = photo
不会导致错误,但是当您执行Label(image=photo)
时设置图像时可以取出此行。在您使用场所几何管理器时,应删除label.pack()
。
离开:
def happy():
photo = PhotoImage(file="sadcat.gif")
label = Label(image=photo).place(x=100, y=250)
label2 = Label(root, text="You are too happy, here's a picture of a sad cat", font=("arial",10, "bold"), fg="black").place(x=100, y=40)
# above line is what is supposed to display the text, it doesnt.
编辑:
from tkinter import*
root = Tk()
root.title("Mood Control")
root.geometry("500x500")
label1 = Label(root, text="From 1-5 how would you rate your mood today? ", font=("arial",15, "bold"), fg="black")
label1.place(x=10, y=40)
def sad():
photo = PhotoImage(file="tenor1.gif")
label = Label(root, image=photo)
label.place(x=100, y=250)
label.image = photo
def happy():
label1['text'] = "You are too happy, here's a picture of a sad cat"
photo = PhotoImage(file="tenor.gif")
label = Label(root, image=photo)
label.place(x=100, y=250)
label.image = photo
# above line is what is supposed to display the text, it doesnt.
button1 = Button(root, text="1", width=3, height=3, bg="lightgrey", command=sad).place(x=20, y=100)
button2 = Button(root, text="2", width=3, height=3, bg="white", command=sad).place(x=60, y=100)
button3 = Button(root, text="3", width=3, height=3, bg="lightgrey", command=happy).place(x=100, y=100)
button4 = Button(root, text="4", width=3, height=3, bg="white", command=happy).place(x=140, y=100)
button5 = Button(root, text="5", width=3, height=3, bg="lightgrey", command=happy).place(x=180, y=100)
root.mainloop()