On 01/08/18 14:17, Matthew Polack wrote: > > c:\Python>python crops3.py > Traceback (most recent call last): > File "crops3.py", line 30, in <module> > filemenu.add_command(label="Show", command=self.showImg) > NameError: name 'self' is not defined
The problem with cutting and pasting/copying code... self is the name used for the first parameter in a class method definition. But you have not defined any classes so self does not make sense. In fact once you get past this error you will likely run into others since self will have very strange values compared to the original author's intent. I suspect you might also run into other problems since you use from tkinter import * and from PIL import Image... But tkinter also has an Image class which your PIL one will hide. If you intend to use the tkinter Image rather than the PIL one you will be stuck. If you use the import tkinter as tk approach you will avoid the ambiguity. > > > I can't figure out why his works and mine doesn't.... > > Here is all my code so far...can either of you spot anything wrong? > > Thanks so much if you have a chance to look. OK, But I've also CC'd it to the list since its good to keep the discussion public so other people can benefit (and contribute) too. > > Matt > > > > fromtkinter import* > root =Tk("Crop Calculator") > root.title("Crop Calculator") > root.geometry("640x640+0+0") > fromPIL importImage, ImageTk > defdonothing(): > filewin =Toplevel(root) > button =Button(filewin, text="Do nothing button") > button.pack() > defshowImg(self): > load =Image.open('wheat.jpg') > render =ImageTk.PhotoImage(load) > img =Label(self, image=render) > img.image =render > img.place(x=0, y=0) > defShowTxt(self): > text =Label (self, text='Hey there') > text.pack() > menubar =Menu(root) > filemenu =Menu(menubar, tearoff=0) > filemenu.add_command(label="New", command=donothing) > filemenu.add_command(label="Show", command=self.showImg) > filemenu.add_command(label="Show Text", command=self.showTxt) > filemenu.add_command(label="Save", command=donothing) > filemenu.add_command(label="Save as...", command=donothing) > filemenu.add_command(label="Close", command=donothing) > filemenu.add_separator() > filemenu.add_command(label="Exit", command=root.quit) > menubar.add_cascade(label="File", menu=filemenu) > editmenu =Menu(menubar, tearoff=0) > editmenu.add_command(label="Undo", command=donothing) > editmenu.add_separator() > editmenu.add_command(label="Cut", command=donothing) > editmenu.add_command(label="Copy", command=donothing) > editmenu.add_command(label="Paste", command=donothing) > editmenu.add_command(label="Delete", command=donothing) > editmenu.add_command(label="Select All", command=donothing) > menubar.add_cascade(label="Edit", menu=editmenu) > helpmenu =Menu(menubar, tearoff=0) > helpmenu.add_command(label="Help Index", command=donothing) > helpmenu.add_command(label="About...", command=donothing) > menubar.add_cascade(label="Help", menu=helpmenu) > root.config(menu=menubar) > # abutton = Button(root, text="Crop Prices in Australia") > #Main Heading Labels > #Answerlabel > answerlabel=Label(root, text="wheatwords") > # abutton.pack(side='left', fill='both', expand=True) > ''' > instruction.grid(row=1, columnspan=12, sticky='ew') > blanklabel.grid(row=2, columnspan=12, sticky='ew') > blanklabel2.grid(row=11, columnspan=12, sticky='ew') > ''' > wheatcost =("$5.23") > peascost =("$3.23") > lupenscost =("$8.23") > barleycost =("$2.53") > canolacost =("$9.72") > sorghumcost =("$22.23") > #List of Crops > croplabel =Label(root, text="Crop Prices in Australia", fg="white", > bg="green", font=("arial", 36, "bold")) > instruction =Label(root, text="Please select from the following > crops", fg="green", bg="white", font=("arial", 18, "bold")) > wheatbutton =Button(root, text="Wheat", fg="black", bg="yellow") > wheatprice =Label(root, text=wheatcost, fg="white", bg="black") > peasbutton =Button(root, text="Peas", fg="white", bg="green") > peasprice =Label(root, text=peascost, fg="white", bg="black") > lupensbutton =Button(root, text="Lupens", fg="white", bg="brown") > lupensprice =Label(root, text=lupenscost, fg="white", bg="black") > barleybutton =Button(root, text="Barley", fg="white", bg="brown") > barleyprice =Label(root, text=barleycost, fg="white", bg="black") > canolabutton =Button(root, text="Canola", fg="white", bg="red") > canolaprice =Label(root, text=canolacost, fg="white", bg="black") > sorghumbutton =Button(root, text="Sorghum", fg="black", bg="ivory3") > sorghumprice =Label(root, text=sorghumcost, fg="white", bg="black") > blankline =Label(root, text="", bg="white") > label_1 =Label(root, text="Type of Crop", fg="white", bg="black") > label_2 =Label(root, text="Number of Metric Tonnes", fg="white", > bg="black") > entry1 =Entry(root) > entry2 =Entry(root) > blankline2 =Label(root, text="", bg="white") > statusbar =Label(root, text="Copyright 2018", fg="white", bg="black", > relief='sunken') > wheat_image =Image.open("wheat.jpg") > wheat_photo =ImageTk.PhotoImage(wheat_image) > wheatlabel =Button(root, image=wheat_photo, fg="black", bg="yellow") > # Grid positioning of crops. > croplabel.grid(columnspan=12, sticky='ew') > wheatbutton.grid(row=4, column=1, sticky='ew') > wheatprice.grid(row=4, column=2, sticky='ew') > lupensbutton.grid(row=6, column=1, sticky='ew') > lupensprice.grid(row=6, column=2, sticky='ew') > peasbutton.grid(row=4, column=7, sticky='ew') > peasprice.grid(row=4, column=8, sticky='ew') > barleybutton.grid(row=6, column=7, sticky='ew') > barleyprice.grid(row=6, column=8, sticky='ew') > canolabutton.grid(row=8, column=1, sticky='ew') > canolaprice.grid(row=8, column=2, sticky='ew') > sorghumbutton.grid(row=8, column=7, sticky='ew') > sorghumprice.grid(row=8, column=8, sticky='ew') > blankline.grid(row=11, sticky='ew') > label_1.grid(column=1, row=12, sticky=E) > label_2.grid(column=1, row=14, sticky=E) > entry1.grid(column=7, row=12, sticky=E) > entry2.grid(column=7, row=14, sticky=E) > blankline2.grid(row=15, sticky='ew') > statusbar.grid(row=17, columnspan=12, sticky='ew') > name=StringVar() > entry_box =Entry(root, textvariable=name, width=25, bg="Lightblue") > graintype =("porridge") > # Definitions > defrequest(): > price =int(input("What is the current price?")) > amount =int(input("\nHow much grain?")) > defwheatwords(): > print("You have selected to convert "+graintype) > price =int(float(input("What is the current price?"))) > amount =int(input("\nHow much grain in metric tonnes?")) > print("This is") > print(price *amount, "Metric Tonnes") > wheatbutton.configure(command=wheatwords) > defshow_entry_fields(): > print("First Name: %s\nLast Name: %s"%(e1.get(), e2.get())) > master =Tk() > Label(master, text="First Name").grid(row=0) > Label(master, text="Last Name").grid(row=1) > e1 =Entry(master) > e2 =Entry(master) > e1.grid(row=0, column=1) > e2.grid(row=1, column=1) > Button(master, text='Quit', command=master.quit).grid(row=3, column=0, > sticky=W, pady=4) > Button(master, text='Show', command=show_entry_fields).grid(row=3, > column=1, sticky=W, pady=4) > # NEW CODE FROM OTHER > defmy_calculator(amount_mt, price): > print(amount_mt *price) > #nothing happens > defmy_multiply(firstnum, secnum): > print(firstnum *secnum) > #nothing happens > root.mainloop() > > // -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor