如何在tkinter中添加徽标图像?

2024-06-26 00:30:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在我的GUI程序中添加一些图像,但它有问题

我使用了以下代码:

import tkinter as tk
import tkinter.simpledialog as tkSimpleDialog
import tkinter.messagebox as tkMessageBox
from tkinter import ttk
import time
from PIL import ImageTk, Image

LargeFont = ("Verdana", 12)
class PageContainer(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.iconbitmap(self,default="ico_image.ico")

        container = tk.Frame(self)
        tk.Tk.geometry(self,'800x600')
        container.pack(side='top', fill='both', expand = True )
        container.grid_rowconfigure(0, weight = 1)
        container.grid_columnconfigure(0, weight = 1)

        self.frame = {}

        for F in (StartPage, Local_Window,Remote_Window,Manual_Window,Automatic_Window):
            frame = F(container, self)
            self.frame[F] = frame
            frame.grid(row = 0, column = 0, sticky = "nsew")
        self.show_frame(StartPage)

    def show_frame(self, cont):

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

class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Maintenance System", font = LargeFont)
        label.pack(pady=10,padx=10)
        button1 = tk.Button(self, text = "Local",
            command = lambda: controller.show_frame(Local_Window), width = 20, height = 3)
        button2 = tk.Button(self, text = " Remote ",
            command = lambda: controller.show_frame(Remote_Window), width = 20, height = 3)
        button3 = tk.Button(self, text=" Exit ",
                            command=lambda: StartPage.quit(self), width=20, height=3)
        button1.place(x = 325, y = 200)
        button2.place(x = 325, y = 300)
        button3.place(x = 100, y = 500)
        acwa_power_img = Image.open("ACWA_Power_logo.png")
        acwa_power_img = acwa_power_img.resize((150, 80), Image.ANTIALIAS)
        acwa_power_img = ImageTk.PhotoImage(acwa_power_img)
        acwa_power_label = tk.Label(self, image=acwa_power_img)
        acwa_power_label.place(x = 0, y = 0)

        hiwpt_img = Image.open("hiwpt2.jpg")
        hiwpt_img = hiwpt_img.resize((150, 80), Image.ANTIALIAS)
        hiwpt_img = ImageTk.PhotoImage(hiwpt_img)
        hipwt_label = tk.Label(self, image=hiwpt_img)
        hipwt_label.place(x = 646, y =0 )
        StartPage.configure(self,background='grey')
app = PageContainer()
app.mainloop()

这是我运行程序时得到的结果:

main GUI of my program

图像很好,dir也正常,有什么问题吗

我找了很多,没有找到任何解决办法


Tags: imageimportselfimgtkintercontainerplacewindow