Structure
from tkinter import *
from PIL import Image, ImageTk
class Window(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.master = master
self.init_window()
# Creation of init_window
def init_window(self):
# changing the title of ourmaster get_tk_widget
self.master.title("GUI")
# allowing the widget to take the full space of the root window
self.pack(fill=BOTH, expand=1)
# creating a menu instance
menu = Menu(self.master)
self.master.config(menu=menu)
# create the file object
file = Menu(menu)
# adds a command to the menu option, calling it exit, and the command it runs on event is client_exit
file.add_command(label="Exit", command=self.client_exit)
# added "file" to our menu
menu.add_cascade(label="File", menu=file)
# create the file object
edit = Menu(menu)
# adds a command to the menu option, calling it exit, and the command it runs on event is client_exit
edit.add_command(label="Show Img", command=self.showImg)
edit.add_command(label="Show Text", command=self.showText)
# added "file" to our menu
menu.add_cascade(label="Edit", menu=edit)
# view menu
view = Menu(menu)
view.add_command(label="View", command=self.testprint("print test view"))
menu.add_cascade(label="View", menu=view)
# creating a button instance
quitButton = Button(self, text="Quit", command = self.client_exit)
# placing the button on my window
quitButton.place(x=0, y=0)
def showImg(self):
load = Image.open("chat.png")
render = ImageTk.PhotoImage(load)
# labels can be text or iamges
img = Label(self, image=render)
img.image = render
img.place(x=0, y=0)
def showText(self):
text = Label(self, text="Hey there good lookin!")
text.pack()
def client_exit(self):
exit()
def testprint(self, str):
print(str)
root = Tk()
# size of the window
root.geometry("400x300")
app = Window(root)
root.mainloop()