[Tutor] How to add an Image in Grid mode with Python and Tkinter?
Matthew Polack
matthew.polack at htlc.vic.edu.au
Mon Jul 30 03:24:27 EDT 2018
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...
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.
Thanks for any clues at all!
- Matt
from tkinter import *
root = Tk("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
wheatlabel = Button(root, text="Wheat", 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()
--
**Disclaimer: *Whilst every attempt has been made to ensure that material
contained in this email is free from computer viruses or other defects, the
attached files are provided, and may only be used, on the basis that the
user assumes all responsibility for use of the material transmitted. This
email is intended only for the use of the individual or entity named above
and may contain information that is confidential and privileged. If you are
not the intended recipient, please note that any dissemination,
distribution or copying of this email is strictly prohibited. If you have
received this email in error, please notify us immediately by return email
or telephone +61 3 5382 2529** and destroy the original message.*
More information about the Tutor
mailing list