0

Brief description of the application:

I have created an application form that is attempting to capture a "X" quantity of serial numbers and the Serial Number itself. If user enter a valid quantity, I enable the "Save SN" button.

The problem:

The problem I have, is that form is able to capture up to 120 SN's. so capturing the SN and clicking "Save SN" each time is getting annoying.

The question:

How to either, click the button to execute the def or Press "Return" (Enter) on the computer to execute the function?

** def enter(self) -- > To get the user input, validate and Save a valid quantity

** def validatesn(self) -- > To get the user input and validate the Serial Number format.

Please see below my code:

from tkinter import *
from tkinter import messagebox

sn_count = 0
class SerialForm ():
    def enter(self):
        global oven_qty
        oven_qty = self.qty.get()
        print(oven_qty)

    def validatesn(self):
        global sn_count, oven_qty
        new_sn = self.serial.get()
        print(new_sn)
        print (sn_count)

    def __init__(self, window):
        self.window = window
        window.geometry('390x320+400+200')
        window.resizable(False,False)
        window.title("App 1.30")
        LabelFrame(window, text = "  Serial Number Entry Form  : ", font = ("Tahoma",14), height=250, width=360, bd=2, relief='groove' ).place(x=15,y=0)
        Label(text='Quantity  :',font=("Tahoma", 12)).place(x=20,y=40)
        Label(text='Quantity Count   :',font=("Tahoma", 12)).place(x=20,y=80)
        LabelFrame(window, font = ("Tahoma",12), height=28, width = 145, bd=2, relief='ridge' ).place(x=160,y=80)
        Label(text='Serial N.  :',font=("Tahoma", 12)).place(x=20,y=120)
        self.qty = Entry(window, font=("Arial Narrow", 12))
        self.qty.place(x=160,y=40)
        self.qty.config(state = NORMAL)
        self.serial = Entry(window, font=("Arial Narrow", 12))
        self.serial.place(x=160,y=120)
        self.serial.config(state = DISABLED)
        Label(text='Last Serial N.      :',font=("Tahoma", 12)).place(x=20,y=160)
        LabelFrame(window, font = ("Tahoma",12), height=28, width = 145, bd=2, relief='ridge' ).place(x=160,y=160)
        self.window_btne = Button(window, text=" Enter Qty ", font=("Tahoma", 14), height=1, width=10, command=self.enter)
        self.window_btne.place(x = 20, y = 200)
        self.window_btne.config(state = NORMAL)
        self.window_btns = Button(window, text=" Save SN ", font=("Tahoma", 14), height=1, width=10, command = self.validatesn)
        self.window_btns.place(x = 140, y = 200)
        self.window_btns.config(state = DISABLED)

