用python做一个GUI计算器小数点
时间: 2023-12-04 21:54:56 浏览: 99
好的,我可以为您编写一个简单的GUI计算器,支持小数点的输入和计算。您可以使用Python的Tkinter库来实现GUI界面,并使用eval()函数来计算输入的表达式。以下是代码示例:
```python
from tkinter import *
def button_click(num):
global expression
expression = expression + str(num)
input_text.set(expression)
def button_clear():
global expression
expression = ""
input_text.set("")
def button_equal():
try:
global expression
result = str(eval(expression))
input_text.set(result)
expression = ""
except:
input_text.set("error")
expression = ""
root = Tk()
root.title("Calculator")
expression = ""
input_text = StringVar()
input_frame = Frame(root, width=312, height=50, bd=0, highlightbackground="black", highlightcolor="black", highlightthickness=1)
input_frame.pack(side=TOP)
input_field = Entry(input_frame, font=('arial', 18, 'bold'), textvariable=input_text, width=50, bg="#eee", bd=0, justify=RIGHT)
input_field.grid(row=0, column=0)
input_field.pack(ipady=10)
button_frame = Frame(root, width=312, height=272.5, bg="grey")
button_frame.pack()
clear_button = Button(button_frame, text="C", fg="black", width=32, height=3, bd=0, bg="#eee", cursor="hand2", command=button_clear)
clear_button.grid(row=0, column=0, columnspan=3, padx=1, pady=1)
button_1 = Button(button_frame, text="1", fg="black", width=10, height=3, bd=0, bg="#fff", cursor="hand2", command=lambda: button_click(1))
button_1.grid(row=1, column=0, padx=1, pady=1)
button_2 = Button(button_frame, text="2", fg="black", width=10, height=3, bd=0, bg="#fff", cursor="hand2", command=lambda: button_click(2))
button_2.grid(row=1, column=1, padx=1, pady=1)
button_3 = Button(button_frame, text="3", fg="black", width=10, height=3, bd=0, bg="#fff", cursor="hand2", command=lambda: button_click(3))
button_3.grid(row=1, column=2, padx=1, pady=1)
button_4 = Button(button_frame, text="4", fg="black", width=10, height=3, bd=0, bg="#fff", cursor="hand2", command=lambda: button_click(4))
button_4.grid(row=2, column=0, padx=1, pady=1)
button_5 = Button(button_frame, text="5", fg="black", width=10, height=3, bd=0, bg="#fff", cursor="hand2", command=lambda: button_click(5))
button_5.grid(row=2, column=1, padx=1, pady=1)
button_6 = Button(button_frame, text="6", fg="black", width=10, height=3, bd=0, bg="#fff", cursor="hand2", command=lambda: button_click(6))
button_6.grid(row=2, column=2, padx=1, pady=1)
button_7 = Button(button_frame, text="7", fg="black", width=10, height=3, bd=0, bg="#fff", cursor="hand2", command=lambda: button_click(7))
button_7.grid(row=3, column=0, padx=1, pady=1)
button_8 = Button(button_frame, text="8", fg="black", width=10, height=3, bd=0, bg="#fff", cursor="hand2", command=lambda: button_click(8))
button_8.grid(row=3, column=1, padx=1, pady=1)
button_9 = Button(button_frame, text="9", fg="black", width=10, height=3, bd=0, bg="#fff", cursor="hand2", command=lambda: button_click(9))
button_9.grid(row=3, column=2, padx=1, pady=1)
button_0 = Button(button_frame, text="0", fg="black", width=10, height=3, bd=0, bg="#fff", cursor="hand2", command=lambda: button_click(0))
button_0.grid(row=4, column=0, padx=1, pady=1)
button_dot = Button(button_frame, text=".", fg="black", width=10, height=3, bd=0, bg="#fff", cursor="hand2", command=lambda: button_click("."))
button_dot.grid(row=4, column=1, padx=1, pady=1)
button_equals = Button(button_frame, text="=", fg="black", width=10, height=3, bd=0, bg="#eee", cursor="hand2", command=button_equal)
button_equals.grid(row=4, column=2, padx=1, pady=1)
root.mainloop()
```
在这个简单的计算器中,我使用了一个字符串变量`expression`来存储用户输入的表达式。当用户按下数字按钮时,我将数字添加到`expression`中,然后在输入框中更新表达式。当用户按下清除按钮时,我将`expression`重置为空字符串,并在输入框中显示空字符串。当用户按下等于号按钮时,我使用`eval()`函数计算输入的表达式,并在输入框中显示结果。如果发生错误,则在输入框中显示“error”消息。
这个计算器界面非常简单,只包含数字按钮、小数点按钮、清除按钮和等于号按钮。您可以按照自己的需要扩展它,添加更多的操作符和功能。
阅读全文