当前位置:   article > 正文

PYTHON之tkinter表格载入数据,双击删除行数据_python tkinter 表格

python tkinter 表格

在使用Tkinter设计窗体时,以下是一些重要的技术点:

  1. 窗口和组件的创建:Tkinter中使用Tk()函数创建主窗口,然后可以使用诸如LabelButtonEntry等组件创建不同的控件。

  2. 布局管理器:Tkinter提供了几种布局管理器,如pack()grid()place()。它们用于定位和排列窗口部件,允许用户管理窗口的外观和布局。

  3. 事件处理:通过bind()方法将事件(比如鼠标点击、键盘输入)与特定的函数关联起来。这样,当事件发生时,相应的函数就会被调用。

  4. 窗口的关闭处理:确保在关闭窗口时,能够处理关闭事件,例如通过protocol()方法处理关闭窗口时的操作。

  5. 菜单和工具栏:创建菜单和工具栏来提供应用程序的功能和选项。Menu类用于创建菜单,可以添加各种菜单项。

  6. 图形和图像处理:Tkinter也支持图形和图像处理。你可以使用Canvas创建基本图形,也可以显示图片,使用PIL库或PhotoImage类处理图像。

  7. 数据输入和验证:使用Entry控件获取用户输入,同时需要对用户输入的数据进行验证和处理。

  8. 弹窗和对话框:在需要显示信息或获取确认时,可以使用messagebox模块显示弹窗、filedialog模块打开文件对话框等。

这些技术点提供了Tkinter窗体设计的基本工具和功能,允许用户创建功能丰富的GUI应用程序。通过掌握这些技术点,你可以构建出适合特定需求的用户界面。

  1. import tkinter as tk
  2. from tkinter import ttk
  3. import sqlite3
  4. def load_data():
  5. conn = sqlite3.connect('usersexample.db')
  6. cursor = conn.cursor()
  7. cursor.execute("SELECT * FROM users")
  8. data = cursor.fetchall()
  9. for row in data:
  10. tree.insert('', 'end', values=row)
  11. print(row[0],row[1])
  12. conn.close()
  13. def delete_data(event):
  14. selected_item = tree.selection()
  15. if selected_item:
  16. for item in selected_item:
  17. try:
  18. selected_id = tree.item(item, 'values')[0] # 假设第一列是唯一ID
  19. tree.delete(item)
  20. conn = sqlite3.connect('usersexample.db')
  21. cursor = conn.cursor()
  22. cursor.execute("DELETE FROM users WHERE id=?", (selected_id,))
  23. conn.commit()
  24. conn.close()
  25. except Exception as e:
  26. print("Error:", e)
  27. tree.delete(*tree.get_children()) # 清空 Treeview 中的数据
  28. load_data() # 重新加载最新数据
  29. root = tk.Tk()
  30. root.title("Database Data Display")
  31. columns = ('ID', 'Column 1', 'Column 2')
  32. tree = ttk.Treeview(root, columns=columns, show='headings')
  33. for col in columns:
  34. tree.heading(col, text=col)
  35. tree.column(col, width=100)
  36. tree.pack()
  37. load_data()
  38. tree.bind('<Double-1>', delete_data)
  39. root.mainloop()

在Python中,*(星号)在这种上下文下被称为解包操作符。它的作用是将列表、元组或其他可迭代对象中的元素解包为单独的参数。

tree.get_children() 的上下文中,这个方法返回一个列表,包含Treeview中所有子项的标识符。当使用*tree.get_children()时,星号将这个列表解包,将列表中的元素作为独立的参数传递给 tree.delete() 方法。

举个例子:

假设tree.get_children() 返回一个列表:['item1', 'item2', 'item3']。当你使用tree.delete(*tree.get_children())时,它实际上相当于tree.delete('item1', 'item2', 'item3'),即用返回的列表中的元素作为独立的参数传递给tree.delete()方法。

-

  1. import tkinter as tk
  2. from tkinter import filedialog
  3. def open_file():
  4. file_path = filedialog.askopenfilename()
  5. if file_path:
  6. with open(file_path, 'r', encoding='utf-8', errors='ignore') as file:
  7. content = file.read()
  8. text_box.delete(1.0, tk.END) # 清空文本框
  9. text_box.insert(tk.END, content) # 在文本框中显示文件内容
  10. root = tk.Tk()
  11. root.title("Display Text File")
  12. text_box = tk.Text(root)
  13. text_box.pack(fill=tk.BOTH, expand=True)
  14. open_button = tk.Button(root, text="Open File", command=open_file)
  15. open_button.pack()
  16. root.mainloop()

 使用 encoding='utf-8', errors='ignore' 参数来指定打开文件时使用的编码格式为utf-8errors='ignore'的作用是忽略在解码文件时遇到的错误,这样可以避免因文件编码问题导致的异常。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/980535?site
推荐阅读
相关标签
  

闽ICP备14008679号