Wirror 发表于 2021-7-15 16:25:08

tkinter treeview

tkinter treeview 双击表格的某一个(不是某一行)数据,如何让其变为Entry,然后输入内容编辑这一个数据并保存

xiaosi4081 发表于 2021-7-15 16:27:47

http://cn.voidcc.com/question/p-rwfigdqq-mq.html

Wirror 发表于 2021-7-15 16:40:48

xiaosi4081 发表于 2021-7-15 16:27
http://cn.voidcc.com/question/p-rwfigdqq-mq.html

这是pyqt,可是我用的tkinter

xiaosi4081 发表于 2021-7-15 16:59:32

Wirror 发表于 2021-7-15 16:40
这是pyqt,可是我用的tkinter

这个呢:
https://kb.kaifa99.com/others/post_14232521

阿奇_o 发表于 2021-7-16 00:55:38

我找到一种解决办法,虽然貌似有点繁琐,你自己看着办吧。。
from tkinter import *
from tkinter import ttk

class App(Tk):
    def __init__(self, geo) -> None:
      super().__init__()
      self.geometry(geo)

      self.tree = ttk.Treeview(self, )
      self.tree.pack()
      self.tree['columns'] = (1, 2, 3)
      self.tree['show'] = 'headings'
      self.tree.column(1, width=90, anchor='c')
      self.tree.column(2, width=90, anchor='se')
      self.tree.column(3, width=90, anchor='se')
      self.tree.heading(1, text='Name')
      self.tree.heading(2, text='Sex')
      self.tree.heading(3, text='Age')
      self.iid = self.tree.insert("", 'end', text ="item_1", values =("Bill", "M", "25"),
                         tags=('d_click', 'test'))# 为每条item添加tags,然后利用该tags来触发事件,如双击
                        #https://tkdocs.com/tutorial/tree.html#appearance
      self.tree.tag_bind('d_click', '<Double-1>', lambda e:self.entry_popup(e))

      self.tree.insert("", 'end', text ="item_2", values =("Caty", "M", "23"),
                        tags=('d_click',)) # 同样给它打上一个“双击”的标签

      # self.tree.bind("<Double-1>", lambda e: self.entry_popup(e))
      # self.tree.bind("<Double-1>", lambda e:self.d_click(e))

    def entry_popup(self, ev):
      self.tl = Toplevel(self.tree, )
      self.tl.title('输入弹窗...')
      self.tl.geometry(f"300x40+{ev.x_root}+{ev.y_root}")
      # self.tl.overrideredirect(True)
      self.ent = Entry(self.tl, borderwidth=2)
      self.ent.focus()
      self.ent.pack()
      curItem = self.tree.focus()
      item_val = self.tree.item(curItem)
      print(item_val)
      self.ent.insert(END, ','.join(map(str, item_val['values'])))
      self.ent.bind("<Return>", lambda e:self._input(self.ent.get()))
   
    def _input(self, entry_text):
      # print(entry_text)
      self.after(10, self.tl.destroy)
      focused = self.tree.focus()
      print(focused)
      x = entry_text.split(',')# 根据 item的特点,输入逗号分隔的 name,sex,age
      self.tree.insert('', str(focused), values=x)
      self.tree.delete(focused)# 删掉原有的

      # https://stackoverflow.com/questions/44197943/python-3-ttk-how-to-change-value-of-the-specific-cell

win = App("500x300+300+100")
win.mainloop()

# https://stackoverflow.com/questions/18562123/how-to-make-ttk-treeviews-rows-editable

# https://stackoverflow.com/questions/30614279/python-tkinter-tree-get-selected-item-values
页: [1]
查看完整版本: tkinter treeview