从Tkinter中的按钮在输入框中输入多个条目

从Tkinter中的按钮在输入框中输入多个条目,tkinter,calculator,tkinter-entry,Tkinter,Calculator,Tkinter Entry,我提供了一些代码,使用回调允许输入框从按钮获取值。问题是,如何将两个串联的值回调到输入框中。如果用户单击1,然后单击7,则显示值17 from tkinter import * win = Tk() win.geometry("210x125") def set_text(text): e.insert(0,text) return e = Entry(win,width=35) e.grid(columnspan=4) seven_button = Button(win,

我提供了一些代码,使用回调允许输入框从按钮获取值。问题是,如何将两个串联的值回调到输入框中。如果用户单击1,然后单击7,则显示值17

from tkinter import *
win = Tk()
win.geometry("210x125")

def set_text(text):
    e.insert(0,text)
    return

e = Entry(win,width=35)
e.grid(columnspan=4)

seven_button = Button(win,width=6,text="7",command=lambda:set_text("7")).grid(row=1, column=0)
eight_button = Button(win,width=6,text="8",command=lambda:set_text("8")).grid(row=1, column=1)
nine_button = Button(win,width=6,text="9",command=lambda:set_text("9")).grid(row=1, column=2)
div_button = Button(win,width=6,text="÷",command=lambda:set_text("÷")).grid(row=1, column=3)

four_button = Button(win,width=6,text="4",command=lambda:set_text("4")).grid(row=2, column=0)
five_button = Button(win,width=6,text="5",command=lambda:set_text("5")).grid(row=2, column=1)
six_button = Button(win,width=6,text="6",command=lambda:set_text("6")).grid(row=2, column=2)
multiply_button = Button(win,width=6,text="x",command=lambda:set_text("x")).grid(row=2, column=3)

one_button = Button(win,width=6,text="1",command=lambda:set_text("1")).grid(row=3, column=0)
two_button = Button(win,width=6,text="2",command=lambda:set_text("2")).grid(row=3, column=1)
three_button = Button(win,width=6,text="3",command=lambda:set_text("3")).grid(row=3, column=2)
minus_button = Button(win,width=6,text="-",command=lambda:set_text("-")).grid(row=3, column=3)

zero_button = Button(win,width=14,text="0",command=lambda:set_text("0")).grid(columnspan=2)
point_button = Button(win,width=6,text=".",command=lambda:set_text(".")).grid(row=4, column=2)
plus_button = Button(win,width=6,text="+",command=lambda:set_text("+")).grid(row=4, column=3)

win.mainloop()

只需在末尾插入文本即可

def set_text(text):
    e.insert(END,text)
    return

如果您想要完整的功能,我可以建议添加一个equals按钮来计算entry小部件中的所有内容吗

import Tkinter as tk
win = tk.Tk()
win.geometry("300x300")


def compute(entry):
 string_to_compute = tk.StringVar()
 string_to_compute = entry.get()
 entry.delete(0, tk.END)
 if '+' in string_to_compute:
    l = string_to_compute.split('+')[0]
    r = string_to_compute.split('+')[1]
    entry.insert(0, float(l) + float(r))
 elif '-' in string_to_compute:
    l = string_to_compute.split('-')[0]
    r = string_to_compute.split('-')[1]
    entry.insert(0, float(l) - float(r))
 elif 'x' in string_to_compute:
    l = string_to_compute.split('x')[0]
    r = string_to_compute.split('x')[1]
    entry.insert(0, float(l) * float(r))
 elif '/' in string_to_compute:
    l = string_to_compute.split('/')[0]
    r = string_to_compute.split('/')[1]
    entry.insert(0, float(l) / float(r))


def set_text(text):
 e.insert(tk.END, text)
 return

e = tk.Entry(win, width=35)
e.grid(columnspan=4)

seven_button = tk.Button(win, width=6, text="7", command=lambda: set_text("7")).grid(row=1, column=0)
eight_button = tk.Button(win, width=6, text="8", command=lambda: set_text("8")).grid(row=1, column=1)
nine_button = tk.Button(win, width=6, text="9", command=lambda: set_text("9")).grid(row=1, column=2)
div_button = tk.Button(win, width=6, text="/", command=lambda: set_text("/")).grid(row=1, column=3)

four_button = tk.Button(win, width=6, text="4", command=lambda: set_text("4")).grid(row=2, column=0)
five_button = tk.Button(win, width=6, text="5", command=lambda: set_text("5")).grid(row=2, column=1)
six_button = tk.Button(win, width=6, text="6", command=lambda: set_text("6")).grid(row=2, column=2)
multiply_button = tk.Button(win, width=6, text="x", command=lambda: set_text("x")).grid(row=2, column=3)

one_button = tk.Button(win, width=6, text="1", command=lambda: set_text("1")).grid(row=3, column=0)
two_button = tk.Button(win, width=6, text="2", command=lambda: set_text("2")).grid(row=3, column=1)
three_button = tk.Button(win, width=6, text="3", command=lambda: set_text("3")).grid(row=3, column=2)
minus_button = tk.Button(win, width=6, text="-", command=lambda: set_text("-")).grid(row=3, column=3)

zero_button = tk.Button(win, width=14, text="0", command=lambda: set_text("0")).grid(columnspan=2)
point_button = tk.Button(win, width=6, text=".", command=lambda: set_text(".")).grid(row=4, column=2)
plus_button = tk.Button(win, width=6, text="+", command=lambda: set_text("+")).grid(row=4, column=3)
equal_button = tk.Button(win, width=6, text="=", command=lambda: compute(e)).grid(row=5, column=1)


win.mainloop()

注:我更改了除号和条目的一些逻辑,因此它将从左向右输入

呜呜,你没有问如何计算,只是问如何正确输入……哦,好吧。