你現在是一個python專家,請你幫我寫一段代碼,需要製作一個GUI窗口,製作一個製程異常記錄的系統,窗口中可以填寫一些信息,列如機種、班別、責任人、異常現象...這些窗口,并且可以將這些信息存儲在本地,另外可以查詢到記錄.
时间: 2024-03-16 19:45:29 浏览: 37
好的,为了实现你所需的功能,我们可以使用 Python 的 tkinter 模块来创建 GUI 窗口,使用 csv 模块来读写 csv 文件。
以下是一个简单的代码示例,用于创建一个包含機種、班別、責任人、異常現象等信息的製程異常記錄系统:
```python
import tkinter as tk
import csv
# 创建主窗口
root = tk.Tk()
root.title("製程異常記錄系統")
# 创建标签和输入框
tk.Label(root, text="機種:").grid(row=0, column=0)
machine_entry = tk.Entry(root)
machine_entry.grid(row=0, column=1)
tk.Label(root, text="班別:").grid(row=1, column=0)
shift_entry = tk.Entry(root)
shift_entry.grid(row=1, column=1)
tk.Label(root, text="責任人:").grid(row=2, column=0)
responsible_entry = tk.Entry(root)
responsible_entry.grid(row=2, column=1)
tk.Label(root, text="異常現象:").grid(row=3, column=0)
issue_entry = tk.Entry(root)
issue_entry.grid(row=3, column=1)
# 创建保存按钮
def save_record():
# 获取输入框中的值
machine = machine_entry.get()
shift = shift_entry.get()
responsible = responsible_entry.get()
issue = issue_entry.get()
# 将记录写入 csv 文件
with open('records.csv', 'a', newline='') as file:
writer = csv.writer(file)
writer.writerow([machine, shift, responsible, issue])
# 清空输入框
machine_entry.delete(0, tk.END)
shift_entry.delete(0, tk.END)
responsible_entry.delete(0, tk.END)
issue_entry.delete(0, tk.END)
# 提示保存成功
tk.messagebox.showinfo("提示", "保存成功!")
save_button = tk.Button(root, text="保存", command=save_record)
save_button.grid(row=4, column=0, columnspan=2)
# 创建查询按钮
def search_records():
# 读取 csv 文件中的记录
with open('records.csv', 'r') as file:
reader = csv.reader(file)
records = list(reader)
# 显示记录
if len(records) > 0:
tk.messagebox.showinfo("查询结果", "\n".join([", ".join(record) for record in records]))
else:
tk.messagebox.showinfo("查询结果", "无记录!")
search_button = tk.Button(root, text="查询", command=search_records)
search_button.grid(row=5, column=0, columnspan=2)
# 运行主循环
root.mainloop()
```
在上面的代码中,我们首先创建了一个名为 `root` 的主窗口,然后在窗口中添加了四个标签和四个输入框,用于输入機種、班別、責任人、異常現象等信息。接着,我们创建了一个保存按钮和一个查询按钮,分别用于保存记录和查询历史记录。在保存按钮的回调函数中,我们首先使用 `get()` 方法获取输入框中的值,然后将这些值写入名为 `records.csv` 的 csv 文件中,并清空输入框。在查询按钮的回调函数中,我们使用 `reader` 对象读取 csv 文件中的记录,并将其显示在消息框中。最后,我们通过调用 `mainloop()` 方法来运行主循环,使窗口保持打开状态。
请注意,上面的代码只是一个示例,你需要根据自己的实际需求进行修改和完善。例如,你可能需要添加更多的输入框和标签,或者对记录进行过滤和排序等操作。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)