Reputation: 84
I can get tkinter to display the windows or the background image, does anyone have a suggestion of how to get both displayed at once (I want the objects to go onto the background image), my code is below:
from tkinter import *
from tkinter import ttk
root= Tk()
# Code to add widgets will go here...
root.title("MTGO Daily Decklists")
def test():
print("things")
# pick a .gif image file you have in the working directory
# or give full path
image1 = PhotoImage(file="backgroundimage.gif")
w = Canvas(root, width=800, height=700,)
background = PhotoImage(file = "backgroundimage.gif")
w.create_image(500, 500, image=image1)
w.pack()
format_mtg= StringVar()
format_entry= ttk.Entry(w, width=25, textvariable=format_mtg)
format_entry_window = w.create_window(10, 10, anchor='n', window=format_entry)
format_entry.pack()
date= StringVar()
date_entry=ttk.Entry(root, width=25, textvariable=date)
date_entry_window = w.create_window(10, 10, anchor='n', window=date_entry)
date_entry.pack()
ttk.Label(w, text="input format here").pack()
ttk.Button(w, text="fetch", command=test).pack()
ttk.Label(w, text="input date here").pack()
sortby= StringVar()
sortby_entry= ttk.Entry()
sortby_entry.pack()
ttk.Label(w, text="input how you want the decklists to be sorted").pack()
root.mainloop()
Upvotes: 1
Views: 2155
Reputation: 385870
You are placing the center of the image at 500x500. However, after the program starts your window is only about 300x200. Your image is likely there, but off of the visible portion of the screen.
Even though you set the size of the canvas to 800x700, you are packing widgets inside the canvas. This causes the canvas to shrink to fit its contents. Compounding that, you don't use the expand
or fill
options when packing the canvas so the end result is that the interior of the GUI shrinks down to the minimum size.
note: if you are using create_window
to add a window to a canvas, you should not also call grid
or pack
on that window. You need to call either create_window
or pack
, but not both. Whichever one you call last is the one that has any effect.
There are many solutions, the choice of which depends on what your ultimate goal is. If you want the canvas to be forced to a height of 800x700 regardless of the window size or the size of its contents, you can turn geometry propagation off inside the canvas. For example:
w.pack_propagate(False)
You could also pack the canvas to fill the space given to it and then anchor the background image to the upper-left corner. For example:
w.pack(fill="both", expand=True)
w.create_image(0, 0, image=image1, anchor="nw")
You could also stop using a canvas, and put the background image in a label. You can then use place
to center the label in the main window. For example:
background_label = Label(root, image=image1)
background_label.place(relx=.5, rely=.5)
Upvotes: 2