Ein Bild als Hintergrund festlegen?

Fragen zu Tkinter.
Antworten
dolu28
User
Beiträge: 3
Registriert: Montag 7. Mai 2018, 15:05

Sonntag 10. Juni 2018, 18:37

Hallo,
ich habe neu mit GUI angefangen und habe schon ein Fenster erstellt. Nun wollte ich ein Bild als Hintergrund festlegen, aber mir ist das nicht gelungen. Ich habe paar Beispiele mit canvas gesehen, aber es nicht verstanden.

Hier mein Code:

Code: Alles auswählen

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

TITLE = ('Arial', 22, 'bold italic underline')

class LoLApp(tk.Tk):

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

        tk.Tk.__init__(self, *args, **kwargs )
        tk.Tk.wm_title(self, 'Test')
        tk.Tk.geometry(self, '800x400')

        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={}
        frame = StartPage(container, self)        
        self.frames[StartPage]=frame
        frame.grid(row=0, column=0, sticky='nsew')
        self.show_frame(StartPage)

    def show_frame(self, cont):

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

class StartPage(tk.Frame):

     def __init__(self, parent, controller):
         tk.Frame.__init__(self, parent)
         label = tk.Label(self, text='Welcome to the First Page', font = TITLE )
         label.pack()

app = LoLApp()
app.mainloop()
Wäre über jede Hilfe dankbar
LG
Benutzeravatar
wuf
User
Beiträge: 1483
Registriert: Sonntag 8. Juni 2003, 09:50

Sonntag 10. Juni 2018, 23:38

Hi dolu28

Variante 1 ohne PIL:

Code: Alles auswählen

import tkinter as tk

APP_TITLE = "Hintergrundbild"
APP_XPOS = 100
APP_YPOS = 100
APP_WIDTH = 800
APP_HEIGHT = 400


class Application(tk.Label):
    
    def __init__(self, app_win, **kwargs):
        self.app_win = app_win

        self.backgound_image = tk.PhotoImage(file="hintergrundbild.png")
        tk.Label.__init__(self, app_win, image=self.backgound_image, **kwargs)
        self.propagate(False)
        
        self.build()
        
    def build(self):
        self.button_frame = tk.Frame(self)
        self.button_frame.pack(side='bottom', pady=4)
        tk.Button(self.button_frame, text="Beenden",
            command=self.app_win.destroy).pack()

    
def main():
    app_win = tk.Tk()
    app_win.title(APP_TITLE)
    app_win.geometry("+{}+{}".format(APP_XPOS, APP_YPOS))
    #app_win.geometry("{}x{}".format(APP_WIDTH, APP_HEIGHT))
    app_win.option_add("*highlightThickness", 0)
    
    app = Application(app_win, bg='steelblue')
    app.pack(fill='both', expand=True, padx=0, pady=0)
    
    app_win.mainloop()
 
 
if __name__ == '__main__':
    main()
Variante 2 mit PIL:

Code: Alles auswählen

import tkinter as tk
from PIL import Image, ImageTk

APP_TITLE = "Hintergrundbild"
APP_XPOS = 100
APP_YPOS = 100
APP_WIDTH = 800
APP_HEIGHT = 400


class Application(tk.Label):
    
    def __init__(self, app_win, **kwargs):
        self.app_win = app_win
        
        self.pil_backgound_image = Image.open("hintergrundbild.jpg")
        self.tk_image = ImageTk.PhotoImage(self.pil_backgound_image)
        tk.Label.__init__(self, app_win, image=self.tk_image, **kwargs)
        self.propagate(False)
        
        self.build()
        
    def build(self):
        self.button_frame = tk.Frame(self)
        self.button_frame.pack(side='bottom', pady=4)
        tk.Button(self.button_frame, text="Beenden",
            command=self.app_win.destroy).pack()

    
def main():
    app_win = tk.Tk()
    app_win.title(APP_TITLE)
    app_win.geometry("+{}+{}".format(APP_XPOS, APP_YPOS))
    #app_win.geometry("{}x{}".format(APP_WIDTH, APP_HEIGHT))
    app_win.option_add("*highlightThickness", 0)
    
    app = Application(app_win)
    app.pack(fill='both', expand=True)
    
    app_win.mainloop()
 
 
if __name__ == '__main__':
    main()
Gruss wuf ;-)
Take it easy Mates!
Antworten