我不知道如何在输入计算器的地方打印答案。
我要插入的代码是:
Button(master, text="Submit",width=23, height=3, command=lambda(?)).grid(row=5, column=0, columnspan=3)
?=我想要定义的东西
我希望条目self.e
中的文本打印在消息框或标签的条目外部。
from tkinter import *
from tkinter import ttk
import tkinter as tk
root = tk.Tk()
class sweltres:
def clearall(self):
self.e.delete(0,END)
def clear1(self):
self.txt=self.e.get()[:-1]
self.e.delete(0,END)
self.e.insert(0,self.txt)
def action(self,argi):
self.e.insert(END,argi)
def __init__(self,master):
master.title('Calculator')
master.geometry("100x50")
self.e = ttk.Entry(master)
self.e.grid(row=0,column=0,columnspan=6,pady=3)
self.e.focus_set()
Button(master,text='AC',width=5,height=3,
fg="black", bg="blue",
command=lambda:self.clearall()).grid(row=4, column=2)
Button(master,text='C',width=5,height=3,
fg="red",bg="blue",
command=lambda:self.clear1()).grid(row=4, column=0)
Button(master,text="7",width=5,height=3,
fg="white",bg="blue",
command=lambda:self.action('7')).grid(row=1, column=0)
Button(master,text="8",width=5,height=3,
fg="white",bg="blue",
command=lambda:self.action(8)).grid(row=1, column=1)
Button(master,text="9",width=5,height=3,
fg="white",bg="blue",
command=lambda:self.action(9)).grid(row=1, column=2)
Button(master,text="4",width=5,height=3,
fg="white",bg="blue",
command=lambda:self.action(4)).grid(row=2, column=0)
Button(master,text="5",width=5,height=3,
fg="white",bg="blue",
command=lambda:self.action(5)).grid(row=2, column=1)
Button(master,text="6",width=5,height=3,
fg="white",bg="blue",
command=lambda:self.action(6)).grid(row=2, column=2)
Button(master,text="1",width=5,height=3,
fg="white",bg="blue",
command=lambda:self.action(1)).grid(row=3, column=0)
Button(master,text="2",width=5,height=3,
fg="white",bg="blue",
command=lambda:self.action(2)).grid(row=3, column=1)
Button(master,text="3",width=5,height=3,
fg="white",bg="blue",
command=lambda:self.action(3)).grid(row=3, column=2)
Button(master,text="0",width=5,height=3,
fg="white",bg="blue",
command=lambda:self.action(0)).grid(row=4, column=1)
Button(master, text="Exit wtf",width=23,height=3,
fg="white",bg="red", command=master.destroy).grid(row=6, column=0, columnspan=3)
sweltres(root)
root.mainloop()
是的,你可以得到一个弹出的消息框,它的文本是Entry
中的文本:
class sweltres:
def msg(self):
tk.messagebox.showinfo(title='Submit Button',message = self.e.get())
def __init__(self,master):
# your existing parts elided
tk.Button(master, text="Submit",width=23, height=3, command=self.msg).grid(row=5, column=0, columnspan=3)
对不起,如果我没有很好地理解你的答案,但如果我知道,我以前做过这种事情。你需要在你的条目中打印文本,self.e
,到一个标签或其他东西。你可以这样做:
def callback(event):
text = tk.StringVar()
text.set(self.e.get())
lbl = ttk.Label(master, textvariable=text)
# lbl.whatever_geometry_manager_you_use(**options)
lbl.pack(side="bottom")
对于消息框,在顶部这样做:
from tkinter.messagebox import showinfo
def callback(event):
text = tk.StringVar()
text.set(self.e.get())
showinfo(title="Result", message=text)
现在,你可以这样定义你的按钮:
# submit_btn being your variable name
submit_btn = Button(master, text="Submit", width=23, height=3, command=callback).grid(row=5, column=0, columnspan=3)
或者如果你喜欢和喜欢lambda
:
# submit_btn being your variable name
submit_btn = Button(master, text="Submit", width=23, height=3, command=lambda: callback()).grid(row=5, column=0, columnspan=3)
毫无疑问,有更好的方法来做这件事。这个答案一部分是对你的问题的回答,一部分是对其他可以回答你问题的人的澄清。