-1

Trying my first foray into Python and TkInter, and have already gotten some great help here. However, I'm hitting another wall and hoping somebody here can give me a lift to the other side.

My app is pretty simple, conceptually. Just a page with 10-20 rows of labels next to text boxes and option menus (with about 25 items, all identical).

All I want to do is add SUBMIT and QUIT buttons. But, as with most things I'm trying, I get errors every step of they way.

SubmitButton = Button(root, text="Submit", command=greetings)
SubmitButton.place(x=700, y=600)
def greetings():
    print("Hello there")

The error (on this particular example) is

NameError: name 'greetings' is not defined

But isn't that what def greetings() does?

Most of the examples I'm seeing refer to things like self/master/frame, but I don't have any of that stuff in my script (at least not explicitly).

Isn't there some easy way to create a custom function and call that function on the click event? Or do I have to go back to the drawing board and try to understand how to use classes and such?

I'm using Python 3.6 and Spyder if that makes any difference.

Thank you for any guidance.

Maxim
  • 52,561
  • 27
  • 155
  • 209
JP_Romano
  • 61
  • 2
  • 9
  • You have to define a function before you can reference it. – Bryan Oakley Sep 22 '17 at 02:02
  • Thanks I moved the def greetings(): lines to the top. I no longer get an error message, but it seems that nothing is happening. Console is blank when I click the SUBMIT button. – JP_Romano Sep 22 '17 at 02:07

1 Answers1

0

I'm not sure why it's not printing to the console on button push and without seeing your updated snippet I wouldn't be able to tell.

However, the below snippet does work for me and should demonstrate how to handle custom functions on buttons:

from tkinter import *

root = Tk()

def greetings():
    print("Hello there")

SubmitButton = Button(root, text="Submit", command=greetings)

SubmitButton.pack()

Also, to answer your question. No, you don't need to use classes or any form of object orientation. However, when it comes to tkinter (and yes this is heavily opinion based) it is often easier to use classes, there are countless reasons why this is and there are plenty of questions and answers on and off this site as to why this is.

This answer probably has the best explanation as to why this is.

Ethan Field
  • 4,646
  • 3
  • 22
  • 43
  • Thank you so much - at the top of some of the other code I've glued together, I have root = tk.Tk() Of course, I misunderstood what that was for, but when I removed root=Tk() from your code above, it worked. Guess I have more reading to do over the next few days - really not following how much of this stuff operates. In any event, than you again for your time and help. – JP_Romano Sep 22 '17 at 11:52
  • `Tk()` is the definition for the window (so is `tk.Tk()` it's just a slightly different way of declaring it) so when we say `root = Tk()` we're saying the variable `root` contains the window we're looking at. And that's fine, we all start somewhere. I have an email address on my profile, if you need help with something and I can help, feel free to email me. Bear in mind I'll reply when I'm free and able to though. – Ethan Field Sep 22 '17 at 12:14