Featured image of post Python Basic 31 Tkinter ABQ Example

Python Basic 31 Tkinter ABQ Example

本篇文章主要讲解 Python Tkinter 的一个 ABQ Example

嗨~嗨~!亲爱的比奇堡居民们 (´▽`)ノ !

你是不是已经厌倦了那些枯燥无味的表格? 是不是每次填写数据都像章鱼哥一样愁眉苦脸?

别担心!派大星和海绵宝宝来帮你啦~(ノ°∀°)ノ

让我们用 Python + Tkinter 打造一个超级 美味 实用的数据录入系统! 就像蟹堡王的秘方一样简单又神奇 (`・ω・´)

准备好了吗?让我们穿上小方裤 (ノ◕ヮ◕)ノ*:・゚✧ 开始这场比奇堡编程大冒险吧!

“我准备好啦!!!” - 海绵宝宝式欢呼

ABQ Data Entry (SpongeBob Style) 🧽🌊

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import tkinter as tk
from tkinter import ttk, messagebox
import csv
from datetime import datetime

class BikiniBottomDataEntry(tk.Tk):
    def __init__(self):
        super().__init__()
      
        # 欢迎来到比奇堡数据录入站! 🎶
        self.title("Bikini Bottom Data Entry 🍍")
        self.resizable(0, 0)
      
        # 用派大星的粉色作为主题色 🌸
        self.style = ttk.Style()
        self.style.theme_use('clam')
        self.style.configure('.', background='#FFB6C1', font=('Comic Sans MS', 10))
        self.style.configure('TFrame', background='#FFB6C1')
        self.style.configure('TLabel', background='#FFB6C1', foreground='#2F4F4F')
        self.style.configure('TButton', background='#FF69B4', foreground='white')
      
        # 主框架 - 像海绵宝宝的家一样方正 🏠
        main_frame = ttk.Frame(self)
        main_frame.grid(row=0, column=0, padx=10, pady=10)
      
        # 数据输入部分 - 就像蟹堡王的菜单 📝
        data_frame = ttk.LabelFrame(main_frame, text="✨ 蟹堡王秘密配方录入表 ✨")
        data_frame.grid(row=0, column=0, padx=5, pady=5, sticky="ew")
      
        # 创建输入字段 - 像海绵宝宝一样整齐 📋
        self.create_input_fields(data_frame)
      
        # 按钮部分 - 像章鱼哥的怒气一样明显 😠
        button_frame = ttk.Frame(main_frame)
        button_frame.grid(row=2, column=0, sticky="ew")
        button_frame.columnconfigure(0, weight=1)
        button_frame.columnconfigure(1, weight=1)
      
        # 保存按钮 - 像海绵宝宝一样积极 🏃‍♂️
        save_button = ttk.Button(
            button_frame, 
            text="保存配方 🍔",
            command=self.save_data
        )
        save_button.grid(row=0, column=0, sticky="ew", padx=5)
      
        # 重置按钮 - 像章鱼哥一样想重启人生 🔄
        reset_button = ttk.Button(
            button_frame,
            text="重置表单 🔄",
            command=self.reset_form
        )
        reset_button.grid(row=0, column=1, sticky="ew", padx=5)
      
        # 状态栏 - 像珊迪的实验室显示屏 🧪
        self.status = tk.StringVar()
        self.status.set("准备好记录美味蟹堡的配方了! 🎵")
        status_bar = ttk.Label(main_frame, textvariable=self.status, relief="sunken")
        status_bar.grid(row=3, column=0, sticky="ew", pady=5)
      
    def create_input_fields(self, frame):
        """像做蟹堡一样创建输入字段! 🍳"""
        # 第一排 - 基本资料
        ttk.Label(frame, text="实验室记录编号:").grid(row=0, column=0, sticky="w")
        self.record_id = ttk.Entry(frame)
        self.record_id.grid(row=0, column=1, sticky="ew")
      
        ttk.Label(frame, text="日期:").grid(row=0, column=2, sticky="w")
        self.date = ttk.Entry(frame)
        self.date.insert(0, datetime.today().strftime("%Y-%m-%d"))
        self.date.grid(row=0, column=3, sticky="ew")
      
        # 第二排 - 实验室信息
        ttk.Label(frame, text="实验室:").grid(row=1, column=0, sticky="w")
        self.lab = ttk.Combobox(frame, values=["比奇堡", "皮老板的秘密实验室", "泡泡室"])
        self.lab.grid(row=1, column=1, sticky="ew")
      
        ttk.Label(frame, text="实验师:").grid(row=1, column=2, sticky="w")
        self.technician = ttk.Combobox(frame, values=[
            "海绵宝宝", "派大星", "珊迪", "章鱼哥", "蟹老板",
            "皮老板", "泡芙阿姨", "珍珍"
        ])
        self.technician.grid(row=1, column=3, sticky="ew")
      
        # 第三排 - 重要数据
        ttk.Label(frame, text="配方温度 (°C):").grid(row=2, column=0, sticky="w")
        self.temp = ttk.Spinbox(frame, from_=0, to=100)
        self.temp.grid(row=2, column=1, sticky="ew")
      
        ttk.Label(frame, text="配方浓度 (g/ml):").grid(row=2, column=2, sticky="w")
        self.concentration = ttk.Spinbox(frame, from_=0, to=100, increment=0.1)
        self.concentration.grid(row=2, column=3, sticky="ew")
      
        # 第四排 - 秘密配方!
        ttk.Label(frame, text="配方成分:").grid(row=3, column=0, sticky="w")
        self.ingredients = tk.Text(frame, height=5, width=40, font=('Comic Sans MS', 10))
        self.ingredients.grid(row=4, column=0, columnspan=4, sticky="ew")
      
        # 调整列宽
        for child in frame.winfo_children():
            child.grid_configure(padx=5, pady=5)
        frame.columnconfigure(1, weight=1)
        frame.columnconfigure(3, weight=1)
  
    def save_data(self):
        """把配方存进蟹老板的金库里! 💰"""
        data = {
            '记录编号': self.record_id.get(),
            '日期': self.date.get(),
            '实验室': self.lab.get(),
            '实验师': self.technician.get(),
            '温度': self.temp.get(),
            '浓度': self.concentration.get(),
            '成分': self.ingredients.get("1.0", tk.END).strip()
        }
      
        # 验证数据 - 不能像章鱼哥一样马虎哦! 👀
        errors = []
        for field, value in data.items():
            if not value:
                errors.append(f"{field} 不能为空! 蟹老板会生气的! 🙈")
              
        if errors:
            messagebox.showerror(
                title="哦不! 海绵宝宝搞砸了!", 
                message="\n".join(errors)
            )
            self.status.set("哎呀! 忘记填一些数据了... 😅")
            return
      
        # 写入CSV文件 - 比海之霸的秘密配方还机密! 🤫
        try:
            with open('bikini_bottom_data.csv', 'a', newline='') as f:
                writer = csv.DictWriter(f, fieldnames=data.keys())
                if f.tell() == 0:
                    writer.writeheader()
                writer.writerow(data)
              
            messagebox.showinfo(
                title="成功!",
                message="配方成功保存! 蟹老板会高兴的! 😄"
            )
            self.status.set("配方已保存! 准备做下一个美味蟹堡吧! 🎉")
            self.reset_form()
          
        except Exception as e:
            messagebox.showerror(
                title="糟糕!",
                message=f"皮老板偷走了我们的数据! 发生了错误: {e}"
            )
            self.status.set("哎呀! 保存时出了问题... 😱")
  
    def reset_form(self):
        """重置表单 - 像派大星的脑子一样空白! 💭"""
        for widget in [self.record_id, self.date, self.lab, 
                      self.technician, self.temp, self.concentration]:
            widget.delete(0, tk.END)
      
        self.ingredients.delete("1.0", tk.END)
        self.date.insert(0, datetime.today().strftime("%Y-%m-%d"))
        self.status.set("表单已重置! 再来一个蟹堡配方吧! 🍔")
      
if __name__ == "__main__":
    # 启动应用,像海绵宝宝那样充满精力! 🤸‍♂️
    app = BikiniBottomDataEntry()
    app.mainloop()

使用说明 🌟

  1. 这个小应用就像蟹堡王一样简单好用!
  2. 填写所有字段 - 不然蟹老板会扣你工资的! 💸

  1. 点击"保存配方"将数据存入CSV文件
  2. 想重新开始就点击"重置表单"

功能特点 🐳

  • 派大星粉色主题 💗
  • 海绵宝宝字体 🧽
  • 所有必填字段验证 ✅
  • 自动填充当前日期 📅
  • 蟹堡王风格的状态消息 🍍

海绵宝宝的小提示 🎺

“我准备好了! 我准备好了! 我准备好了!” 准备好记录美味蟹堡的配方了嘛? 🎶

(使用这个应用不会让你变成百万富翁,但蟹老板可能会请你吃一个蟹堡! 🍔)

Licensed under CC BY-NC-SA 4.0
使用 Hugo 构建
主题 StackJimmy 设计