update_button = tk.Button(window, text="更新", command=update_student_info_window) update_button.pack()
时间: 2024-03-18 21:44:24 浏览: 8
这段代码使用 tkinter 模块创建了一个名为 update_button 的按钮控件,并将其添加到名为 window 的 GUI 窗口中。按钮上的文本为 “更新”,并且当用户点击该按钮时,会执行名为 update_student_info_window 的函数。通过调用 pack() 方法,可以将控件添加到窗口中并显示在屏幕上。
相关问题
add_button = tk.Button(window, text="添加", command=add_student) add_button.grid(row=5, column=0) update_button = tk.Button(window, text="修改", command=update_student) update_button.grid(row=5, column=1) delete_button = tk.Button(window, text="删除", command=delete_student) delete_button.grid(row=5, column=2) search_button = tk.Button(window, text="查询", command=search_student) search_button.grid(row=5, column=3) # 连接到 MySQL 数据库 cursor.execute("SELECT * FROM student_info") result = cursor.fetchall() for row in result: tk.Label(root, text=row[0]).pack() # 运行 GUI 应用程序 root.mainloop()
这段代码添加了四个按钮,分别是“添加”、“修改”、“删除”和“查询”,并分别绑定了对应的函数 add_student()、update_student()、delete_student() 和 search_student()。这些按钮都被放置在窗口中的第 5 行,并分别占据第 0、1、2、3 列。
此外,还连接到了 MySQL 数据库,并执行了 SQL 语句“SELECT * FROM student_info”,将查询结果存储在 result 变量中,并使用 for 循环将每一行数据插入到 GUI 界面的标签中,以显示在窗口中。
最后,使用 mainloop() 方法来运行 GUI 应用程序,保持窗口的持续显示。
import tkinter as tk import mysql.connector # 连接 MySQL 数据库 cnx = mysql.connector.connect(user='sa', password='sa', host='localhost', db='Student') cursor = cnx.cursor() # 创建 GUI 界面 def create_student_info_window(): tk.Label(root, text="添加学生信息").pack() tk.Entry(root, textvariable=tk.StringVar(value='请输入学生信息')).pack() tk.Button(root, text="添加", command=lambda: add_student_info(cursor, cnx)).pack() def update_student_info_window(): tk.Label(root, text="更新学生信息").pack() tk.Entry(root, textvariable=tk.StringVar(value='请输入学生信息')).pack() tk.Button(root, text="更新", command=lambda: update_student_info(cursor, cnx)).pack() def delete_student_info_window(): tk.Label(root, text="删除学生信息").pack() tk.Entry(root, textvariable=tk.StringVar(value='请输入学生 ID')).pack() tk.Button(root, text="删除", command=lambda: delete_student_info(cursor, cnx)).pack() def query_student_info_window(): tk.Label(root, text="查询学生信息").pack() tk.Entry(root, textvariable=tk.StringVar(value='请输入学生 ID')).pack() tk.Button(root, text="查询", command=lambda: query_student_info(cursor, cnx)).pack() root = tk.Tk() root.geometry('300x300') root.title("学生信息系统") # 创建 GUI 界面 window = tk.Frame(root) window.pack() # 添加学生信息按钮 add_button = tk.Button(window, text="添加", command=create_student_info_window) add_button.pack() # 更新学生信息按钮 update_button = tk.Button(window, text="更新", command=update_student_info_window) update_button.pack() # 删除学生信息按钮 delete_button = tk.Button(window, text="删除", command=delete_student_info_window) delete_button.pack() # 查询学生信息按钮 query_button = tk.Button(window, text="查询", command=query_student_info_window) query_button.pack() # 连接到 MySQL 数据库 cursor.execute("SELECT * FROM student_info") result = cursor.fetchall() for row in result: tk.Label(root, text=row[0]).pack() # 运行 GUI 应用程序 root.mainloop()
这是一个使用 tkinter 和 mysql.connector 模块创建的学生信息管理系统的代码。该程序提供了添加、更新、删除和查询学生信息的功能。在 GUI 界面中,用户可以通过点击不同的按钮来执行不同的操作。程序使用 mysql.connector 模块连接到 MySQL 数据库,并使用 cursor 对象执行 SQL 查询和操作。在 GUI 界面中,程序通过 tkinter 模块创建不同的控件,例如标签、文本框和按钮等,来实现不同的功能。