Matthew Polack wrote: > Hi, > > Steadily trying to learn Python and Tkinter here with our students... > > Am working on a simple 'Price of Cereal Crop' calculator....and have made > a start with the code below... > > I'm trying to simply add an image to our program to make the GUI more > interesting...but most of the tutorials describe using the 'Pack' > method.... not the grid method of layout...
Open the image with PIL, and then put the image into a Label or Button. What layout manager you use for that widget afterwords doesn't really matter. > and apparently you cannot use 'Pack' and 'Grid' in the one program... > > Can anyone explain how I could add an image to this program? Or show me an > example of a simple program that lets someone simply add an image and use > the grid layout in combination. Let's assume you want to show a picture stored in the file "wheat.jpg" on the button with the -- misleading! -- name wheatlabel. Then > from tkinter import * from PIL import Image, ImageTk > root = Tk("Crop Calculator") # The line above produced an exception over here; I think you wanted root = Tk() root.title("Crop Calculator") > # abutton = Button(root, text="Crop Prices in Australia") > > #Main Heading Labels > croplabel = Label(root, text="Crop Prices in Australia", fg="white", bg= > "green") > instruction = Label(root, text="Please select from the following crops", > fg= "green", bg="white") > blanklabel = Label(root, text="", fg="green", bg="white") > blanklabel2 = Label(root, text="", fg="green", bg="white") > statusbar = Label(root, text="Copyright 2018", fg="white", bg="black", > relief='sunken') > > #Answerlabel > answerlabel=Label(root, text="wheatwords") > > # abutton.pack(side='left', fill='both', expand=True) > croplabel.grid(columnspan=12, sticky='ew') > instruction.grid(row=1, columnspan=12, sticky='ew') > blanklabel.grid(row=2, columnspan=12, sticky='ew') > statusbar.grid(row=12, columnspan=12, sticky='ew') > blanklabel2.grid(row=11, columnspan=12, sticky='ew') > > > #List of Crops wheat_image = Image.open("wheat.jpg") wheat_photo = ImageTk.PhotoImage(wheat_image) wheatlabel = Button(root, image=wheat_photo, fg="black", bg="yellow") > peaslabel = Button(root, text="Peas", fg="white", bg="green") > lupenslabel = Button(root, text="Lupens", fg="white", bg="brown") > barleylabel = Button(root, text="Barley", fg="white", bg="orange") > canolalabel = Button(root, text="Canola", fg="white", bg="red") > sorghumlabel = Button(root, text="Sorghum", fg="white", bg="ivory3") > > # Grid positioning of crops. > wheatlabel.grid(row=4, column=1, sticky='ew') > peaslabel.grid(row=4, column=7, sticky='ew') > lupenslabel.grid(row=5, column=1, sticky='ew') > barleylabel.grid(row=5, column=7, sticky='ew') > canolalabel.grid(row=6, column=1, sticky='ew') > sorghumlabel.grid(row=6, column=7, sticky='ew') > > # Definitions > > def wheatwords(): > print("Button one was pushed") > > wheatlabel.configure(command=wheatwords) > > > root.mainloop() > _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor