本文整理汇总了Python中tkinter.ttk.Treeview.focus方法的典型用法代码示例。如果您正苦于以下问题:Python Treeview.focus方法的具体用法?Python Treeview.focus怎么用?Python Treeview.focus使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tkinter.ttk.Treeview
的用法示例。
在下文中一共展示了Treeview.focus方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: DeviceToFrame
# 需要导入模块: from tkinter.ttk import Treeview [as 别名]
# 或者: from tkinter.ttk.Treeview import focus [as 别名]
class DeviceToFrame(CopilotInnerFrame):
def __init__(self, master, config, state):
super(DeviceToFrame, self).__init__(master, config)
self._state = state
if self._state.action == 'copy':
self._frame_lbl['text'] = 'Copy To Directory'
elif self._state.action == 'delete':
self._frame_lbl['text'] = 'Delete From Directory'
self._next_btn['command'] = self._next_cmd
self._tree = Treeview(self._master, columns=('size'))
self._tree.heading('size', text='Size')
self._tree.grid(row=1, column=0, columnspan=3, sticky='nsew')
self._tree.configure(yscrollcommand=self._sb.set)
self._sb['command'] = self._tree.yview
self._item_paths = {}
self._populate_tree(self._state.to_device.part().mount())
def _next_cmd(self):
cur_item = self._tree.focus()
cur_path = self._item_paths.get(cur_item, '')
if cur_path != '':
self._state.device_to_path = cur_path
self._new_state_window(CopyFileFrame, self._state)
def _populate_tree(self, tree_root):
self._item_paths = {}
def insert_path(tree, path, parent_id):
dirs = [e for e in scandir(path) if e.is_dir()]
dirs.sort(key=lambda e: e.name)
for d in dirs:
dir_name = d.name
dir_id = '{}-{}'.format(parent_id, dir_name)
dir_path = os.path.join(path, dir_name)
tree.insert(parent_id, 'end', dir_id, text=dir_name, tags=('dir'))
self._item_paths[dir_id] = dir_path
try:
insert_path(tree, dir_path, dir_id)
except:
pass
insert_path(self._tree, tree_root, '')
tree = self._tree
tree.tag_configure('dir', font=self._config.item_font)
tree.tag_configure('file', font=self._config.item_font)
tree.tag_configure('file_odd', background='light grey')
示例2: CopyFileFrame
# 需要导入模块: from tkinter.ttk import Treeview [as 别名]
# 或者: from tkinter.ttk.Treeview import focus [as 别名]
class CopyFileFrame(CopilotInnerFrame):
def __init__(self, master, config, state):
super(CopyFileFrame, self).__init__(master, config)
self._state = state
self._item_paths = {}
self._tree = Treeview(self._master, columns=('size'))
self._tree.heading('size', text='Size')
self._tree.grid(row=1, column=0, columnspan=3, sticky='nsew')
self._tree.configure(yscrollcommand=self._sb.set)
self._sb['command'] = self._tree.yview
if self._state.action == 'copy':
self._next_btn['text'] = 'Copy'
self._frame_lbl['text'] = 'Copy File'
self._populate_tree(self._config.file_root)
elif self._state.action == 'delete':
self._next_btn['text'] = 'Delete'
self._frame_lbl['text'] = 'Delete File'
self._populate_tree(self._state.to_device.part().mount())
self._next_btn['command'] = self._next_cmd
def _next_cmd(self):
if self._state.action == 'copy':
self._copy_file()
elif self._state.action == 'delete':
self._delete_file()
def _copy_file(self):
cur_item = self._tree.focus()
cur_path = self._item_paths.get(cur_item, '')
if cur_path != '':
new_path = os.path.join(self._state.device_to_path, os.path.basename(cur_path))
try:
if os.path.exists(new_path):
if ConfirmFrame.show(
self._master, self._config,
'The file already exists in the destination.\n'
'Would you like to overwrite it?',
'Yes', 'No'
):
shutil.copyfile(cur_path, new_path)
else:
shutil.copyfile(cur_path, new_path)
except PermissionError:
OkFrame.show(
self._master, self._config,
'Error copying file:\n\nInvalid permissions'
)
except Exception as e:
OkFrame.show(
self._master, self._config,
'An error occurred while copying the file:\n\n{}'.format(e)
)
def _delete_file(self):
cur_item = self._tree.focus()
cur_path = self._item_paths.get(cur_item, '')
if cur_path != '':
disp_path = cur_path[len(self._state.to_device.part().mount()):]
try:
if ConfirmFrame.show(
self._master, self._config,
'Are you sure you\'d like to delete this file?\n{}'.format(disp_path),
'Yes', 'No'
):
os.remove(cur_path)
self._tree.delete(self._tree.focus())
except PermissionError:
OkFrame.show(
self._master, self._config,
'Error deleting file:\n\nInvalid permissions'
)
except Exception as e:
OkFrame.show(
self._master, self._config,
'An error occurred while deleting the file:\n\n{}'.format(e)
)
def _populate_tree(self, tree_root):
self._item_paths = {}
def insert_path(tree, path, parent_id):
dirs = [e for e in scandir(path) if e.is_dir()]
dirs.sort(key=lambda e: e.name)
for d in dirs:
dir_name = d.name
dir_id = '{}-{}'.format(parent_id, dir_name)
dir_path = os.path.join(path, dir_name)
tree.insert(parent_id, 'end', dir_id, text=dir_name, tags=('dir'))
try:
insert_path(tree, dir_path, dir_id)
except:
pass
files = [e for e in scandir(path) if e.is_file()]
#.........这里部分代码省略.........
示例3: NameView
# 需要导入模块: from tkinter.ttk import Treeview [as 别名]
# 或者: from tkinter.ttk.Treeview import focus [as 别名]
class NameView(object):
"""Shows a treeview of unique names."""
def __init__(self, master, names):
self.widget = Frame(master)
self._tree = Treeview(self.widget, columns='name')
self._tree.grid(row=0,column=0, sticky=(N,S,W,E))
self._tree.view = self
self.widget.columnconfigure(0, weight=1)
self.widget.rowconfigure(0,weight=1)
self._tree.column('name', width=50)
self._tree['show'] = 'tree'
actions = {'edit': lambda e: self.edit(),
'search': lambda e: self.search(),
'focus_next': lambda e: self.focus_next(),
'focus_prev': lambda e: self.focus_prev(),
'select': lambda e: self._tree.selection_toggle(self._tree.focus()),
'clear_selection': lambda e: self._tree.selection_set([])
}
kb.make_bindings(kb.tagview, actions, self._tree.bind)
self._iids = dict()
self._names = dict()
logger.debug('Names: %s', names)
self.widget.focus_set = self._tree.focus_set
for name in sorted(names):
iid = self._tree.insert('', 'end', text=name)
self._names[iid] = name
self._iids[name] = iid
self._scroll = Scrollbar(self.widget, command=self._tree.yview)
self._tree['yscrollcommand'] = self._scroll.set
self._scroll.grid(row=0, column=1, sticky=(N, S))
self.widget.columnconfigure(1, weight=0)
def selection(self):
logger.debug('Selection: %s', self._tree.selection())
return [self._names[iid] for iid in self._tree.selection()]
def edit(self):
self._tree.event_generate('<<NameViewEdit>>')
def search(self):
if len(self._tree.selection()) == 0:
self._tree.selection_add(self._tree.focus())
self._tree.event_generate('<<NameViewSearch>>')
def append(self, names):
logger.debug('Append names: %s', names)
for name in names:
if name not in self._names.values():
iid = self._tree.insert('', 'end', text=name)
self._names[iid] = name
self._iids[name] = iid
def delete(self, name):
self._tree.delete(self._iids[name])
del self._names[self._iids[name]]
del self._iids[name]
def _focus(self, iid):
self._tree.focus(iid)
self._tree.see(iid)
def focus_next(self):
cur_iid = self._tree.focus()
next_iid = self._tree.next(cur_iid)
if next_iid == '':
iids = self._tree.get_children()
next_iid = iids[0]
self._focus(next_iid)
def focus_prev(self):
cur_iid = self._tree.focus()
prev_iid = self._tree.prev(cur_iid)
if prev_iid == '':
iids = self._tree.get_children()
prev_iid = iids[-1]
self._focus(prev_iid)
def jump_to(self, name):
try:
iid = self._iids[name]
self._focus(iid)
except KeyError:
pass
def get_names(self):
return tuple(self._names.values())
def set(self, names):
self._tree.delete(*self._iids.values())
self._iids.clear()
self._names.clear()
for name in sorted(names):
iid = self._tree.insert('', 'end', text=name)
self._names[iid] = name
self._iids[name] = iid