首页 > 解决方案 > 一种在满足特定条件时在 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()

标签: pythontkinterpython-imaging-library

解决方案


添加作为答案,因为评论太长:

为画布图像添加标签,例如:

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不需要进一步导入任何东西的方法。

不确定这是否可行,无法运行您的代码,请查看并告诉我。


推荐阅读