python - 一种在满足特定条件时在 python 程序中更新图像的方法(使用 Tkinter 和 PIL)
问题描述
我不太精通 Tkinter,并且使用多个站点为一个项目创建这个程序。这是一个测验程序,它使用 GUI 界面在问题上方显示横幅。我需要一种方法来根据某人何时获得满分/低分来更新横幅。另外我想知道我是否需要删除标签的 PIL 来容纳图像。所有建议将不胜感激。
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
不需要进一步导入任何东西的方法。
不确定这是否可行,无法运行您的代码,请查看并告诉我。
推荐阅读
- javascript - 如何简化 `(variableA && !variableB) || JavaScript 中的!variableA` 表达式?
- sql - 数据库关系取决于表类型
- javascript - 使用带有 localStorage 的 javascript 变量
- r - 更改了 R 4.0.0 文本编码行为?
- iphone - Xcode 设备问题 - 即使设备未被密码锁定,_shouldMakeReadyForDevelopment 检查失败
- typescript - 如何使用可选参数定义通用接口
- python - 通过空索引
- rust - Rust 中的结构成员双重可变借用
- r - 1 var 相对于其他变量的 R 表平均值
- python - 使用 Pandas 读取数据并跳过元数据