Tkinter添加背景图片

时间:2020-06-03 22:23:42

标签: python image tkinter

当我使用在这里找到的框架遵循多页格式时,我试图弄清楚如何为tkinter GUI创建背景图像:Switch between two frames in tkinter

我的问题是我的图像最终隐藏在框架/应用程序的背景图像后面,无法弄清楚如何解决它。

from tkinter import font  as tkfont
from tkinter import messagebox
import paho.mqtt.publish as publish
import sqlite3

class PowerhouseApp(tk.Tk):

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

    self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")
    self.geometry("720x480")

    # the container is where we'll stack a bunch of frames
    # on top of each other, then the one we want visible
    # will be raised above the others
    container = tk.Frame(self, bg='grey')
    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 (HomePage, LoginPage, LockerSelect, createUser):
        page_name = F.__name__
        frame = F(parent=container, controller=self)
        self.frames[page_name] = frame

        # put all of the pages in the same location;
        # the one on the top of the stacking order
        # will be the one that is visible.
        frame.grid(row=0, column=0, sticky = "nsew")

    self.show_frame("HomePage")

def show_frame(self, page_name):
    '''Show a frame for the given page name'''
    frame = self.frames[page_name]
    frame.tkraise()


class HomePage(tk.Frame):

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

这是我当前在其中添加照片的地方

    photo = PhotoImage("bg.pgm")
    mainTitle = Canvas(self, width=720, height=75)
    mainTitle.create_text(360,0, font=('arial',50,'bold'), fill='grey', text="Welcome", anchor="n")
    mainTitle.create_image(0,0, image=photo)
    mainTitle.grid(column=0, row=0, columnspan=3)

    button1 = Button(self, text="Login", font=('arial',15,'bold'), fg='blue', height=1, width=10, command=lambda: controller.show_frame("LoginPage"))
    button1.grid(column=0,row=2, pady=50)
    button2 = Button(self, text="New User", font=('arial',15,'bold'), fg='blue', height=1, width=10, command=lambda: controller.show_frame("createUser"))
    button2.grid(column=1,row=2, pady=50)
    button3 = Button(self, text="Exit", font=('arial',15,'bold'), fg='blue', height=1, width=10, command=parent.quit)
    button3.grid(column=2,row=2, pady=50)

def quit(parent):
    parent.destroy()

0 个答案:

没有答案
相关问题