簡體   English   中英

Tkinter更改畫布上的圖像

[英]Tkinter changing image on a canvas

因此,我寫了一些代碼,為數學專業的學生顯示有關某個主題的隨機問題。 模塊定義顯示有關所選主題的初始圖像。 然后,在相應按鈕下使用下一個問題和分數方案定義。 下一個問題可以正常工作,但是因為標記方案定義已鏈接到模塊定義,所以該問題不會更新。 僅顯示第一個問題的評分方案。 如何使用下一個問題按鈕進行更新?

def module(mod):
    def mark_scheme():
        questions.delete('all')
        scheme=random_question.replace('.GIF', 'MS.GIF')
        answer=ImageTk.PhotoImage(file=scheme)
        lab1.configure(image=answer)
        lab1.image=answer
        lab1.pack(fill=BOTH, expand=1)
        return

    def next_question():

        questions.delete('all')
        random_question=random.choice(mod)
        question=ImageTk.PhotoImage(file=random_question)
        lab1.configure(image=question)
        lab1.image=question
        lab1.pack(fill=BOTH, expand=1)
        return

    questions=Canvas(root)
    questions.pack(fill=BOTH, expand=1)
    lab1=Label(questions)
    random_question=random.choice(mod)                            
    question=ImageTk.PhotoImage(file=random_question) 
    lab1.configure(image=question)
    lab1.image=question
    lab1.pack(fill=BOTH, expand=1)
    button1=Button(root, text='Next Question', command=next_question).pack()
    button2=Button(root, text='Mark Scheme', command=mark_scheme).pack()
    return

這里不需要使用Canavas小部件,因為您只需一個簡單的Label就可以完成所有這些操作。 同樣,您選擇使用閉包來包裝代碼並停止對圖像引用進行垃圾回收的選擇有點不尋常。 您可能會轉向以下簡單的類結構更好。

以下示例顯示了如何在Tkinter中更新標簽圖像。 下面的代碼而不是隨機選擇圖像,而是循環瀏覽當前目錄中的所有PNG文件。

import Tkinter as tk
from PIL import ImageTk
import glob

IMAGES = glob.glob("*.png")
DEFAULT_IMAGE = IMAGES[0]

class QuestionWindow(tk.Frame):
    def __init__(self,parent):
        self.parent=parent
        tk.Frame.__init__(self)
        self.init_window()

    def update_image(self,move=0):
        """
        move = +1 jumps to the next image in the sequence
        move = -1 skips back to the previous image in the sequence
        move = +2 jumps... you get it.
        """
        self.img_idx += move
        fname = IMAGES[self.img_idx%len(IMAGES)]

        # here generate the image and make sure a reference is 
        # kept in the form of a class attribute.
        self.img = ImageTk.PhotoImage(file=fname)

        # set the label image.
        self.question_lbl.configure(image=self.img)

    def init_window(self):
        self.img_idx = 0

        # choose a default image for startup
        self.img = ImageTk.PhotoImage(file=DEFAULT_IMAGE)

        # This is the question label that will contain the image
        self.question_lbl = tk.Label(self,image=self.img)
        self.question_lbl.pack(anchor=tk.N, side=tk.TOP, fill=tk.BOTH, expand=1)

        # These are the next and prev buttons. Note that they are using lambdas to
        # generate partial functions that call update_image,
        self.next_btn = tk.Button(self,text="Next",command=lambda:self.update_image(+1))
        self.next_btn.pack(anchor=tk.S,side=tk.RIGHT)

        self.prev_btn = tk.Button(self,text="Previous",command=lambda:self.update_image(-1))
        self.prev_btn.pack(anchor=tk.S,side=tk.LEFT)


if __name__ == "__main__":
    root = tk.Tk()
    qw = QuestionWindow(root)
    qw.pack()
    root.mainloop()

照原樣,窗口將根據所顯示的圖像調整大小,但是可以通過固定窗口的幾何形狀進行更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM