Backend Development 29 min read

Python Tkinter Vaccine Management System with CRUD Operations and Database Integration

This article presents a step‑by‑step tutorial for building a Python Tkinter vaccine management application that demonstrates practical CRUD operations, GUI design, and MySQL database integration, providing complete source code, database schema, and detailed explanations for each functional module.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python Tkinter Vaccine Management System with CRUD Operations and Database Integration

Many beginners ask how to learn Python effectively; the author recommends practical, hands‑on projects and shares a small yet comprehensive vaccine management system built with Python, Tkinter, and MySQL, suitable for newcomers.

The system includes a SQLite‑compatible MySQL database schema for storing vaccine information, distribution records, maintenance logs, and vaccination personnel data.

<code>create table vaccine_info(
    vaccine_num    char(50) not null primary key,
    vaccine_name   char(50) not null,
    company_name   char(50) not null,
    company_num    char(50) not null,
    size           char(50) null,
    buy_price      char(50) not null,
    pre_sale_price char(20) not null,
    limit_up       char(50) not null,
    limit_down    char(50) not null
);

create table user_info(
    id int auto_increment primary key,
    user_name char(50) NOT NULL,
    user_code char(50) NOT NULL
);

create table if not exists vaccine_distr_info (
    vaccine_distr_num char(50) primary key,
    date date not null,
    vaccine_num char(50) not null,
    vaccine_name char(50) not null,
    company_num char(50) not null,
    operator_num char(50) not null,
    num int not null
);

create table if not exists vaccine_maintenance_info (
    vaccine_maintenance_num char(50) primary key,
    vaccine_maintenance_name char(50) not null,
    admin_num char(50) not null,
    admin_name char(50) not null,
    maintenance_time date,
    cold_storage_temp char(20) not null,
    freezer_temp char(20) not null,
    equipment_operation char(50) not null,
    alter_info char(50) not null
);

create table if not exists vaccination_person_info(
    id int auto_increment primary key,
    name char(20) not null,
    sexy char(10) not null,
    age char(10) not null,
    ID_num char(50) not null,
    address char(70) not null,
    allergy char(10) not null,
    date date
);</code>

The main GUI window is created with Tkinter, providing buttons for login, registration, and exit, each linked to their respective functions.

<code>def main_window(self):
    tk.Button(app, text='登录', bg='white', font=('Arial',12), width=12, height=1, command=self.login).place(x=260, y=200)
    tk.Button(app, text='注册', bg='white', font=('Arial',12), width=12, height=1, command=self.register).place(x=260, y=240)
    tk.Button(app, text='退出', bg='white', font=('Arial',12), width=12, height=1, command=self.quit_mainloop).place(x=260, y=280)
</code>

The registration window collects administrator name and code, validates input, and inserts a new record into the user_info table.

<code>def register(self):
    register = tk.Toplevel(app)
    tk.Label(register, text='欢迎注册', font=('KaiTi',40)).place(x=200, y=15)
    tk.Label(register, text='添加管理员姓名:', font=('Arial',9)).place(x=80, y=120)
    tk.Label(register, text='确认管理员编号:', font=('Arial',9)).place(x=80, y=150)
    entry1 = tk.Entry(register, font=('Arial, 9'), width=46)
    entry2 = tk.Entry(register, font=('Arial, 9'), width=46)
    entry1.place(x=180, y=120, width=350)
    entry2.place(x=180, y=150, width=350)
    tk.Button(register, text='注册', bg='white', font=('Arial,9'), command=user_register).place(x=250, y=250)
</code>

The login window verifies credentials against the user_info table and proceeds to the main options interface upon success.

<code>def login(self):
    login = tk.Toplevel(app)
    tk.Label(login, text='用户登录', font=('KaiTi',40)).place(x=200, y=15)
    tk.Label(login, text='管理员姓名:', font=('Arial',9)).place(x=80, y=120)
    tk.Label(login, text='管理员编号:', font=('Arial',9)).place(x=80, y=150)
    entry1 = tk.Entry(login, font=('Arial, 9'), width=46)
    entry2 = tk.Entry(login, font=('Arial, 9'), width=46, show='*')
    entry1.place(x=180, y=120, width=350)
    entry2.place(x=180, y=150, width=350)
    tk.Button(login, text='登录', bg='white', font=('Arial,9'), command=user_check).place(x=250, y=250)
</code>

After successful login, the options window offers CRUD functionalities: adding new vaccine records, distribution records, maintenance logs, vaccination personnel, as well as queries, modifications, and deletions.

