I have 2 lines in the calculator with data entry and 1 with output. How to make it so that when you press Enter, it would take me to line 2, and then to line 3? Here is my code:

from tkinter import * from tkinter.messagebox import * import math def showSum(): Ans1 = int(num1.get()) + int(num2.get()) str_ans1 = str(Ans1) blank.delete(0, END) blank.insert(0, Ans1) def showSubs(): Ans2 = int(num1.get()) - int(num2.get()) str_ans2 = str(Ans2) blank.delete(0, END) blank.insert(0, Ans2) def showMult(): Ans3 = int(num1.get()) * int(num2.get()) str_ans3 = str(Ans3) blank.delete(0, END) blank.insert(0, Ans3) def showDiv(): Ans4 = int(num1.get()) / int(num2.get()) str_ans4 = str(Ans4) blank.delete(0, END) blank.insert(0, Ans4) def sq(): Ans5 = int(num1.get())**2 str_ans5 = str(Ans5) blank.delete(0, END) blank.insert(0, Ans5) def cube(): Ans6 = int(num1.get())**3 str_ans6 = str(Ans6) blank.delete(0, END) blank.insert(0, Ans6) def clear(): blank.delete(0, END) num1.delete(0, END) num2.delete(0, END) main = Tk() main.title('Calculator') main.geometry('340x200') Label1 = Label(main, text = "Number 1", bg = 'white', fg = 'black').grid(row=0, sticky = W) Label(main, text = "Number 2", bg = 'white', fg = 'black').grid(row=1, sticky = W) Label(main, text = "Answer", bg = 'white', fg = 'black').grid(row=2, sticky = W) num1 = Entry(main, bg = 'white') num2 = Entry(main, bg = 'white') blank = Entry(main, bg = 'white') num1.grid(row=0, column=1) num2.grid(row=1, column=1) blank.grid(row=2, column=1) Button0 = Button(main, text='Quit', bg = 'white', fg = 'red', width=8, height=3, command=main.destroy) Button0.place(x = 270, y = 140) Button1 = Button(main, text = 'Clear', bg = 'white', fg = 'Red', width=8, height=3, command = clear) Button1.place(x = 180, y = 140) Button2 = Button(main, text='Plus', bg = 'white', fg = 'green', width=8, height=3, command=showSum) Button2.place(x = 1, y = 70) Button3 = Button(main, text = 'Minus', bg = 'white', fg = 'green', width=8, height=3, command=showSubs) Button3.place(x = 90, y = 70) Button4 = Button(main, text = 'Multiply', bg = 'white', fg = 'green', width=8, height=3, command = showMult) Button4.place(x = 180, y = 70) Button5 = Button(main, text = 'Divide', bg = 'white', fg = 'green', width=8, height=3, command = showDiv) Button5.place(x = 270, y = 70) Button6 = Button(main, text = 'Square', bg = 'white', fg = 'green', width=8, height=3, command = sq) Button6.place(x = 1, y = 140) Button7 = Button(main, text = 'Cube', bg = 'white', fg = 'green', width=8, height=3, command = cube) Button7.place(x = 90, y = 140) main.maxsize(340,200) main.minsize(340,200) main.configure(background = 'white') main.mainloop() 

1 answer 1

Using the bind() function, you can assign a key when you click on it to perform a specific action in this case, focus()

 from tkinter import Tk, Entry def select(event): if len(edit_1.get()) > 0: edit_2.focus() if len(edit_2.get()) > 0: edit_3.focus() root = Tk() edit_1 = Entry(root) edit_1.bind("<Return>", select) edit_1.grid() edit_2 = Entry(root) edit_2.bind("<Return>", select) edit_2.grid() edit_3 = Entry(root) edit_3.grid() root.mainloop() 
  • Thanks for the help! - Madiyar