帮我用tkinter设计一款带有登录系统和可供重复查询的数据库的调查问卷,在登陆界面要求输入账号及密码,成功后载入数据库页面,在数据库页面可以选择查询数据或选择跳转至调查问卷页面,在调查问卷可以选择是否将结果输入数据库
时间: 2024-05-13 11:16:51 浏览: 195
问卷调查数据库设计.doc
5星 · 资源好评率100%
好的,以下是一个简单的示例代码,你可以根据自己的需要进行修改和扩展:
```python
import tkinter as tk
from tkinter import messagebox
import sqlite3
# 创建数据库连接
conn = sqlite3.connect('survey.db')
c = conn.cursor()
# 创建用户表
c.execute('''CREATE TABLE IF NOT EXISTS users
(username TEXT PRIMARY KEY, password TEXT)''')
# 创建问卷表
c.execute('''CREATE TABLE IF NOT EXISTS survey
(id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
age INTEGER,
gender TEXT,
occupation TEXT)''')
conn.commit()
# 登录界面
class LoginPage(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.create_widgets()
def create_widgets(self):
self.username_label = tk.Label(self, text="Username:")
self.username_label.pack()
self.username_entry = tk.Entry(self)
self.username_entry.pack()
self.password_label = tk.Label(self, text="Password:")
self.password_label.pack()
self.password_entry = tk.Entry(self, show="*")
self.password_entry.pack()
self.login_button = tk.Button(self, text="Login", command=self.login)
self.login_button.pack()
def login(self):
username = self.username_entry.get()
password = self.password_entry.get()
# 查询用户表,判断用户名和密码是否正确
c.execute("SELECT * FROM users WHERE username=? AND password=?", (username, password))
result = c.fetchone()
if result is None:
messagebox.showerror("Error", "Invalid username or password")
else:
self.master.switch_frame(DatabasePage)
# 数据库页面
class DatabasePage(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.create_widgets()
def create_widgets(self):
self.query_button = tk.Button(self, text="Query", command=self.query)
self.query_button.pack()
self.survey_button = tk.Button(self, text="Survey", command=self.survey)
self.survey_button.pack()
def query(self):
# 查询问卷表,显示结果
c.execute("SELECT * FROM survey")
result = c.fetchall()
messagebox.showinfo("Query Result", result)
def survey(self):
self.master.switch_frame(SurveyPage)
# 问卷页面
class SurveyPage(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.pack()
self.create_widgets()
def create_widgets(self):
self.name_label = tk.Label(self, text="Name:")
self.name_label.pack()
self.name_entry = tk.Entry(self)
self.name_entry.pack()
self.age_label = tk.Label(self, text="Age:")
self.age_label.pack()
self.age_entry = tk.Entry(self)
self.age_entry.pack()
self.gender_label = tk.Label(self, text="Gender:")
self.gender_label.pack()
self.gender_entry = tk.Entry(self)
self.gender_entry.pack()
self.occupation_label = tk.Label(self, text="Occupation:")
self.occupation_label.pack()
self.occupation_entry = tk.Entry(self)
self.occupation_entry.pack()
self.submit_button = tk.Button(self, text="Submit", command=self.submit)
self.submit_button.pack()
def submit(self):
name = self.name_entry.get()
age = self.age_entry.get()
gender = self.gender_entry.get()
occupation = self.occupation_entry.get()
# 插入问卷表
c.execute("INSERT INTO survey(name, age, gender, occupation) VALUES (?, ?, ?, ?)", (name, age, gender, occupation))
conn.commit()
messagebox.showinfo("Success", "Survey submitted")
self.master.switch_frame(DatabasePage)
# 主程序
class App(tk.Tk):
def __init__(self):
super().__init__()
self.title("Survey App")
self.geometry("300x200")
self.resizable(False, False)
self.create_widgets()
def create_widgets(self):
self.container = tk.Frame(self)
self.container.pack(side="top", fill="both", expand=True)
self.container.grid_rowconfigure(0, weight=1)
self.container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (LoginPage, DatabasePage, SurveyPage):
page_name = F.__name__
frame = F(master=self.container)
self.frames[page_name] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.switch_frame(LoginPage)
def switch_frame(self, page_class):
frame = self.frames[page_class.__name__]
frame.tkraise()
if __name__ == "__main__":
app = App()
app.mainloop()
# 关闭数据库连接
conn.close()
```
在这个例子中,我们使用了tkinter来创建一个GUI程序,包含了登录界面、数据库页面和问卷页面。用户需要在登录界面输入正确的用户名和密码才能进入数据库页面,然后可以选择查询数据或跳转到问卷页面。在问卷页面,用户可以填写调查问卷,选择是否将结果输入到数据库中。数据库使用SQLite进行存储,其中包含了两张表:用户表和问卷表。你可以根据自己的需要修改和扩展这个程序,例如添加更多的字段、修改样式、添加验证等等。
阅读全文