在tkinter中同时使用返回键和一个按钮

时间:2016-01-01 20:58:31

标签: python-3.x tkinter

我想为我的生物课编写一个程序...我想整合你可以在条目栏中输入内容的功能,然后你可以使用按钮或点击返回键。我有问题,我只需点击按钮即可。其他一切都不起作用。这是我的代码(以简单的形式):

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()

感谢您帮助我。

4 个答案:

答案 0 :(得分:3)

当您按return key时,它会将事件作为参数发送到dict_function1,当您点击按钮时,不会发送任何内容。 将参数添加到dict_function1并将None作为默认值。

def dict_function1(event=None)

答案 1 :(得分:2)

分配给按钮的函数在没有参数的情况下调用,但是通过参数调用bind来调用 - 事件信息 - 因此您的函数必须接收该参数

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

-

绑定到<Return>

Entry仅在Entry得到关注时有效,但在Button关注时则无效。如果您将<Return>绑定到root,那么<Return>将适用于这两种情况。

答案 2 :(得分:1)

你忽略了说什么&#34;没有工作&#34;手段。当我从IDLE运行你的代码时,输​​入3个字母,点击return,我得到以下

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

问题在于,当tk调用&#39;命令时,它不会传递任何参数,但是当它调用绑定到事件的函数时,它会传递一个事件参数。因此,在函数中添加一个可选参数。

def dict_function1(event=None):

答案 3 :(得分:0)

除了按下您不提供的Enter键时的错误消息,它对我有用,因此可能是也可能不是问题。它很容易修复,但是其他一切都不能正常工作&#34;是太模糊,无法帮助你。请参阅&#34;捕获键盘事件&#34;在http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm如果在词典中找不到Entry值,您还应该包含代码。最后,在程序的前两个语句中导入Tkinter两次。选择其中一个。

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()