python课程设计项目tkinter应用开发
时间: 2023-06-19 16:05:30 浏览: 103
Python课程设计基于tkinter的学生信息管理系统源码+实验报告.zip
好的,下面是一个简单的 Python Tkinter 应用开发示例。这个示例应用程序是一个简单的计算器,可以对两个数进行加、减、乘、除运算。
首先,我们需要导入必要的模块:
```
from tkinter import *
from tkinter import messagebox
```
接下来,我们需要创建一个 Tkinter 应用程序对象并设置窗口标题:
```
root = Tk()
root.title("Calculator")
```
然后,我们需要定义计算器的一些函数,例如加法、减法、乘法和除法:
```
def add():
num1 = float(entry1.get())
num2 = float(entry2.get())
result = num1 + num2
label_result.config(text="Result: " + str(result))
def subtract():
num1 = float(entry1.get())
num2 = float(entry2.get())
result = num1 - num2
label_result.config(text="Result: " + str(result))
def multiply():
num1 = float(entry1.get())
num2 = float(entry2.get())
result = num1 * num2
label_result.config(text="Result: " + str(result))
def divide():
num1 = float(entry1.get())
num2 = float(entry2.get())
if num2 == 0:
messagebox.showerror("Error", "Cannot divide by zero!")
else:
result = num1 / num2
label_result.config(text="Result: " + str(result))
```
在这些函数中,我们首先获取文本框中输入的两个数,然后执行相应的操作并将结果显示在标签中。如果用户试图除以零,我们会显示一个错误消息框。
接下来,我们需要在窗口中添加文本框和标签,并设置它们的位置和样式:
```
entry1 = Entry(root, width=10, font=("Arial", 16))
entry1.grid(row=0, column=0, padx=10, pady=5)
entry2 = Entry(root, width=10, font=("Arial", 16))
entry2.grid(row=0, column=1, padx=10, pady=5)
label_plus = Label(root, text="+", font=("Arial", 16))
label_plus.grid(row=0, column=2, padx=10, pady=5)
label_minus = Label(root, text="-", font=("Arial", 16))
label_minus.grid(row=1, column=2, padx=10, pady=5)
label_multiply = Label(root, text="*", font=("Arial", 16))
label_multiply.grid(row=2, column=2, padx=10, pady=5)
label_divide = Label(root, text="/", font=("Arial", 16))
label_divide.grid(row=3, column=2, padx=10, pady=5)
button_add = Button(root, text="Add", font=("Arial", 16), command=add)
button_add.grid(row=1, column=0, padx=10, pady=5)
button_subtract = Button(root, text="Subtract", font=("Arial", 16), command=subtract)
button_subtract.grid(row=2, column=0, padx=10, pady=5)
button_multiply = Button(root, text="Multiply", font=("Arial", 16), command=multiply)
button_multiply.grid(row=3, column=0, padx=10, pady=5)
button_divide = Button(root, text="Divide", font=("Arial", 16), command=divide)
button_divide.grid(row=4, column=0, padx=10, pady=5)
label_result = Label(root, text="Result: ", font=("Arial", 16))
label_result.grid(row=4, column=1, padx=10, pady=5)
```
在这些代码中,我们创建了两个文本框、四个标签和四个按钮,分别对应加、减、乘、除运算。我们将它们放置在网格中并设置相应的行和列。
最后,我们需要运行应用程序并进入主事件循环:
```
root.mainloop()
```
完整的代码如下所示:
```
from tkinter import *
from tkinter import messagebox
root = Tk()
root.title("Calculator")
def add():
num1 = float(entry1.get())
num2 = float(entry2.get())
result = num1 + num2
label_result.config(text="Result: " + str(result))
def subtract():
num1 = float(entry1.get())
num2 = float(entry2.get())
result = num1 - num2
label_result.config(text="Result: " + str(result))
def multiply():
num1 = float(entry1.get())
num2 = float(entry2.get())
result = num1 * num2
label_result.config(text="Result: " + str(result))
def divide():
num1 = float(entry1.get())
num2 = float(entry2.get())
if num2 == 0:
messagebox.showerror("Error", "Cannot divide by zero!")
else:
result = num1 / num2
label_result.config(text="Result: " + str(result))
entry1 = Entry(root, width=10, font=("Arial", 16))
entry1.grid(row=0, column=0, padx=10, pady=5)
entry2 = Entry(root, width=10, font=("Arial", 16))
entry2.grid(row=0, column=1, padx=10, pady=5)
label_plus = Label(root, text="+", font=("Arial", 16))
label_plus.grid(row=0, column=2, padx=10, pady=5)
label_minus = Label(root, text="-", font=("Arial", 16))
label_minus.grid(row=1, column=2, padx=10, pady=5)
label_multiply = Label(root, text="*", font=("Arial", 16))
label_multiply.grid(row=2, column=2, padx=10, pady=5)
label_divide = Label(root, text="/", font=("Arial", 16))
label_divide.grid(row=3, column=2, padx=10, pady=5)
button_add = Button(root, text="Add", font=("Arial", 16), command=add)
button_add.grid(row=1, column=0, padx=10, pady=5)
button_subtract = Button(root, text="Subtract", font=("Arial", 16), command=subtract)
button_subtract.grid(row=2, column=0, padx=10, pady=5)
button_multiply = Button(root, text="Multiply", font=("Arial", 16), command=multiply)
button_multiply.grid(row=3, column=0, padx=10, pady=5)
button_divide = Button(root, text="Divide", font=("Arial", 16), command=divide)
button_divide.grid(row=4, column=0, padx=10, pady=5)
label_result = Label(root, text="Result: ", font=("Arial", 16))
label_result.grid(row=4, column=1, padx=10, pady=5)
root.mainloop()
```
这个示例只是 Tkinter 应用程序开发的一个简单示例,您可以根据自己的需要进行修改和扩展。
阅读全文