从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,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()
jwillis0720

如果您想使用全部功能,我是否建议添加一个等于按钮,该按钮将计算条目小部件中的所有内容?

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

注意,我更改了除号和输入的某些逻辑,因此它将从左到右输入。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

tkinter 输入框中的第一个字母未被删除

如何使用vbscript在一个用户窗体中创建多个输入框

想用Tkinter输入框写一个txt

提交按钮仅保存一个输入框

用复选框中的另一个输入自动填充同一个类中的多个输入框

如何将在tkinter的输入框中输入的文本从一个函数传递给另一个函数?

在当前输入框中输入数字后,专注于下一个输入框

在输入框中检测到第一个字符后,显示“清除文本”按钮

在React中单击另一个输入框时如何更改输入值

如何将我的第一个输入框链接到数据库中已存在的另一个输入框

输入框进入循环仅插入最后一个条目

如何给出一个JS命令将光标放在输入框中?

跳至JSON数组中的下一个对象并填充输入框

如何在输入框中创建一个下拉列表?

将输入框的值与JavaScript中的上一个结果相除

如何将下拉列表中的一个值附加到输入框的末尾

我想使用 js 或 jquery 只复制输入框中的最后一个文本

我在 python 中有一个 tkinter 输入框,但我无法将输入输入到我的代码中?

当两个输入框中的任何一个有输入时,如何自动将输入框的值设置为相反的数字?

从具有相同HTML代码的多个框中选择一个特定的输入框

在onkeyup的另一个输入框中,在两个输入值之间显示'&'。

添加2个输入值并在另一个输入框中显示结果

是否可以在 html 中取消隐藏已选中但未选中的第一个框的第二个输入框上的提交按钮

如何将输入框中的 1 个值除以 10 另一个输入框除以 10 而不舍入

pygame如何在一个表面上具有多个输入框并将这些输入存储在变量中?

如何创建一个滑块来更改表示时间的两个输入框中的数值

如何基于另一个输入框自动填充输入框

如何从另一个输入框生成“n”行输入框?

相对于另一个输入框更改输入框的值?