如何在Tkinter中连续更新背景

时间:2020-07-24 00:00:49

标签: python tkinter

我有一个非常简单的设置。我有一个循环,只是通过x值。我希望用户决定是否希望再次循环。在循环中,根据x的条件弹出2张图像。在第一次迭代中,我可以遍历循环,并且图像会正确弹出,但是在后续迭代中,Tkinter窗口不再更新。

我了解after(),但是我试图理解为什么以下内容不起作用(从第1次迭代到第2次迭代之间什么都没有改变,所以为什么它不再起作用了?)

import tkinter as tk
from tkinter import *
from PIL import ImageTk, Image

Images='mountain.jpg'
class Example(Frame):
    global Images
    def __init__(self, master, *pargs):
        Frame.__init__(self, master, *pargs)



        self.image = Image.open(Images)
        self.img_copy= self.image.copy()


        self.background_image = ImageTk.PhotoImage(self.image)

        self.background = Label(self, image=self.background_image)
        self.background.pack(fill=BOTH, expand=YES)
        self.background.bind('<Configure>', self._resize_image)

    def _resize_image(self,event):

        new_width = event.width
        new_height = event.height

        self.image = self.img_copy.resize((new_width, new_height))

        self.background_image = ImageTk.PhotoImage(self.image)
        self.background.configure(image =  self.background_image)

def main(e):
    global Images
    x=0
    root.update_idletasks()
    while x<10000900:
        x+=1
        if x == 50:
            Images='mountain.jpg'
            e.destroy()
            e = Example(root)
            e.pack(fill=BOTH, expand=YES)
            root.update_idletasks()

        if x == 5000000:
            Images='greencar.jpg'
            e.destroy()
            e = Example(root)
            e.pack(fill=BOTH, expand=YES)
            root.update_idletasks()


def loop(e):
    while True:
        question=input('would you like to go again?')
        if question == 'n':
            break
        main(e)

root = tk.Tk()
root.geometry('600x600')
e = Example(root)
e.pack(fill=BOTH, expand=YES)
loop(e)
root.destroy()
root.mainloop()

编辑:要解决注释的实现:

import tkinter as tk
from tkinter import *
from PIL import ImageTk, Image
from tkinter import messagebox as mb


Images='mountain.jpg'
class Example(Frame):
    global Images
    def __init__(self, master, *pargs):
        Frame.__init__(self, master, *pargs)



        self.image = Image.open(Images)
        self.img_copy= self.image.copy()


        self.background_image = ImageTk.PhotoImage(self.image)

        self.background = Label(self, image=self.background_image)
        self.background.pack(fill=BOTH, expand=YES)
        self.background.bind('<Configure>', self._resize_image)

    def _resize_image(self,event):

        new_width = event.width
        new_height = event.height

        self.image = self.img_copy.resize((new_width, new_height))

        self.background_image = ImageTk.PhotoImage(self.image)
        self.background.configure(image =  self.background_image)

def main():
    global Images
    e = Example(root)
    e.pack(fill=BOTH, expand=YES)
    x=0
    while x<10000900:
        x+=1
        if x == 50:
            Images='mountain.jpg'
            e.destroy()
            e = Example(root)
            e.pack(fill=BOTH, expand=YES)
            root.update_idletasks()

        if x == 5000000:
            Images='greencar.jpg'
            e.destroy()
            e = Example(root)
            e.pack(fill=BOTH, expand=YES)
            root.update_idletasks()
    return e


def loop():
    while True:
        res=mb.askquestion('Replay', 'Would you like to play another round?')
        if res == 'yes' :
            e = main()
        else :
            root.destroy()

root = tk.Tk()
root.geometry('600x600')
loop()
root.mainloop()

1 个答案:

答案 0 :(得分:3)

尽管您已从e返回了main(),但没有在下一循环中将其传递回main()。以下是修改后的main()loop()

def main(e):
    global Images
    x=0
    while x<10000900:
        x+=1
        if x == 50:
            e.destroy()
            Images='images/nier-a2.png'
            e = Example(root)
            e.pack(fill=BOTH, expand=YES)
            root.update()

        if x == 5000000:
            e.destroy()
            Images='images/nier-a2_clicked.png'
            e = Example(root)
            e.pack(fill=BOTH, expand=YES)
            root.update()
    return e

def loop():
    e = Example(root)
    e.pack(fill=BOTH, expand=YES)
    root.update()
    while True:
        e = main(e)
        if mb.askquestion('Replay', 'Would you like to play another round?') != 'yes':
            break

root = tk.Tk()
root.geometry('600x600')
loop()
# you should not call `root.mainloop()` as `loop()` is used
#root.mainloop()