如何通过Entry Widget显示输出。假设我正在添加2个数字,并希望使用条目小部件在文本框中显示它们的总和。
from tkinter import *
from tkinter.messagebox import *
def show_answer():
Ans = int(num1.get()) + int(num2.get())
ans.set(Ans)
Entry(main, text = "%s" %(ans) ).grid(row=2, column=1)
main = Tk()
Label(main, text = "Enter Num 1:").grid(row=0)
Label(main, text = "Enter Num 2:").grid(row=1)
Label(main, text = "The Sum is:").grid(row=2)
num1 = Entry(main)
num2 = Entry(main)
blank = Entry(main)
num1.grid(row=0, column=1)
num2.grid(row=1, column=1)
blank.grid(row=2, column=1)
Button(main, text='Quit', command=main.quit).grid(row=4, column=0, sticky=W, pady=4)
Button(main, text='Show', command=show_answer).grid(row=4, column=1, sticky=W, pady=4)
mainloop()
示例输出:
答案 0 :(得分:1)
如果您想在窗口中显示答案,可以先创建一个标签。然后,您可以将该标签中的已发送文本更改为您希望它显示的内容。
创建标签:
snprintf(getdetailbyparamfilelocation,
sizeof(getdetailbyparamfilelocation),
"%s/getdetailbyparam.txt",cwd);
然后更新它:
lsum = Tk.Label(master, text = 'The sum is:')
lsum.grid(row=5, column=1, sticky=W, pady=4)
在您的代码中实现它:
lsum["text"] = "The sum is: " + str(ans)
答案 1 :(得分:1)
from tkinter import *
from math import *
main = Tk()
main.title('CALCULATOR')
def add():
blank.delete(0, END)
Ans = int(num1.get()) + int(num2.get())
blank.insert(0, Ans)
def sub():
blank.delete(0, END)
Ans = int(num1.get()) - int(num2.get())
blank.insert(0, Ans)
def mult():
blank.delete(0, END)
Ans = int(num1.get()) * int(num2.get())
blank.insert(0, Ans)
def div():
blank.delete(0, END)
Ans = int(num1.get()) / int(num2.get())
blank.insert(0, Ans)
def clear():
blank.delete(0, END)
num2.delete(0, END)
num1.delete(0, END)
def sq():
blank.delete(0, END)
Ans = int(num1.get()) * int(num1.get())
blank.insert(0, Ans)
def sqrtt():
blank.delete(0, END)
h = int(num1.get())
a = sqrt(h)
Ans = (int(a))
blank.insert(0, Ans)
main.geometry('500x100')
Label(main, text = "Enter Num 1:").grid(row=0)
Label(main, text = "Enter Num 2:").grid(row=1)
Label(main, text = "The Answer is:").grid(row=2)
num1 = Entry(main)
num2 = Entry(main)
blank = Entry(main)
num1.grid(row=0, column=1)
num2.grid(row=1, column=1)
blank.grid(row=2, column=1)
Button(main, text='Quit', command=main.destroy).grid(row=4, column=0, sticky=W)
Button(main, text='Add', command=add).grid(row=0, column=3, sticky=W,)
Button(main, text='Subtract', command=sub).grid(row=0, column=4, sticky=W)
Button(main, text='Multiply', command=mult).grid(row=0, column=5, sticky=W)
Button(main, text='Divide', command=div).grid(row=0, column=6, sticky=W)
Button(main, text='^2', command=sq).grid(row=0, column=7, sticky=W)
Button(main, text='Sqrt', command=sqrtt).grid(row=0, column=8, sticky=W)
Button(main, text='Clear', command=clear).grid(row=0, column=9, sticky=W)
mainloop()
我正在做类似的事情,我对其进行了修改,使它有些过头了,它不适用于小数,但可以与其他所有功能一起使用。
答案 2 :(得分:0)
我已经有了答案
from tkinter import *
from tkinter.messagebox import *
def show_answer():
Ans = int(num1.get()) + int(num2.get())
blank.insert(0, Ans)
main = Tk()
Label(main, text = "Enter Num 1:").grid(row=0)
Label(main, text = "Enter Num 2:").grid(row=1)
Label(main, text = "The Sum is:").grid(row=2)
num1 = Entry(main)
num2 = Entry(main)
blank = Entry(main)
num1.grid(row=0, column=1)
num2.grid(row=1, column=1)
blank.grid(row=2, column=1)
Button(main, text='Quit', command=main.destroy).grid(row=4, column=0, sticky=W, pady=4)
Button(main, text='Show', command=show_answer).grid(row=4, column=1, sticky=W, pady=4)
mainloop()
答案 3 :(得分:0)
这也可以(用户友好)
from tkinter import *
import tkinter.messagebox
def show_answer():
Ans = int(num1.get()) + int(num2.get())
tkinter.messagebox.showinf('answer',Ans)
main = Tk()
Label(main, text="Enter Num 1:").grid(row=0)
Label(main, text="Enter Num 2:").grid(row=1)
num1 = Entry(main)
num2 = Entry(main)
num1.grid(row=0,column=1)
num2.grid(row=1,column=1)
Button(main,text='Quit',command=main.destroy).grid(row=4,column=0,sticky=W,pady=4)
Button(main,text='Show',command=show_answer).grid(row=4,column=1,sticky=W,pady=4)
mainloop()