main.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/python3
  2. import csv
  3. import tkinter as tk
  4. import tkinter.ttk as ttk
  5. class App(tk.Tk):
  6. def __init__(self, path):
  7. super().__init__()
  8. self.title("Ttk Treeview")
  9. columns = ("#1", "#2", "#3")
  10. self.tree = ttk.Treeview(self, show="headings", columns=columns)
  11. self.tree.heading("#1", text="Фамилия")
  12. self.tree.heading("#2", text="Имя")
  13. self.tree.heading("#3", text="Почта")
  14. ysb = ttk.Scrollbar(self, orient=tk.VERTICAL, command=self.tree.yview)
  15. self.tree.configure(yscroll=ysb.set)
  16. with open("../lesson_13/contacts.csv", newline="") as f:
  17. for contact in csv.reader(f):
  18. self.tree.insert("", tk.END, values=contact)
  19. self.tree.bind("<<TreeviewSelect>>", self.print_selection)
  20. self.tree.grid(row=0, column=0)
  21. ysb.grid(row=0, column=1, sticky=tk.N + tk.S)
  22. self.rowconfigure(0, weight=1)
  23. self.columnconfigure(0, weight=1)
  24. def print_selection(self, event):
  25. for selection in self.tree.selection():
  26. item = self.tree.item(selection)
  27. last_name, first_name, email = item["values"][0:3]
  28. text = "Выбор: {}, {} <{}>"
  29. print(text.format(last_name, first_name, email))
  30. if __name__ == "__main__":
  31. app = App(path=".")
  32. app.mainloop()