Как задействовать Enter в калькуляторе?

241 просмотра
0
0 Комментариев

У меня в калькуляторе 2 строчки с вводом данных и 1 с выводом. Как сделать так, чтобы при нажатии Enter, он переносил меня на 2 строчку, а затем и на 3?
Вот мой код:

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 Ответы

Python Опубликовано 14.12.2018
0

При помощи функции bind() можно назначит клавишу при нажатии на которую будет выполняться определенное действие в данном случае 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()

Добавить комментарий
Напишите свой ответ на данный вопрос.
Scroll Up