GUI是指使用Python和tkinter创建的类中的图形用户界面。

huangapple go评论112阅读模式
英文:

GUI in a class with python + tkinter

问题

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

class Gui():
    
    def __init__(self, window):       
        File = "/home/pi/Rainer/Graphic/Bahn01.png"
        img = self.load_image(File, 0.5)
       
        canvas = tk.Canvas(window, bg='white')
        canvas.place(x=10, y=10, width=300, height=400)
        canvas.create_image(0, 0, image=img, anchor="nw")
        window.update()

    def load_image(self, File, Zoom):
        img = Image.open(File)
        resized_image = img.resize((int(600 * Zoom), int(800 * Zoom)), Image.LANCZOS)
        return ImageTk.PhotoImage(resized_image)

def main():  
    
    root = tk.Tk()
    root.geometry("620x420+320+20")
        
    App = Gui(root)
    
    time.sleep(3)

    root.mainloop()


if __name__ == '__main__':
    main()

这是你提供的代码的翻译部分。

英文:

I have a problem putting a GUI in a class. Loading an image from a file, resize and display it on a canvas works as long the complete code runs in main().
In the code below I do the same steps in a class. The image is shown, but entering the mainloop() the canvas is cleared.

Many thanks

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

class Gui():
    
    def __init__(self, window):       
        File = "/home/pi/Rainer/Graphic/Bahn01.png"
        img = self.load_image(File, 0.5)
       
        canvas = tk.Canvas(window, bg='white')
        canvas.place(x=10, y=10, width=300, height=400)
        canvas.create_image(0, 0, image=img, anchor="nw")
        window.update()

    def load_image(self, File, Zoom):
        img = Image.open(File)
        resized_image = img.resize((int(600 * Zoom), int(800 * Zoom)), Image.Resampling.LANCZOS)
        return ImageTk.PhotoImage(resized_image)

def main():  
    
    root = tk.Tk()
    root.geometry("620x420+320+20")
        
    App = Gui(root)
    
    time.sleep(3)

    root.mainloop()


if __name__ == '__main__':
    main()

答案1

得分: 1

你遇到的问题是因为 Gui 类中的 img 变量是 __init__ 方法中的局部变量。当方法执行完毕时,img 变量不再被引用,因此被垃圾回收,导致图像从画布中消失。

要解决这个问题,你需要将图像存储为 Gui 类的实例变量。可以通过使用 self 关键字来实现。

以下是你的代码的更新版本:

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

class Gui():
    
    def __init__(self, window):       
        File = "/home/pi/Rainer/Graphic/Bahn01.png"
        self.img = self.load_image(File, 0.5)
       
        self.canvas = tk.Canvas(window, bg='white')
        self.canvas.place(x=10, y=10, width=300, height=400)
        self.canvas.create_image(0, 0, image=self.img, anchor="nw")
        window.update()

    def load_image(self, File, Zoom):
        img = Image.open(File)
        resized_image = img.resize((int(600 * Zoom), int(800 * Zoom)), Image.LANCZOS)
        return ImageTk.PhotoImage(resized_image)

def main():  
    root = tk.Tk()
    root.geometry("620x420+320+20")
        
    App = Gui(root)
    
    time.sleep(3)

    root.mainloop()

if __name__ == '__main__':
    main()

更多帮助可以参考这个 答案

英文:

The issue you're facing is because the img variable in your Gui class is a local variable within the __init__ method. When the method finishes executing, the ``img variable is no longer referenced and gets garbage-collected. This causes the image to disappear from the canvas.

To fix this, you need to store the image as an instance variable of the Gui class. You can do this by using the self keyword.

Here's an updated version of your code:

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

class Gui():
    
    def __init__(self, window):       
        File = "/home/pi/Rainer/Graphic/Bahn01.png"
        self.img = self.load_image(File, 0.5)
       
        self.canvas = tk.Canvas(window, bg='white')
        self.canvas.place(x=10, y=10, width=300, height=400)
        self.canvas.create_image(0, 0, image=self.img, anchor="nw")
        window.update()

    def load_image(self, File, Zoom):
        img = Image.open(File)
        resized_image = img.resize((int(600 * Zoom), int(800 * Zoom)), Image.Resampling.LANCZOS)
        return ImageTk.PhotoImage(resized_image)

def main():  
    root = tk.Tk()
    root.geometry("620x420+320+20")
        
    App = Gui(root)
    
    time.sleep(3)

    root.mainloop()


if __name__ == '__main__':
    main()

You can see this answer for more help.

huangapple
  • 本文由 发表于 2023年6月21日 22:57:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76524657.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定