【问题标题】:Show image in tkinter Button; garbage collection / switching Frames issue?在 tkinter 按钮中显示图像;垃圾收集/切换帧问题?
【发布时间】:2019-08-14 13:14:43
【问题描述】:

我对 python(和 tkinter)比较陌生,而且我似乎也对 OOP 有困难。但我正在尝试:)

我在 SO 上找到了这个,并在我的代码中实现了它:

我收到一些错误,例如: " TclError: 图像 "pyimagexx" 不存在 "

我想我发现这是一个垃圾收集问题?但我想我已经按照 original.load() 的步骤来规避它?

并且: - tk.Tk.configure(self, background="black")

不改变框架的背景颜色。我想在哪里更改框架的外观?

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

class myApp(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)

        tk.Tk.resizable(self, width=False, height=False)
        tk.Tk.configure(self, background="black")

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (LoginPage, PageOne, PageTwo):

            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(LoginPage)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

class LoginPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)

        player_name = tk.Label(self, text='Login Name').grid(row=1, column=1)
        player_name_entry = tk.Entry(self).grid(row=2, column=1)        
        player_pass = tk.Label(self, text='Password').grid(row=3, column=1)
        player_pass_entry = tk.Entry(self).grid(row=4, column=1)

        button = ttk.Button(self, text="Login", command=lambda: [controller.show_frame(PageOne)]).grid(row=5, column=1)


class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        button1 = tk.Button(self, text="Logout",
                            borderwidth=0,
                            command=lambda: [controller.show_frame(LoginPage), self.logout()])     

        original = Image.open('Untitled.gif')
        ph_im = ImageTk.PhotoImage(original)
        button1.config(image=ph_im)
        original.load()
        button1.grid(row=2, column=1)


        button2 = tk.Button(self, text="PageTwo",
                            command=lambda: [controller.show_frame(PageTwo)])
        button2.grid(row=2, column=2)

    def logout(self):
        print("Logged out")

class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        button1 = tk.Button(self, text="Logout",
                             borderwidth=0,
                             command=lambda: [controller.show_frame(LoginPage), self.logout()])
        button1.grid(row=2, column=1) 

        button2 = tk.Button(self, text="PageOne",
                             command=lambda: [controller.show_frame(PageOne)])
        button2.grid(row=2, column=2)

    def logout(self):
        print("Logged out")

app = myApp()
app.geometry("1500x750")
app.mainloop()```

【问题讨论】:

  • 每一帧都有自己的颜色——而且它不是透明的颜色——你只能在主窗口Tk()改变颜色——而不是在帧中。所以黑色可以隐藏在框架后面。在Pages 你可能需要self.configure(background="black")
  • PageOne 中使用button1.image = ph_im 然后GC 不会从内存中删除图像。你会看到图像。或者在所有代码中使用self.ph_im 而不是ph_im

标签: python tkinter garbage-collection


【解决方案1】:

尝试下一个代码来检查您的路径。我觉得你的相对路径不好。如果/image.gif 不起作用,则可能您的路径位于其他文件夹中,您需要执行正确的相对路径。 folder_example/folder_example/image.gif (视窗)

import os
print(os.system('dir'))

(Debian)

import os
print(os.system('ls'))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-23
    • 2011-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-17
    相关资源
    最近更新 更多