root = Tk()
my_gui = SerialForm(root)
root.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301
F Casian
  • 93
  • 1
  • 11
  • 1
    It would help if you could condense this code down to a [mcve]. If your question is about how to bind the return key to a function, we don't need much more than one or two input fields, the function, your attempt at binding to it, and enough additional code to make it work. – Bryan Oakley Jun 15 '20 at 16:58
  • @BryanOakley Hi Bryan, I posted the whole code so viewers can "copy & paste" my code and execute it for review purposes. Also, yes, I'm trying to bind the "return key" into a function but I also want the "button" it self too. Sorry if my code create confusion and thanks for your comment. – F Casian Jun 15 '20 at 17:02
  • 1
    If you want a code review you should post on the code review stackexchange site. For questions on Stackoverflow, a [mcve] tailored to the question is preferred. – Bryan Oakley Jun 15 '20 at 17:12
  • F Casian: Folks could just as easily copy & paste and simplified version of the code. Beside making your question clearer, it would likely also make the answer that way, as well. – martineau Jun 15 '20 at 17:38
  • 1
    @FCasian: Does this answer your question? [how-can-i-update-entry-without-a-submit-button-in-tkinter](https://stackoverflow.com/questions/39957699) or [Entry validation](https://stackoverflow.com/a/60904457/7414759) – stovfl Jun 15 '20 at 18:07
  • Hi @martineau I have edited my code making it a bit shorter. – F Casian Jun 15 '20 at 19:07

1 Answers1

2

You can do it by "binding" events, like keypresses, to the Entry widget. In this case the '<Return>' key, which is the name tkinter uses for the Enter key.

Event-handler callback function receive a event argument that contains information about what triggered them being called, which is often not needed, but still has to be declared when defining the function. Since you already have a command= callback function for when the Enter Qty Button is clicked, I've modified it to accept this optional extra argument so the same function can be used for both widgets.

Below is your code with the modifications needed to do this as indicated by # ALL CAP comments showing where they are (there weren't many). Note I also made overall changes to your code to make it conform closely to the PEP 8 coding style guidelines and be more readable.

from tkinter import *
from tkinter import messagebox

sn_count = 0


class SerialForm:
    def enter(self, *event):  # MODIFIED TO ACCEPT OPTIONAL event ARGUMENT
        global oven_qty
        oven_qty = self.qty.get()
        print(oven_qty)

    def validatesn(self):
        global sn_count, oven_qty
        new_sn = self.serial.get()
        print(new_sn)
        print(sn_count)

    def __init__(self, window):
        self.window = window
        window.geometry('390x320+400+200')
        window.resizable(False, False)
        window.title("App 1.30")
        LabelFrame(window, text="  Serial Number Entry Form  : ", font=("Tahoma", 14),
                   height=250, width=360, bd=2, relief='groove' ).place(x=15,y=0)
        Label(text='Quantity  :', font=("Tahoma", 12)).place(x=20,y=40)
        Label(text='Quantity Count   :', font=("Tahoma", 12)).place(x=20,y=80)
        LabelFrame(window, font=("Tahoma", 12), height=28, width=145, bd=2,
                   relief='ridge').place(x=160,y=80)
        Label(text='Serial N.  :',font=("Tahoma", 12)).place(x=20,y=120)
        self.qty = Entry(window, font=("Arial Narrow", 12))
        self.qty.place(x=160, y=40)
        self.qty.config(state=NORMAL)

        self.qty.bind('<Return>', self.enter)  # ADDED

        self.serial = Entry(window, font=("Arial Narrow", 12))
        self.serial.place(x=160, y=120)
        self.serial.config(state=DISABLED)
        Label(text='Last Serial N.      :',font=("Tahoma", 12)).place(x=20,y=160)
        LabelFrame(window, font=("Tahoma", 12), height=28, width=145, bd=2,
                                 relief='ridge' ).place(x=160, y=160)
        self.window_btne = Button(window, text=" Enter Qty ", font=("Tahoma", 14),
                                  height=1, width=10, command=self.enter)
        self.window_btne.place(x=20, y=200)
        self.window_btne.config(state=NORMAL)
        self.window_btns = Button(window, text=" Save SN ", font=("Tahoma", 14),
                                  height=1, width=10, command=self.validatesn)
        self.window_btns.place(x=140, y=200)
        self.window_btns.config(state=DISABLED)


root = Tk()
my_gui = SerialForm(root)
root.mainloop()
martineau
  • 119,623
  • 25
  • 170
  • 301
  • Thank you so much Martin, my code is working as I need, I added a few lines to make the other buttons and entries work the same, I really appreciated your help on this, i was getting crazy trying to find the answer to my problem, but now, it works ... thanks to you .... – F Casian Jun 15 '20 at 22:18
  • That's good to hear…and you're welcome. BTW, here's some `tkinter` [documentation](https://web.archive.org/web/20190524140835id_/http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html) I found you might find helpful. It's a little outdated, but I still use it. – martineau Jun 15 '20 at 22:22
  • Thank you @martineau I'll take a look at it, please let me know if you want me to share my code (Full one) more than happy to collaborate.... – F Casian Jun 15 '20 at 22:24
  • Thanks for the offer, but I've got my own stuff to work on at the moment. Good luck with your project! – martineau Jun 15 '20 at 22:29