1

I'm trying to add a background to my Tkinter window, but when I use this code, then It opens the background as a different Tkinter window and the main window as a seperate one. How can I make them into one?

from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter import messagebox

top = Tk()

C = Canvas(top, bg ="blue", height=250, width=300)
filename = PhotoImage(file = "C:/Users/plapl/Desktop/ching.pgm")
background_label = Label(top, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

C.pack()
top.mainloop()


def newfile():
    print("New File!")


root = Tk()
menu = Menu(root)
root.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=newfile)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=root.quit)


mainloop()
Axsor
  • 69
  • 6

1 Answers1

2

I have simplified your code and now it works for me in 1 window and also i imported a module you might have missed (PIL)

Code:

from tkinter import *
from PIL import ImageTk
from tkinter.filedialog import askopenfilename
from tkinter import messagebox

top = Tk()

C = Canvas(top, bg ="blue", height=250, width=300)
filename = ImageTk.PhotoImage(file = "C:/Users/plapl/Desktop/ching.pgm")
background_label = Label(top, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
C.pack()


def newfile():
    print("New File!")


menu = Menu(top)
top.config(menu=menu)
filemenu = Menu(menu)
menu.add_cascade(label="File", menu=filemenu)
filemenu.add_command(label="New", command=newfile)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=top.quit)


top.mainloop()
Delrius Euphoria
  • 14,910
  • 3
  • 15
  • 46