0

我提供了一些代码,这些代码使用回调来允许条目框从按钮中获取值。问题是,如何将两个连接的值回调到输入框中。如果用户单击 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()
4

2 回答 2

0

只需在末尾插入文本即可。

def set_text(text):
    e.insert(END,text)
    return
于 2013-12-11T06:54:01.690 回答
0

如果您想要完整的功能,我是否建议添加一个等号按钮来计算条目小部件中的所有内容?

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()

请注意,我更改了除法符号和条目的一些逻辑,因此它会从左到右输入。

于 2013-12-11T07:26:42.723 回答