Как вставить картинку на заставку? - PullRequest
0 голосов
/ 29 июня 2018

Я просто хочу знать, как я могу вставить картинку в заставку?

from tkinter import *


#splash screen

class SplashScreen(Frame):
    def __init__(self, master=None, width=0.8, height=0.6, useFactor=True):
        Frame.__init__(self, master)
        self.pack(side=TOP, fill=BOTH, expand=YES)

        # get screen width and height

        ws = self.master.winfo_screenwidth()
        hs = self.master.winfo_screenheight()
        w = (useFactor and ws * width) or width
        h = (useFactor and ws * height) or height
        # calculate position x, y
        x = (ws / 2) - (w / 2)
        y = (hs / 2) - (h / 2)
        self.master.geometry('%dx%d+%d+%d' % (w, h, x, y))
        self.master.overrideredirect(True)
        self.lift()


if __name__ == '__main__':
    root = Tk()
    sp = SplashScreen(root)
    sp.config(bg="#3632ff")


    m = Label(sp, text="MH60 NAVIGATION APP")
    m.pack(side=TOP, expand=YES)
    m.config(bg="#3366ff", justify=CENTER, font=("calibri", 29))

    Button(sp, text="PRESS TO START", bg='red', command=root.destroy).pack(side=BOTTOM, fill=X)
    root.mainloop()

1 Ответ

0 голосов
/ 29 июня 2018

Просто добавьте какой-нибудь виджет с изображением в ваш экземпляр SplashScreen. Например, скажем, ваше изображение заставки было .gif:

splash.gif image

Тогда добавление его в ваш код будет выглядеть примерно так (через виджет Button):

from tkinter import *

class SplashScreen(Frame):
    def __init__(self, master=None, width=0.8, height=0.6, useFactor=True):
        Frame.__init__(self, master)
        self.pack(side=TOP, fill=BOTH, expand=YES)

        # Add widget with the splash screen image on it.
        self.img = PhotoImage(file='splash.gif')
        btn = Button(self, image=self.img)
        btn.pack(expand=YES, ipadx=10, ipady=10)

        # get screen width and height
        ws = self.master.winfo_screenwidth()
        hs = self.master.winfo_screenheight()
        w = (useFactor and ws * width) or width
        h = (useFactor and ws * height) or height

        # calculate position x, y
        x = (ws / 2) - (w / 2)
        y = (hs / 2) - (h / 2)
        self.master.geometry('%dx%d+%d+%d' % (w, h, x, y))
        self.master.overrideredirect(True)

        self.lift()


if __name__ == '__main__':
    root = Tk()
    sp = SplashScreen(root)
    sp.config(bg="#3632ff")

    m = Label(sp, text="MH60 NAVIGATION APP")
    m.pack(side=TOP, expand=YES)
    m.config(bg="#3366ff", justify=CENTER, font=("calibri", 29))

    Button(sp, text="PRESS TO START", bg='red', command=root.destroy).pack(side=BOTTOM, fill=X)
    root.mainloop()

Вот как это работает в моей системе:

screenshot with splash screen displayed

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...