Python Forum
[Tkinter] Activating buton using "Enter" key
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Activating buton using "Enter" key
#1
Hello everybody!
I am a beginner in python. I start to access the basics...
Thank you for this very useful site and for the time you spend to answer beginners like me :-)

I created a program* in which (without setting anything) the "tab" key lets move from one field to another. When the focus comes on the button "Validar", I would like to only press "Enter" key to press the button, but it does not work... I have to "Click" with the mouse...

How to do that?

Thank you !

* It can only multiply two numbers for the moment... Blush

Here is te code:

#!/usr/bin/python3.7
# -*- coding: utf-8 -*-

# Premières lignes = shebang : à étudier !!!
# http://www.chicoree.fr/w/Rendre_un_script_ex%C3%A9cutable

# Utilisation de la méthode "place()" pour organiser l'interface
# http://www.xavierdupre.fr/app/teachpyx/helpsphinx/c_gui/tkinter.html#disposition-des-objets-dans-une-fenetre

from tkinter import *

# Création de la fenêtre
fenetre = Tk()

# Définir la taille de la fenêtre
fenetre.geometry("240x165")

fenetre.title("¡Laurent Multiplica!")

# Création de la fonction qui sera appelée par le bouton
def valider():
    a1 = entry_a1.get()
    print ("Su numero a1 es: "+ a1)
    a2 = entry_a2.get()
    print ("Su numero a1 es: "+ a2)
    c = int(a1)*int(a2)
    print ("a1 x a2 vale:", c)
    # Création d'une zone de texte de type "Label"
    resultat = Label(fenetre, text="  a1 x a2 = " + str(c) + "  ",  bg='#6EC86E', font = ('Helvetica', '13'))
    resultat.place(x=100, y=80)

# Création d'une zone de texte de type "Label" Pour le titre
titre = Label(fenetre, text="  Pequeña aplicación  ", bg='white', font = ('Helvetica', '16') )
titre.pack()
# Création d'une zone de texte de type "Label" Pour le titre
sous_titre = Label(fenetre, text="Cálculo de a1 x a2", bg='white', font = ('Helvetica', '13') )
sous_titre.pack()

# Création d'une zone de texte de type "Label" pour a1
label_a1 = Label(fenetre, text="a1 :")
label_a1.place(x=10, y=60)

# Création d'un champ de saisie de type "Entry" pour a1
entry_a1 = Entry(fenetre, width=5)
# entry_a1.insert(0, "a1 ?")
entry_a1.place(x=40, y=60)

# Création d'une zone de texte de type "Label" pour a2
label_a2 = Label(fenetre, text="a2 :")
label_a2.place(x=10, y=90)

# Création d'un champ de saisie de type "Entry" pour a2
entry_a2 = Entry(fenetre, width=5)
# entry_a2.insert(0, "a2 ?")
entry_a2.place(x=40, y=90)


# Création d'un Button
monBouton = Button(fenetre, text="Validar", command=valider)
monBouton.place(x=10, y=120)  # Affichage du Button dans la fenêtre
Reply
#2
The spacebar automatically works to activate the button.

If you want Enter (Return) to activate it, bind it to the same validation function:
monBouton.bind("<Return>", valider)
This doesn't quite work, because bind() passes a variable, whereas Button.command doesn't, so you need to change the definition of valider to accept an optional parameter, and then ignore it:

def valider(event=None):
I tried this just now and it worked for me. So thank you: I hadn't done this before, and it's useful.
Reply
#3
Thank you

I will try this.
Reply
#4
Hi

I tried this code, but there is no change... :-(

#!/usr/bin/python3.7
# -*- coding: utf-8 -*-

# Premières lignes = shebang : à étudier !!!
# http://www.chicoree.fr/w/Rendre_un_script_ex%C3%A9cutable

# Utilisation de la méthode "place()" pour organiser l'interface
# http://www.xavierdupre.fr/app/teachpyx/helpsphinx/c_gui/tkinter.html#disposition-des-objets-dans-une-fenetre

from tkinter import *

# Création de la fenêtre
fenetre = Tk()

# Définir la taille de la fenêtre
fenetre.geometry("240x165")

fenetre.title("  ¡Laurent Multiplica!")

# Création de la fonction qui sera appelée par le bouton
def valider(event=None):
    a1 = entry_a1.get()
    print ("Su numero a1 es: "+ a1)
    a2 = entry_a2.get()
    print ("Su numero a1 es: "+ a2)
    c = int(a1)*int(a2)
    print ("a1 x a2 vale:", c)
    # Création d'une zone de texte de type "Label"
    resultat = Label(fenetre, text="  a1 x a2 = " + str(c) + "  ",  bg='#6EC86E', font = ('Helvetica', '13'))
    resultat.place(x=100, y=80)

# Création d'une zone de texte de type "Label" Pour le titre
titre = Label(fenetre, text="  Pequeña aplicación  ", bg='white', font = ('Helvetica', '16') )
titre.pack()
# Création d'une zone de texte de type "Label" Pour le titre
sous_titre = Label(fenetre, text="Cálculo de a1 x a2", bg='white', font = ('Helvetica', '13') )
sous_titre.pack()

# Création d'une zone de texte de type "Label" pour a1
label_a1 = Label(fenetre, text="a1 :")
label_a1.place(x=10, y=60)

# Création d'un champ de saisie de type "Entry" pour a1
entry_a1 = Entry(fenetre, width=5)
# entry_a1.insert(0, "a1 ?")
entry_a1.place(x=40, y=60)

# Création d'une zone de texte de type "Label" pour a2
label_a2 = Label(fenetre, text="a2 :")
label_a2.place(x=10, y=90)

# Création d'un champ de saisie de type "Entry" pour a2
entry_a2 = Entry(fenetre, width=5)
# entry_a2.insert(0, "a2 ?")
entry_a2.place(x=40, y=90)


# Création d'un Button
monBouton = Button(fenetre, text="Validar", command=valider)
monBouton.place(x=10, y=120)  # Affichage du Button dans la fenêtre
monBouton.bind("<Return>", valider)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] QTimer not activating function after timeout LavaCreeperKing 0 3,843 Apr-03-2017, 09:09 PM
Last Post: LavaCreeperKing

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020