在满足特定条件时更新python程序中图像的方法(使用Tkinter和PIL)
我不是很精通Tkinter,并使用多个网站为一个项目创建此程序。这是一个测验程序,使用GUI界面在问题上方显示横幅。我需要一个方法来更新横幅的基础上,当有人达到了满分/低分。此外,我想知道我是否需要删除一个标签,以容纳图像的PIL。所有建议将不胜感激在满足特定条件时更新python程序中图像的方法(使用Tkinter和PIL),python,tkinter,python-imaging-library,Python,Tkinter,Python Imaging Library,我不是很精通Tkinter,并使用多个网站为一个项目创建此程序。这是一个测验程序,使用GUI界面在问题上方显示横幅。我需要一个方法来更新横幅的基础上,当有人达到了满分/低分。此外,我想知道我是否需要删除一个标签,以容纳图像的PIL。所有建议将不胜感激 from tkinter import * from time import sleep from PIL import ImageTk,Image class Question: def __init__(self, question
from tkinter import *
from time import sleep
from PIL import ImageTk,Image
class Question:
def __init__(self, question, answers, correctLetter):
self.question = question
self.answers = answers
self.correctLetter = correctLetter
def check(self, letter, view):
global right
if(letter == self.correctLetter):
label2= Label(view, text="Right!")
right += 1
else:
label2= Label(view, text="Wrong!")
label2.pack()
view.after(1000, lambda *args: self.unpackView(view))
def getView(self, window):
view = Frame(window)
label1 = Label(view, text=self.question)
button_a = Button(view, text=self.answers[0], command=lambda *args: self.check("A", view))
button_b = Button(view, text=self.answers[1], command=lambda *args: self.check("B", view))
button_c = Button(view, text=self.answers[2], command=lambda *args: self.check("C", view))
button_d = Button(view, text=self.answers[3], command=lambda *args: self.check("D", view))
label1.pack()
button_a.pack()
button_b.pack()
button_c.pack()
button_d.pack()
return view
def unpackView(self, view):
view.pack_forget()
askQuestion()
def askQuestion():
global questions, window, index, button, right, number_of_questions
if(len(questions) == index + 1):
Label(window, text="Thank you for answering the questions. " + str(right) + " of " + str(number_of_questions) + " questions answered right").pack()
return
button.pack_forget()
index += 1
questions[index].getView(window).pack()
questions = []
file = open('C:\\Users\\tjohn\\Desktop\\Art Intergration\\Comp\\questions.txt', "r")
line = file.readline()
while(line != ""):
questionString = line
answers = []
for i in range (4):
answers.append(file.readline())
correctLetter = file.readline()
correctLetter = correctLetter[:-1]
questions.append(Question(questionString, answers, correctLetter))
line = file.readline()
file.close()
index = -1
right = 0
number_of_questions = len(questions)
window = Tk()
C = Canvas(window, bg="blue", height=350, width=800)
C.pack()
img = ImageTk.PhotoImage(Image.open("C:\\Users\\tjohn\\Desktop\\quiz.png"))
C.create_image(800/2, 350/2, anchor=CENTER, image=img)
button = Button(window, text="Start", command=askQuestion)
button.pack(anchor=CENTER)
window.mainloop()
添加作为答案,因为评论太长: 向画布图像添加标记,如:
window = Tk()
C = Canvas(window, bg="blue", height=350, width=800)
C.pack()
img = ImageTk.PhotoImage(Image.open("C:\\Users\\tjohn\\Desktop\\quiz.png"))
C.create_image(800/2, 350/2, anchor=CENTER, image=img,tags='pic')
稍后在函数内部说:
def askQuestion():
global questions, window, index, button, right, number_of_questions
if(len(questions) == index + 1):
Label(window, text="Thank you for answering the questions. " + str(right) + " of " + str(number_of_questions) + " questions answered right").pack()
new_img = ImageTk.PhotoImage(Image.open('New path'))
c.itemconfigure('pic',image=new_img)
return
button.pack_forget()
index += 1
questions[index].getView(window).pack()
itemconfigure()
是一种tkinter.Canvas
方法,无需进一步导入任何内容
不确定这是否可行,无法运行代码,请查看并通知我。要更新哪个函数或更新位置?我要更新程序末尾画布中使用的图像。。。。我猜,当函数打印感谢消息时,它将在askQuestion()的函数定义中完成……尝试将初始画布图像更改为
C.create\u image(800/2350/2,anchor=CENTER,image=img,tag='pic')
,当您想要更新它时,说C.itemconfigure('pic',image=newpic)
嗯,我尝试了这个方法,但只是想澄清一下,使用itemconfigure是否有任何先决条件,比如任何要导入的模块或任何要插入到任何部分的新代码?我可能做了错误的代码,但我得到了一个很长的错误列表…这就是我放在这里的def askQuestion():全局问题、窗口、索引、按钮,对吗,问题数量if(len(questions)==index+1):标签(window,text=“感谢您回答问题。”+str(right)+“of”+str(questions)+“questions responsed right”).pack()if right==问题数量:C.itemconfigure('pic',image=“C:\\Users\\tjohn\\Desktop\\congreats.png”)return按钮。pack_forget()index+=1
它肯定会更新…但是新图像与前一图像不一致…两者大小相同。。。。有没有需要锚的地方?真奇怪,我想它会被取代,我不知道为什么?可能包括一个屏幕截图或问一个新问题