[Tkinter-discuss] how do I pass variables when binding to a widget?

Chris Roy-Smith chris_roysmith at internode.on.net
Wed Jan 9 23:13:12 EST 2019


Hi
System: 	Linux,
Python Version:	3.6.7

I am trying to learn how to bind to a widget, and process the contents 
of the bound widget

here is my attempt at code:

#! /usr/bin/python3
from tkinter import *
import datetime

def NewAge(start):
     print(start)
     futureAge = start + 5
     calculated.delete(0,END)
     calculated.insert(0,futureAge)


master=Tk()
Label(master, text='present age').grid(row=0, column=0)
Label(master, text='age in 5 years').grid(row=1, column=0)
first=Entry(master)
first.grid(row=0, column=1)
calculated = Entry(master)
calculated.grid(row=1, column=1)
first.bind("<Return>", NewAge(first.get()))

Button(master, text='exit', command=master.destroy).grid(row=3, column=0)

master.mainloop()

I get the following traceback:

Traceback (most recent call last):
   File "./bind.py", line 19, in <module>
     first.bind("<Return>", NewAge(first.get()))
   File "./bind.py", line 7, in NewAge
     futureAge = start + 5
TypeError: must be str, not int

All hints & help greatly appreciated.


regards,
Chris Roy-Smith



More information about the Tkinter-discuss mailing list