[Tutor] Syntax question

Alan Gauld alan.gauld at yahoo.co.uk
Thu Aug 9 04:38:22 EDT 2018


On 09/08/18 05:10, Matthew Polack wrote:

> I'm trying to configure a button that prints a variable and calls a
> function at the same time...but I can't figure out how to get the syntax
> right...or if this is even possible:
Of couse its possible just wrap it in a higher level function:

def newfunc():
   print(variable)
   callFunction()

> print("You have selected to convert "), croptype

The crop type needs to be inside the parens
if you want it printed. That's the syntax error.

If you are trying to call a function called croptype
then you need to put it on a separate line and lose the ,
and add parens.


> code for this part says;
> 
> def wheatwords():
> print("You have selected to convert "), croptype
> price = int(float(input("What is the current price?")))
> amount = int(input("\n How much grain in metric tonnes?"))
> print("This is")
> print(price * amount, "Metric Tonnes")
>

I'll assume the lack of indentation is an email formatting
issue. Did you post in plain text?


> wheatbutton = Button(root, text="Wheat", fg="black", bg="yellow")
> wheatbutton.configure(croptype="wheat", command=wheatwords)

You set croptype to "wheat" but croptype is not an attribute
of Button. You can't just add arbitrary attributes you need
to use the ones that the object supports. You can see them
by using help(). For example:

>>> help(tk.Button)

Help on class Button in module tkinter:

class Button(Widget)
 |  Button widget.
 |
 |  Method resolution order:
 |      Button
 |      Widget
 |      BaseWidget
 |      Misc
 |      Pack
 |      Place
 |      Grid
 |      builtins.object
 |
 |  Methods defined here:
 |
 |  __init__(self, master=None, cnf={}, **kw)
 |      Construct a button widget with the parent MASTER.
 |
 |      STANDARD OPTIONS
 |
 |          activebackground, activeforeground, anchor,
 |          background, bitmap, borderwidth, cursor,
 |          disabledforeground, font, foreground
 |          highlightbackground, highlightcolor,
 |          highlightthickness, image, justify,
 |          padx, pady, relief, repeatdelay,
 |          repeatinterval, takefocus, text,
 |          textvariable, underline, wraplength
 |
 |      WIDGET-SPECIFIC OPTIONS
 |
 |          command, compound, default, height,
 |          overrelief, state, width
 |  <SNIP>

Shows the valid options available.


-- 
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




More information about the Tutor mailing list