Reputation: 13
I am pretty new to writing code and got stuck. Im using python with the tkinter Module. It's a very simple program that converts C to F. Now I added a GUI to it and it seemed to be working alright. The Conversion worked as it was supposed to. It displayed all the info in the correct boxes. Now off course it still needed a lot of details but the base was working. I then added the 'Enter' key to control a function and the program went haywire. It now keep on looping over and over as if im constantly hitting the Converter button or 'Enter'. I can't seemed to find where the loops is created nor how to terminate it correctly. I already tried putting a 'Break' after the else Statement.
import tkinter
window = tkinter.Tk()
value = tkinter.Label(window, text='Please enter C\u00b0:',
bd=2, justify='left')
value.grid(row=1, ipadx=7, ipady=2, sticky='e')
value_print = tkinter.Label(window, text='F\u00b0 is:', bd=2,
justify='left')
value_print.grid(row=2, ipadx=7, ipady=2, sticky='e')
output = tkinter.IntVar()
var = tkinter.IntVar()
c_input = tkinter.Entry(window, textvariable=var, bd=2,
justify='left')
c_input.grid(row=1, column=2, ipadx=7, ipady=2, sticky='w')
def converter(c):
f = (c*9/5+32)
if c < -273.15:
output.set('''Please enter a value over the lowest possible
temperature(-273.15) that physical matter can reach.''')
print(invalid)
else:
output.set(f)
print(f)
def print_con(event=None):
c = var.get()
print(converter(int(c)))
enter = tkinter.Button(window, text='Convert', command=print_con)
enter.grid(row=3, column=2, ipadx=7, ipady=2, sticky='w')
window.bind('<Enter>', print_con)
f_output = tkinter.Message(window, bd=2, textvariable=output,
justify='left')
f_output.grid(row=2, column=2, ipadx=7, ipady=2, sticky='w')
window.mainloop()
Upvotes: 1
Views: 95
Reputation: 386285
<Enter>
is triggered whenever the mouse enters or leaves a widget. If you want the return key you need to use <Return>
.
Upvotes: 0
Reputation: 26
The enter key is labeled as <Return>
in Tkinter.
So, just change this line of code:
window.bind('<Enter>', print_con)
Into this:
window.bind('<Return>', print_con)
Upvotes: 1