<code>def options(self):
    options = tk.Toplevel(app)
    tk.Label(options, text='欢迎使用!', font=('KaiTi',40)).place(x=180, y=15)
    tk.Button(options, text='新建疫苗信息', bg='white', font=('Arial',12), width=20, height=2, command=self.add_vacc_info).place(x=100, y=100)
    tk.Button(options, text='新建疫苗分配信息', bg='white', font=('Arial',12), width=20, height=2, command=self.add_vaccine_distr_info).place(x=100, y=160)
    tk.Button(options, text='新建疫苗养护信息', bg='white', font=('Arial',12), width=20, height=2, command=self.add_vaccine_maintenance_info).place(x=100, y=220)
    tk.Button(options, text='新建接种人员信息', bg='white', font=('Arial',12), width=20, height=2, command=self.add_vaccination_person_info).place(x=100, y=280)
    tk.Button(options, text='查询疫苗分配信息', bg='white', font=('Arial',12), width=20, height=2, command=self.vaccine_distr_info_query).place(x=320, y=100)
    tk.Button(options, text='查询疫苗养护信息', bg='white', font=('Arial',12), width=20, height=2, command=self.vaccination_maintenance_info_query).place(x=320, y=160)
    tk.Button(options, text='查询接种人员信息', bg='white', font=('Arial',12), width=20, height=2, command=self.vaccination_person_info_query).place(x=320, y=220)
    tk.Button(options, text='查询疫苗信息', bg='white', font=('Arial',12), width=20, height=2, command=self.vaccine_info_query).place(x=320, y=280)
    tk.Button(options, text='修改疫苗信息', bg='white', font=('Arial',12), width=20, height=2, command=self.modify_vaccine_info).place(x=320, y=340)
    tk.Button(options, text='删除疫苗信息', bg='white', font=('Arial',12), width=20, height=2, command=self.del_vaccine_info).place(x=320, y=400)
</code>

Each "add" function opens a dedicated form with Entry widgets for the required fields and inserts the collected data into the corresponding tables using parameterised INSERT statements.

<code>def add_vacc_info(self):
    add_vacc_info = tk.Toplevel(app)
    tk.Label(add_vacc_info, text='疫苗批号:', font=('Arial',9)).place(x=80, y=60)
    entry1 = tk.Entry(add_vacc_info, font=('Arial, 9'), width=46)
    entry1.place(x=180, y=60, width=350)
    # ... other fields ...
    def add():
        text1 = entry1.get(); text2 = entry2.get(); ...
        content = "INSERT INTO vaccine_info (vaccine_num, vaccine_name, company_name, company_num, size, buy_price, pre_sale_price, limit_up, limit_down) VALUES (%s, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');" % (text1, text2, ...)
        self.connect_DBS(database='vaccine_info', content=content)
        tkinter.messagebox.showinfo(title='信息', message='数据添加成功!')
    tk.Button(add_vacc_info, text='添加', bg='white', font=('Arial,9'), command=add).place(x=400, y=360)
</code>

Similar forms are provided for vaccine distribution ( add_vaccine_distr_info ), maintenance ( add_vaccine_maintenance_info ), and vaccination personnel ( add_vaccination_person_info ).

Query windows allow the user to input a key (e.g., vaccine batch number) and display the result of a SELECT query in a Text widget.

<code>def vaccine_info_query(self):
    query = tk.Toplevel(app)
    tk.Label(query, text='请输入疫苗批号:', font=('Arial',9)).place(x=50, y=80)
    entry = tk.Entry(query, width=30)
    entry.place(x=200, y=80)
    text1 = tk.Text(query, width=50, height=20)
    text1.place(x=150, y=120)
    def base_query():
        vaccine_num = entry.get()
        content = "SELECT * FROM vaccine_info WHERE vaccine_num = %s;" % vaccine_num
        data = self.connect_DBS(database='vaccine_info', content=content)
        text1.delete('1.0', 'end')
        text1.insert('insert', str(data))
    tk.Button(query, text='查询', bg='white', font=('Arial,12'), command=base_query).place(x=450, y=75)
</code>

Modification and deletion functionalities follow the same pattern: a query step to fetch existing data, editable fields for updates, and an UPDATE or DELETE statement executed against the database.

<code>def modify_vaccine_info(self):
    modify_info = tk.Toplevel(app)
    tk.Label(modify_info, text='请输入疫苗分配单号:', font=('Arial',9)).place(x=50, y=60)
    entry = tk.Entry(modify_info, width=30)
    entry.place(x=200, y=60)
    # Text widgets for each column ...
    def base_query():
        vaccine_modify_num = entry.get()
        content = "SELECT * FROM vaccine_info WHERE vaccine_num = %s;" % vaccine_modify_num
        data = self.connect_DBS(database='vaccine_info', content=content)
        # populate Text widgets with data
    def update_info():
        # collect edited values from Text widgets
        content = "UPDATE vaccine_info SET vaccine_name='%s', company_name='%s', ... WHERE vaccine_num='%s';" % (new_name, new_company, ..., vaccine_modify_num)
        self.connect_DBS(database='vaccine_info', content=content)
        tkinter.messagebox.showinfo(title='信息', message='数据修改成功!')
    tk.Button(modify_info, text='查询', bg='white', command=base_query).place(x=450, y=55)
    tk.Button(modify_info, text='修改', bg='white', command=update_info).place(x=260, y=370)
</code>

Overall, the article provides a complete, runnable example that teaches beginners how to combine Tkinter GUI development with MySQL database operations, reinforcing core Python programming concepts such as functions, error handling, and user interaction.

GUIPythonDatabaseMySQLCRUDTkinterVaccine Management
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.