I've created a very basic word game that I want to create a basic gui for. I am testing a file that will put an image on a canvas and then place text boxes for the words over-top of the image.
I cannot seem to get it to use the same window. The text box shows up in a different window than the image. I've tried many various of how to write this code but cannot seem to figure it out. Any help would be greatly appreciated.
"""
Python Delete2.py
"""
from tkinter import *
from PIL import Image, ImageTk
class App(Frame):
def __init__(self, master):
Frame.__init__(self, master)
self.columnconfigure(0,weight=1)
self.rowconfigure(0,weight=1)
self.original = Image.open('687ee377f1820465b443950055671cb6.png')
self.image = ImageTk.PhotoImage(self.original)
self.display = Canvas(self, bd=0, highlightthickness=0)
self.display.create_image(0, 0, image=self.image, anchor=NW, tags="IMG")
self.display.grid(row=0, sticky=W+E+N+S)
self.pack(fill=BOTH, expand=1)
self.bind("<Configure>", self.resize)
def resize(self, event):
size = (event.width, event.height)
resized = self.original.resize(size,Image.ANTIALIAS)
self.image = ImageTk.PhotoImage(resized)
self.display.delete("IMG")
self.display.create_image(0, 0, image=self.image, anchor=NW, tags="IMG")
madlib = Tk()
Label(madlib, text="Please pick an Adjective.").grid(row=0)
e1 = Entry(madlib)
e1.grid(row=0, column=1)
root = Tk()
app = App(root)
app.mainloop()
root.destroy()
This
Tk()
Should only appear once in your code - it's the main window. You call it twice so you have two main windows. Even if you wanted a second window Toplevel
is what you should be using. I'd keep the root window in your class:
self.root = master
in __init__
. Then when you want to embed stuff in this window, then use it!
Label(self.root, text="Please pick an Adjective.").grid(row=0)
e1 = Entry(self.root)
If you want to embed in the specific frame (probably the better idea), just use self - which is already a frame embedded in the root:
Label(self, text="Please pick an Adjective.").grid(row=0)
e1 = Entry(self)
drop madlib
.