|
|

楼主 |
发表于 2024-12-29 11:02:01
|
显示全部楼层
app_ttk_ui.py
- import ttkbootstrap as ttk
- from ttkbootstrap.constants import *
- from ttkbootstrap.dialogs import Messagebox
- import sqlite3
- # from EXT_XKB_BT import *
- class DBINIT:
- def __init__(self, root, title, idb):
- self.root = root
- self.root.title(title)
- # self.root.geometry("800x600")
- # 创建数据库连接
- self.conn = sqlite3.connect(idb)
- self.create_table()
- # 创建主框架
- self.create_main_frame()
- # 创建界面元素
- self.create_toolbar() # 工具栏和搜索框
- self.create_input_area() # 输入区域和增删改查按钮
- self.create_table_view() # 数据表格
- # 显示初始数据
- # self.show_records()
- class DBUI(DBINIT):
- def create_main_frame(self):
- self.main_frame = ttk.Frame(self.root, padding="10")
- self.main_frame.pack(fill=BOTH, expand=YES)
- self.root.geometry("800x600")
- def create_toolbar(self):
- toolbar = ttk.Frame(self.main_frame)
- toolbar.pack(fill=X, pady=(0, 10))
- # 主题选择下拉框
- themes = ttk.Style().theme_names()
- self.theme_var = ttk.StringVar(value=ttk.Style().theme.name)
- theme_menu = ttk.Menubutton(toolbar, text="切换主题", bootstyle=OUTLINE)
- theme_menu.pack(side=LEFT, padx=5)
- inner_menu = ttk.Menu(theme_menu)
- theme_menu['menu'] = inner_menu
- for theme in themes:
- inner_menu.add_radiobutton(
- label=theme,
- value=theme,
- variable=self.theme_var,
- command=self.change_theme
- )
- # 搜索框
- self.search_var = ttk.StringVar()
- search_entry = ttk.Entry(toolbar, textvariable=self.search_var)
- search_entry.pack(side=RIGHT, padx=5)
- search_btn = ttk.Button(
- toolbar,
- text="搜索",
- command=self.search_records,
- bootstyle=INFO
- )
- search_btn.pack(side=RIGHT)
- def clear_entries(self):
- self.name_entry.delete(0, END)
- self.age_entry.delete(0, END)
- self.email_entry.delete(0, END)
- self.search_var.set("")
- def item_selected(self, event):
- selected = self.tree.selection()
- if selected:
- values = self.tree.item(selected)['values']
- self.clear_entries()
- self.name_entry.insert(0, values[1])
- self.age_entry.insert(0, values[2])
- self.email_entry.insert(0, values[3])
- def create_table_view(self):
- # 创建表格框架
- table_frame = ttk.LabelFrame(
- self.main_frame,
- text="数据显示",
- padding="15"
- )
- table_frame.pack(fill=BOTH, expand=YES)
- # 创建表格和滚动条
- columns = ("编号", "名字", "年龄", "邮箱", "创建时间")
- self.tree = ttk.Treeview(
- table_frame,
- columns=columns,
- show="headings",
- bootstyle=INFO
- )
- # 设置列
- for col in columns:
- self.tree.heading(col, text=col)
- self.tree.column(col, width=130)
- # 添加滚动条
- scrollbar = ttk.Scrollbar(
- table_frame,
- orient=VERTICAL,
- command=self.tree.yview
- )
- self.tree.configure(yscrollcommand=scrollbar.set)
- # 放置表格和滚动条
- self.tree.pack(side=LEFT, fill=BOTH, expand=YES)
- scrollbar.pack(side=RIGHT, fill=Y)
- # 绑定选择事件
- self.tree.bind('<<TreeviewSelect>>', self.item_selected)
- def change_theme(self):
- ttk.Style().theme_use(self.theme_var.get())
- class UDB(DBUI):
- # def __init__(self, root, title, idb,name):
- # self.mgdb = 'mgdb'
- # self.name = name
- def show_records(self):
- for item in self.tree.get_children():
- self.tree.delete(item)
- cursor = self.conn.cursor()
- cursor.execute("SELECT * FROM users")
- records = cursor.fetchall()
- for record in records:
- self.tree.insert("", END, values=record)
- def search_records(self):
- search_term = self.search_var.get()
- for item in self.tree.get_children():
- self.tree.delete(item)
- cursor = self.conn.cursor()
- cursor.execute("""
- SELECT * FROM users
- WHERE name LIKE ? OR email LIKE ?
- """, (f'%{search_term}%', f'%{search_term}%'))
- records = cursor.fetchall()
- for record in records:
- self.tree.insert("", END, values=record)
- def create_table(self):
- cursor = self.conn.cursor()
- cursor.execute('''
- CREATE TABLE IF NOT EXISTS users (
- id INTEGER PRIMARY KEY AUTOINCREMENT,
- name TEXT NOT NULL,
- age INTEGER,
- email TEXT,
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
- )
- ''')
- self.conn.commit()
- def create_input_area(self):
- input_frame = ttk.LabelFrame(
- self.main_frame,
- text="数据输入",
- padding="10"
- )
- input_frame.pack(fill=X, pady=(0, 10))
- # 创建输入字段
- fields_frame = ttk.Frame(input_frame)
- fields_frame.pack(fill=X)
- # 姓名输入
- name_label = ttk.Label(fields_frame, text="姓名:")
- name_label.grid(row=0, column=0, padx=5, pady=5)
- self.name_entry = ttk.Entry(fields_frame)
- self.name_entry.grid(row=0, column=1, padx=5, pady=5)
- # 年龄输入
- age_label = ttk.Label(fields_frame, text="年龄:")
- age_label.grid(row=0, column=2, padx=5, pady=5)
- self.age_entry = ttk.Entry(fields_frame)
- self.age_entry.grid(row=0, column=3, padx=5, pady=5)
- # 邮箱输入
- email_label = ttk.Label(fields_frame, text="邮箱:")
- email_label.grid(row=0, column=4, padx=5, pady=5)
- self.email_entry = ttk.Entry(fields_frame)
- self.email_entry.grid(row=0, column=5, padx=5, pady=5)
- # 按钮区域
- btn_frame = ttk.Frame(input_frame)
- btn_frame.pack(fill=X, pady=(10, 0))
- ttk.Button(
- btn_frame,
- text="添加",
- command=self.add_record,
- bootstyle=SUCCESS
- ).pack(side=RIGHT, padx=5)
- ttk.Button(
- btn_frame,
- text="更新",
- command=self.update_record,
- bootstyle=WARNING
- ).pack(side=RIGHT, padx=5)
- ttk.Button(
- btn_frame,
- text="删除",
- command=self.delete_record,
- bootstyle=DANGER
- ).pack(side=RIGHT, padx=5)
- ttk.Button(
- btn_frame,
- text="清空",
- command=self.clear_entries,
- bootstyle=SECONDARY
- ).pack(side=RIGHT, padx=5)
- ttk.Button(
- btn_frame,
- text="MQTT",
- command=bt_13,
- bootstyle=PRIMARY
- ).pack(side=LEFT, padx=5)
- ttk.Button(
- btn_frame,
- text="发送siot1",
- command=bt_12,
- bootstyle=SUCCESS
- ).pack(side=LEFT, padx=5)
- ttk.Button(
- btn_frame,
- text="发送siot2",
- command=bt_11,
- bootstyle=DARK
- ).pack(side=LEFT, padx=5)
- def add_record(self):
- name = self.name_entry.get()
- age = self.age_entry.get()
- email = self.email_entry.get()
- if name and age and email:
- try:
- cursor = self.conn.cursor()
- cursor.execute(
- "INSERT INTO users (name, age, email) VALUES (?, ?, ?)",
- (name, age, email)
- )
- self.conn.commit()
- self.show_records()
- self.clear_entries()
- Messagebox.show_info("记录添加成功!", "成功")
- except Exception as e:
- Messagebox.show_error(f"添加失败:{str(e)}", "错误")
- else:
- Messagebox.show_warning("请填写所有字段!", "警告")
- def update_record(self):
- selected = self.tree.selection()
- if not selected:
- Messagebox.show_warning("请先选择一条记录!", "警告")
- return
- name = self.name_entry.get()
- age = self.age_entry.get()
- email = self.email_entry.get()
- if name and age and email:
- try:
- item_id = self.tree.item(selected)['values'][0]
- cursor = self.conn.cursor()
- cursor.execute(
- "UPDATE users SET name=?, age=?, email=? WHERE id=?",
- (name, age, email, item_id)
- )
- self.conn.commit()
- self.show_records()
- self.clear_entries()
- Messagebox.show_info("记录更新成功!", "成功")
- except Exception as e:
- Messagebox.show_error(f"更新失败:{str(e)}", "错误")
- else:
- Messagebox.show_warning("请填写所有字段!", "警告")
- def delete_record(self):
- selected = self.tree.selection()
- if not selected:
- Messagebox.show_warning("请先选择一条记录!", "警告")
- return
- if Messagebox.show_question("确定要删除这条记录吗?", "确认"):
- try:
- item_id = self.tree.item(selected)['values'][0]
- cursor = self.conn.cursor()
- cursor.execute("DELETE FROM users WHERE id=?", (item_id,))
- self.conn.commit()
- self.show_records()
- self.clear_entries()
- Messagebox.show_info("记录删除成功!", "成功")
- except Exception as e:
- Messagebox.show_error(f"删除失败:{str(e)}", "错误")
- if __name__ == "__main__":
- root = ttk.Window(themename="cyborg")
- # app = DatabaseManager(title="37创客sqlite数据库管理系统", root=root)
- # app = DatabaseManager(root,title="37创客sqlite数据库管理系统")
- app = UDB(root, "37创客sqlite数据库管理系统", 'tk.db')
- app.show_records()
- root.mainloop()
复制代码
EXT_CB.py
- # 事件回调函数
- def bt_11():
- print('bt_11')
- return '11'
- def bt_12():
- print('bt_12')
- return '12'
- def bt_13():
- return '13'
- def bt_14():
- return '14'
- def bt_21():
- return '21'
- def bt_22():
- return '22'
- def bt_23():
- return '23'
- def bt_24():
- return '24'
- def bt_31():
- return '31'
- def bt_32():
- return '32'
- def bt_33():
- return '33'
- def bt_34():
- return '34'
- def on_0_key():
- print('on_0_key')
- def on_1_key():
- print('on_1_key')
- def on_2_key():
- print('on_2_key')
- def on_3_key():
- print('on_3_key')
- def on_4_key():
- print('on_4_key')
- def on_5_key():
- print('on_5_key')
- def on_6_key():
- print('on_6_key')
- def on_7_key():
- print('on_7_key')
- def on_8_key():
- print('on_8_key')
- def on_9_key():
- print('on_9_key')
复制代码
appui.py
- # -*- coding: UTF-8 -*-
- import json
- from unihiker import GUI
- from EXT_XKB_BT import *
- from UI_VAR import *
- # 自定义函数
- def FUN_TuPian(MingZi, HanShu, WenJian, X, Y, W, H):
- MingZi = u_gui.draw_image(image=WenJian, x=X, y=Y)
- MingZi.config(h=H)
- MingZi.config(w=W)
- MingZi.config(onclick=HanShu)
- def GetKD(Jian):
- if (G_User == 1):
- return Dujson(G_CFG, Jian)
- else:
- return (str(G_QianZhui) + str(Dujson(G_CFG, Jian)))
- def FUN_GTuPian(_1, _2, _3, _4, X, Y, W, H, BT1, BT2, BT3, BT4):
- if bool(BT1):
- FUN_TuPian(_1, BT1, _1, X, Y, W, H)
- if bool(BT2):
- FUN_TuPian(_2, BT2, _2, ((W * 1) + X), Y, W, H)
- if bool(BT3):
- FUN_TuPian(_3, BT3, _3, ((W * 2) + X), Y, W, H)
- if bool(BT4):
- FUN_TuPian(_4, BT4, _4, ((W * 3) + X), Y, W, H)
- def Dujson(FJson, K):
- fileObj = open(FJson, "r", encoding="UTF8")
- pyObj = json.loads(fileObj.read())
- fileObj.close()
- G_KEY = pyObj
- return (G_KEY[K])
- def FUN_CaoZuo():
- FUN_GTuPian('ui/11.png', '遥控/前进.jpg', 'ui/fy.png', 'ui/yy.png', GetKD('x'), GetKD('y'), GetKD('宽'),
- GetKD('高'), False, 0, bt_11, bt_11)
- FUN_GTuPian(GetKD('bt_11'), GetKD('bt_12'), GetKD('bt_13'), GetKD('bt_14'), GetKD('x'), (GetKD('y') + 50),
- GetKD('宽'), GetKD('高'), bt_11, bt_12, bt_13, bt_14)
- FUN_GTuPian(GetKD('bt_21'), GetKD('bt_22'), GetKD('bt_23'), GetKD('bt_24'), GetKD('x'), (GetKD('y') + 90),
- GetKD('宽'), GetKD('高'), bt_21, bt_22, bt_23, bt_24)
- FUN_GTuPian(GetKD('bt_31'), GetKD('bt_32'), GetKD('bt_33'), GetKD('bt_34'), GetKD('x'), (GetKD('y') + 130),
- GetKD('宽'), GetKD('高'), bt_31, bt_32, bt_33, bt_34)
- def fun_TouBu(XiangMuMing, BeiJingTu):
- bj = u_gui.draw_image(image=BeiJingTu, x=0, y=0)
- 项目名字 = u_gui.draw_text(text=XiangMuMing, x=60, y=0, font_size=18, color="#000066")
- 动态 = u_gui.draw_text(text="实时消息", x=5, y=30, font_size=10, color="#FF0000")
- u_gui.draw_round_rect(x=2, y=75, w=235, h=130, r=5, width=3, color="#FF0000")
- def fun_DiBu():
- u_gui.draw_round_rect(x=0, y=210, w=235, h=105, r=5, width=3, color="#FF0000")
- msg = u_gui.draw_text(text="动态:项目启动", x=3, y=215, font_size=11, color="#0000FF")
- 编号 = u_gui.draw_text(text="编号:20241101", x=3, y=230, font_size=11, color="#FF0000")
- 情况 = u_gui.draw_text(text="情况:启动完成", x=3, y=245, font_size=11, color="#0000FF")
- 操作 = u_gui.draw_text(text="操作:初始化", x=3, y=260, font_size=11, color="#FF0000")
- 实时 = u_gui.draw_text(text="状态:init ok", x=3, y=275, font_size=11, color="#0000FF")
- 卡号 = u_gui.draw_text(text="卡号:20200808", x=3, y=295, font_size=11, color="#FF0000")
- TouXiang()
- def TouXiang():
- 头像 = u_gui.draw_image(image=GetKD('默认图片'), x=130, y=215)
- 头像.config(h=100)
- 头像.config(w=100)
- def UIinit(iname, ibg):
- fun_TouBu(iname, ibg)
- FUN_CaoZuo()
- fun_DiBu()
- # u_gui = GUI()
- # G_QianZhui = 'wt'
- # G_CFG = 'config.txt'
- # G_User = 1
- # fun_TouBu('项目名称', 'BG/bj.png')
- # FUN_CaoZuo()
- UIinit('37创客PY作品', 'BG/bj.png')
- while True:
- pass
复制代码
|
|