用python tkinter写计算器
时间: 2023-05-31 07:20:45 浏览: 175
基于python tk模块制作的计算器
### 回答1:
可以使用 Python 的 Tkinter 库来创建一个计算器。首先需要导入 Tkinter 库,然后创建一个主窗口,在窗口中添加文本框、按钮等组件。在按钮上绑定相应的回调函数,实现计算器的功能。最后调用主窗口的 mainloop() 方法运行程序。
下面是一个简单的计算器代码示例:
```python
from tkinter import *
def button_click(number):
current = e.get()
e.delete(0, END)
e.insert(0, str(current) + str(number))
def button_clear():
e.delete(0, END)
def button_add():
first_number = e.get()
global f_num
global math
math = "addition"
f_num = int(first_number)
e.delete(0, END)
def button_equal():
second_number = e.get()
e.delete(0, END)
if math == "addition":
e.insert(0, f_num + int(second_number))
if math == "subtraction":
e.insert(0, f_num - int(second_number))
if math == "multiplication":
e.insert(0, f_num * int(second_number))
if math == "division":
e.insert(0, f_num / int(second_number))
root = Tk()
root.title("Calculator")
e = Entry(root, width=35, borderwidth=5)
e.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
# Define buttons
button_1 = Button(root, text="1", padx=40, pady=20, command=lambda: button_click(1))
button_2 = Button(root, text="2", padx=40, pady=20, command=lambda: button_click(2))
button_3 = Button(root, text="3", padx=40, pady=20, command=lambda: button_click(3))
button_4 = Button(root, text="4", padx=40, pady=20, command=lambda: button_click(4))
button_5 = Button(root, text="5", padx=40, pady=20, command=lambda: button_click(5))
button_6 = Button(root, text="6", padx=40, pady=20, command=lambda:button_click(6))
button_7 = Button(root, text="7", padx=40, pady=20, command=lambda: button_click(7))
button_8 = Button(root, text="8", padx=40, pady=20, command=lambda: button_click(8))
button_9 = Button(root, text="9", padx=40, pady=20, command=lambda: button_click(9))
button_0 = Button(root, text="0", padx=40, pady=20, command=lambda: button_click(0))
button_add = Button(root, text="+", padx=39, pady=20, command=button_add)
button_equal = Button(root, text="=", padx=91, pady=20, command=button_equal)
button_clear = Button(root, text="Clear", padx=79, pady=20, command=button_clear)
# Put the buttons on the screen
button_1.grid(row=1, column=0)
button_2.grid(row=1, column=1)
button_3.grid(row=1, column=2)
button_4.grid(row=2, column=0)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=2)
button_7.grid(row=3, column=0)
button_8.grid(row=3, column=1)
button_9.grid(row=3, column=2)
button_0.grid(row=4, column=0)
button_clear.grid(row=4, column=1, columnspan=2)
button_add.grid(row=5, column=0)
button_equal.grid(row=5, column=1, columnspan=2)
root.mainloop()
```
这是一个简单的 Tkinter 计算器示例,可以实现基本的加法运算。如果需要实现其他运算,可以在 button_equal() 函数中添加相应的 if 语句。需要注意的是,这个程序只是一个示例,并不能在生产环境中使用。
### 回答2:
Python是一种功能强大的编程语言,而Tkinter是Python标准GUI工具包。Python的Tkinter模块使得用户可以很容易地创建各种用户界面,包括简单的计算器。下面我们来讲一下使用Tkinter如何编写一个简单的计算器。
首先,我们需要导入Tkinter模块,然后创建主窗口,并定义主窗口的标题。
```python
from tkinter import *
root = Tk()
root.title("计算器")
```
然后,我们可以创建一个文本框,用于显示计算结果。
```python
result = Entry(root, width=28, font=('Arial', 14))
result.grid(row=0, columnspan=4)
```
接下来,我们可以创建一个计算函数,来实现计算器的功能。这里我们实现加法、减法、乘法和除法的功能,并定义相应的按钮。
```python
def button_click(number):
current = result.get()
result.delete(0, END)
result.insert(0, str(current) + str(number))
def button_clear():
result.delete(0, END)
def button_add():
first_number = result.get()
global f_num
global math
math = "addition"
f_num = int(first_number)
result.delete(0, END)
def button_subtract():
first_number = result.get()
global f_num
global math
math = "subtraction"
f_num = int(first_number)
result.delete(0, END)
def button_multiply():
first_number = result.get()
global f_num
global math
math = "multiplication"
f_num = int(first_number)
result.delete(0, END)
def button_divide():
first_number = result.get()
global f_num
global math
math = "division"
f_num = int(first_number)
result.delete(0, END)
def button_equal():
second_number = result.get()
result.delete(0, END)
if math == "addition":
result.insert(0, f_num + int(second_number))
if math == "subtraction":
result.insert(0, f_num - int(second_number))
if math == "multiplication":
result.insert(0, f_num * int(second_number))
if math == "division":
result.insert(0, f_num / int(second_number))
button_1 = Button(root, text="1", padx=20, pady=10, command=lambda: button_click(1))
button_2 = Button(root, text="2", padx=20, pady=10, command=lambda: button_click(2))
button_3 = Button(root, text="3", padx=20, pady=10, command=lambda: button_click(3))
button_4 = Button(root, text="4", padx=20, pady=10, command=lambda: button_click(4))
button_5 = Button(root, text="5", padx=20, pady=10, command=lambda: button_click(5))
button_6 = Button(root, text="6", padx=20, pady=10, command=lambda: button_click(6))
button_7 = Button(root, text="7", padx=20, pady=10, command=lambda: button_click(7))
button_8 = Button(root, text="8", padx=20, pady=10, command=lambda: button_click(8))
button_9 = Button(root, text="9", padx=20, pady=10, command=lambda: button_click(9))
button_0 = Button(root, text="0", padx=20, pady=10, command=lambda: button_click(0))
button_add = Button(root, text="+", padx=19, pady=10, command=button_add)
button_subtract = Button(root, text="-", padx=20, pady=10, command=button_subtract)
button_multiply = Button(root, text="*", padx=20, pady=10, command=button_multiply)
button_divide = Button(root, text="/", padx=20, pady=10, command=button_divide)
button_clear = Button(root, text="C", padx=20, pady=10, command=button_clear)
button_equal = Button(root, text="=", padx=48, pady=10, command=button_equal)
```
最后,我们可以把所有的按钮都放到主窗口中,并调用mainloop()来循环显示计算器。
```python
button_1.grid(row=3, column=0)
button_2.grid(row=3, column=1)
button_3.grid(row=3, column=2)
button_4.grid(row=2, column=0)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=2)
button_7.grid(row=1, column=0)
button_8.grid(row=1, column=1)
button_9.grid(row=1, column=2)
button_0.grid(row=4, column=0)
button_clear.grid(row=4, column=1)
button_equal.grid(row=4, column=2, columnspan=2)
button_add.grid(row=1, column=3)
button_subtract.grid(row=2, column=3)
button_multiply.grid(row=3, column=3)
button_divide.grid(row=4, column=3)
root.mainloop()
```
到此,一个基于Tkinter的简单计算器就完成了,运行代码即可看到效果。这个计算器还可以进行改进和扩充,例如添加更多的功能和操作,并让界面更加美观、易用。总之,使用Tkinter编写计算器是一个很好的练习,能够帮助我们深入了解Python的GUI编程和数值计算等知识。
### 回答3:
Python tkinter 是一个强大的 Python GUI 工具包,可以为您的应用程序提供图形界面。Tkinter 允许您在 Python 中创建窗口,标签,按钮和文本框等小部件。一旦您熟悉了 Tkinter,就可以使用它来创建任何类型的 GUI 应用程序,包括计算器。
以下是用 Python Tkinter 创建一个简单的计算器的步骤和代码。
1.导入Tkinter模块
```
import tkinter
from tkinter import *
```
2.定义主函数和窗口
```
def main():
calc = Tk()
calc.title("简单计算器")
```
3.定义计算器按钮的函数
```
def calc_button(btn, calc_input):
calc_input.insert(END, str(btn))
```
4.定义计算器清空函数
```
def calc_clear(calc_input):
calc_input.delete(0, END)
```
5.定义计算器计算函数
```
def calc_equal(calc_input):
try:
result = eval(calc_input.get())
calc_clear(calc_input)
calc_input.insert(END, str(result))
calc_input.focus()
except:
calc_clear(calc_input)
calc_input.insert(END, "错误")
```
6.创建计算器界面和按钮
```
calc_input = Entry(calc, width=30, borderwidth=5)
calc_input.grid(row=0, column=0, columnspan=3, padx=10, pady=10)
btn_1 = Button(calc, text="1", padx=40, pady=20, command=lambda: calc_button(1, calc_input))
btn_2 = Button(calc, text="2", padx=40, pady=20, command=lambda: calc_button(2, calc_input))
btn_3 = Button(calc, text="3", padx=40, pady=20, command=lambda: calc_button(3, calc_input))
btn_4 = Button(calc, text="4", padx=40, pady=20, command=lambda: calc_button(4, calc_input))
btn_5 = Button(calc, text="5", padx=40, pady=20, command=lambda: calc_button(5, calc_input))
btn_6 = Button(calc, text="6", padx=40, pady=20, command=lambda: calc_button(6, calc_input))
btn_7 = Button(calc, text="7", padx=40, pady=20, command=lambda: calc_button(7, calc_input))
btn_8 = Button(calc, text="8", padx=40, pady=20, command=lambda: calc_button(8, calc_input))
btn_9 = Button(calc, text="9", padx=40, pady=20, command=lambda: calc_button(9, calc_input))
btn_0 = Button(calc, text="0", padx=40, pady=20, command=lambda: calc_button(0, calc_input))
btn_add = Button(calc, text="+", padx=39, pady=20, command=lambda: calc_button("+", calc_input))
btn_sub = Button(calc, text="-", padx=41, pady=20, command=lambda: calc_button("-", calc_input))
btn_mul = Button(calc, text="*", padx=40, pady=20, command=lambda: calc_button("*", calc_input))
btn_div = Button(calc, text="/", padx=41, pady=20, command=lambda: calc_button("/", calc_input))
btn_equal = Button(calc, text="=", padx=91, pady=20, command=lambda: calc_equal(calc_input))
btn_clear = Button(calc, text="清空", padx=79, pady=20, command=lambda: calc_clear(calc_input))
btn_7.grid(row=1, column=0)
btn_8.grid(row=1, column=1)
btn_9.grid(row=1, column=2)
btn_4.grid(row=2, column=0)
btn_5.grid(row=2, column=1)
btn_6.grid(row=2, column=2)
btn_1.grid(row=3, column=0)
btn_2.grid(row=3, column=1)
btn_3.grid(row=3, column=2)
btn_0.grid(row=4, column=0)
btn_clear.grid(row=4, column=1, columnspan=2)
btn_add.grid(row=5, column=0)
btn_sub.grid(row=6, column=0)
btn_mul.grid(row=6, column=1)
btn_div.grid(row=6, column=2)
btn_equal.grid(row=5, column=1, columnspan=2)
```
7.运行主函数
```
if __name__ == "__main__":
main()
```
这是一个非常基本的计算器,功能不强,可以根据需要添加更多的按钮和功能来增强其功能。
在这个示例中,我们使用 Tkinter 中的 grid 布局来安排按钮和文本输入框。每个按钮都会调用 calc_button 函数,该函数将按下的按钮添加到文本输入框中。每次按下等号按钮时,都会调用 calc_equal 函数来执行结果计算。使用 calc_clear 函数来清除文本输入框。
总之,在 Python Tkinter 中编写一个计算器非常简单,您只需遵循上述步骤即可轻松创建自己的计算器!
阅读全文