Найти - Пользователи
Полная версия: Tkinter: Treeview управление с клавиатуры
Начало » GUI » Tkinter: Treeview управление с клавиатуры
1
Cover Story
Всем доброго!
Что-то не получается управлять с клавиатуры перемещением по строкам в Treeview. Не могу понять как определить последний index или item.
Помогите!
#-*- coding:utf-8 -*-
from Tkinter import *
import sqlite3
import ttk

def insdown(event):

index = tablegas.index(tablegas.selection()[0])
selitem = tablegas.selection()[0]
if tablegas.exists(tablegas.next(selitem)):
selitem = tablegas.next(selitem)
index = index+1
print "Next", tablegas.exists(tablegas.next(selitem))
else:
index = index
selitem = tablegas.focus(selitem)
print index

tools_text.config(state=NORMAL)
tools_text.delete(1.0, END)
tools_text.insert(END, tablegas.item(selitem, option='values')[9])
tools_text.config(state=DISABLED)
komment_text.config(state=NORMAL)
komment_text.delete(1.0, END)
komment_text.insert(END, tablegas.item(selitem, option='values')[10])
komment_text.config(state=DISABLED)

# print tablegas.focus()

def insup(event):
index = tablegas.index(tablegas.selection()[0])
selitem = tablegas.selection()[0]
if int(index)<>0:
seltext = index-1
selitem = tablegas.prev(selitem)
if int(index)==0:
seltext = index
print seltext
# print tablegas.selection(seltext)[0]
tools_text.config(state=NORMAL)
tools_text.delete(1.0, END)
tools_text.insert(END, tablegas.item(selitem, option='values')[9])
tools_text.config(state=DISABLED)
komment_text.config(state=NORMAL)
komment_text.delete(1.0, END)
komment_text.insert(END, tablegas.item(selitem, option='values')[10])
komment_text.config(state=DISABLED)

def insrec(event):
# Вывод Item строки
print tablegas.selection()[0]
# Вывод ID строки
print tablegas.index(tablegas.selection()[0])
# Вывод содержимого строки
# print tablegas.item(tablegas.selection()[0], option='values')[9]
tools_text.config(state=NORMAL)
tools_text.delete(1.0, END)
tools_text.insert(END, tablegas.item(tablegas.selection()[0], option='values')[9])
tools_text.config(state=DISABLED)
komment_text.config(state=NORMAL)
komment_text.delete(1.0, END)
komment_text.insert(END, tablegas.item(tablegas.selection()[0], option='values')[10])
komment_text.config(state=DISABLED)

c = sqlite3.connect(database=r"avto.sdb")
cu = c.cursor()

root = Tk()
root.wm_title("multicolumn ListBox")
f = ttk.Frame(root)
f.pack()

fr = ttk.Frame(f)
fr.grid(row=0, column=0, columnspan=2)
fr_tools = ttk.Frame(f)
fr_tools.grid(row=1, column=0)
fr_komment = ttk.Frame(f)
fr_komment.grid(row=1, column=1, pady=5)


# Определяем таблицу Treeview
tablegas = ttk.Treeview(fr, show='headings', selectmode='browse', height=10)

# Задаем заголовки колонкам
tablegas["columns"]=("ID","date","avtopart","namepart","numpart","mileage","brand","cost","sto","tools","komment")
# Выводим необходимые столбцы
tablegas["displaycolumns"]=("date","avtopart","namepart","numpart","mileage","brand","cost","sto")

tablegas.heading("ID", text=u"ID", anchor='w')
tablegas.heading("date", text=u"Дата", anchor='w')
tablegas.heading("avtopart", text=u"Часть авто", anchor='w')
tablegas.heading("namepart", text=u"Наименование ремонта", anchor='w')
tablegas.heading("numpart", text=u"Номер детали", anchor='w')
tablegas.heading("mileage", text=u"Пробег", anchor='w')
tablegas.heading("brand", text=u"Фирма", anchor='w')
tablegas.heading("cost", text=u"Стоимость ремонта", anchor='w')
tablegas.heading("sto", text=u"Место ремонта", anchor='w')
tablegas.heading("tools", text=u"Инструмент", anchor='w')
tablegas.heading("komment", text=u"Комментарии", anchor='w')

tablegas.column("ID", stretch=0, width=70)
tablegas.column("date", stretch=0, width=70)
tablegas.column("avtopart", stretch=0, width=90)
tablegas.column("namepart", stretch=0, width=90)
tablegas.column("numpart", stretch=0, width=90)
tablegas.column("mileage", stretch=0, width=80)
tablegas.column("brand", stretch=0, width=100)
tablegas.column("cost", stretch=0, width=90)
tablegas.column("sto", stretch=0, width=80)
tablegas.column("tools", stretch=0, width=100)
tablegas.column("komment", stretch=0, width=100)


sel = 'SELECT repair.ID, date, avtopart.avtoparts, namerepair, numberparts, mileage, brand.brands, cost, sto.place, tools, komment FROM repair, avtopart, brand, sto WHERE avtopart.ID = repair.avtoparts and brand.ID = repair.brand and sto.ID = repair.sto'
cu.execute(sel)
for item in cu.fetchall():
tablegas.insert('', 'end', values=(item[0], item[1], item[2], item[3], item[4], item[5], item[6], item[7], item[8], item[9], item[10]))

scroll = ttk.Scrollbar(fr)
tablegas.config(yscrollcommand=scroll.set)
scroll.config(command=tablegas)
scroll.grid(row=0, column=1, sticky=N+S)

tablegas.grid(column=0, row=0, sticky='nsew')
tablegas.bind('<ButtonRelease>', insrec)
tablegas.bind('<Down>', insdown)
tablegas.bind('<Up>', insup)

tools_lab = ttk.Label(fr_tools, text=u"Используемые инструменты")
tools_lab.grid(row=1, column=0, columnspan=2, pady=3)
tools_text = Text(fr_tools, height=5, width=20, font="6")
scrolltools = ttk.Scrollbar(fr_tools)
tools_text.config(yscrollcommand=scrolltools.set, state=DISABLED)
scrolltools.config(command=tools_text)
tools_text.grid(row=2, column=0)
scrolltools.grid(row=2, column=1, sticky=N+S)

komment_lab = ttk.Label(fr_komment, text=u"Комментарии")
komment_lab.grid(row=1, column=0, columnspan=2, pady=3)
komment_text = Text(fr_komment, height=5, width=20, font="6")
scrollkomment = ttk.Scrollbar(fr_komment)
komment_text.config(yscrollcommand=scrollkomment.set, state=DISABLED)
scrollkomment.config(command=tools_text)
komment_text.grid(row=2, column=0)
scrollkomment.grid(row=2, column=1, sticky=N+S)

Style = ttk.Style()
ttk.Style.theme_use(Style, "clam")
mainloop()
c.close()
balu
1) Читать тут - http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm Кроме того полезно будет http://effbot.org/tkinterbook/widget.htm
2) Потом тут - http://www.tcl.tk/man/tcl/TkCmd/ttk_treeview.htm#M46
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB