-2

I have written a function, which needs two variables (start, end). Now I'm trying to build a GUI around this function. I want to set two variables with an entry widget and a button which starts the function.

start = StringVar()
end = StringVar()

Label(root, text="Begin").grid(row=0)
Label(root, text="End").grid(row=1)
e1 = Entry(root, textvariable=start)
e2 = Entry(root, textvariable=end)
e1.grid(row=0, column=1)
e2.grid(row=1, column=1)

b1 = Button(root, text="Test_Button", command=getshift())
b1.grid(row=2, column=1)

When I run the code, the error is always that start and end are not defined.

Patrick
  • 139
  • 1
  • 1
  • 12
  • This `, command=getshift()` is the culprit. Read [The Tkinter Button Widget](http://effbot.org/tkinterbook/button.htm) how to set `command=` parameter. **Note** the `()` – stovfl Dec 23 '18 at 15:01

1 Answers1

0

@Peet When I run the code, the error is always that start and end are not defined.

There is no error in your script. You make a mistake. Your getshift function executes while creation of your button(b1). That means your getshift is invoked before you are able to set your two variables.

Solution: Delete the parentheses after getshift.

b1 = Button(root, text="Test_Button", command=getshift)

If you need pass arguments to the function so use lamdas. Check stovfl suggestion!