J. Doe
J. Doe

Reputation: 39

Use return key and a button at the same time in tkinter

I want to write a program for my biology class... I want to integrate the function that you can type something in the Entry bar and then you can use the button or click the return key. I've the problem, that I just can click the button. Everything else don't work. Here is my code (in a simple form):

from tkinter import *
import tkinter as tk

# Main Graphic User Interface
root = Tk()
root.title("Genetic Translator")
root.geometry("300x175")
root.resizable(0,0)

# Solid Label "Information for Input"
s_label2 = Label(root, text = "\nInput Tripplet which decodes for an amino acid:\n")
s_label2.pack()

# Entry Bar
trip = Entry(root)
trip.pack()
# Function for setting focus on entry bar
trip.focus_set()

# Dictionary
output = {"GCX":"Alanine [Ala]"}

# Dict Function Function (Trans:    trip -in- AS)
def dict_function1():
    global o_screen
    o_screen.configure(text=(output.get(trip.get().upper(),"Unknown tripplet!")))

# Bind the Return Key for Input
trip.bind("<Return>", dict_function1)

# Space Label 1
space_label1 = Label(root)
space_label1.pack()

# Button "Confirm"
mainbutton = Button(root, text = "Confirm", command = dict_function1)
mainbutton.pack()

# Space Label 2
space_label2 = Label(root)
space_label2.pack()

# Output Screen
o_screen = Label(root)
o_screen.pack()

# Mainloop function for Interface Options
root.mainloop()

Thank you for helping me.

Upvotes: 0

Views: 902

Answers (4)

user4171906
user4171906

Reputation:

It works for me, except for the error message when the Enter key is pressed which you don't provide, so that may or may not be the problem. It is easily fixed, but "everything else don't work" is way too vague to help you with. See "Capturing keyboard events" at http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm You should also include code if the Entry value is not found in the dictionary. Finally, you import Tkinter twice in the first 2 statements in the program. Choose one or the other.

from tkinter import *

# Main Graphic User Interface
root = Tk()
root.title("Genetic Translator")
root.geometry("300x175")
root.resizable(0,0)

# Solid Label "Information for Input"
s_label2 = Label(root, text = "\nInput Tripplet which decodes for an amino acid:\n")
s_label2.pack()

# Entry Bar
trip = Entry(root)
trip.pack()
# Function for setting focus on entry bar
trip.focus_set()

# Dictionary
output = {"GCX":"Alanine [Ala]"}

# Dict Function Function (Trans:    trip -in- AS)
def dict_function1(arg=None): ## capture the event from the Return key
    ##global o_screen
    o_screen.configure(text=(output.get(trip.get().upper(),"Unknown tripplet!")))

# Bind the Return Key for Input
trip.bind("<Return>", dict_function1)

# Space Label 1
space_label1 = Label(root)
space_label1.pack()

# Button "Confirm"
mainbutton = Button(root, text = "Confirm", command = dict_function1)
mainbutton.pack()

# Space Label 2
space_label2 = Label(root)
space_label2.pack()

# Output Screen
o_screen = Label(root)
o_screen.pack()

# Mainloop function for Interface Options
root.mainloop()

Upvotes: 0

Terry Jan Reedy
Terry Jan Reedy

Reputation: 19144

You neglected to say what "don't work" means. When I run your code from IDLE, enter 3 letters, and hit return, I get the following

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Programs\Python35\lib\tkinter\__init__.py", line 1549, in __call__
    return self.func(*args)
TypeError: dict_function1() takes 0 positional arguments but 1 was given

The issue is that when tk calls a 'command', it does not pass any arguments, but when it calls a function bound to an event, it passes an event argument. So add an optional parameter to the function.

def dict_function1(event=None):

Upvotes: 1

furas
furas

Reputation: 142631

Function assigned to button is called without arguments but assigned by bind is called with argument - event information - so your function have to receive that argument

 def dict_function1(event=None): # None for "command="

--

<Return> binded to Entry will work only if Entry is focused, but not when Button is focused. If you bind <Return> to root then <Return> will work in both situations.

Upvotes: 2

Kenly
Kenly

Reputation: 26688

When you press return key it will send event as argument to dict_function1 and when you click on the button nothing is send. add argument to dict_function1 with None as default value.

def dict_function1(event=None)

Upvotes: 2

Related Questions