Python ttk.treeview ¿Cómo usar botones para cambiar directamente el valor de una celda sin seleccionarla?
desde tkinter importar cuadro de mensaje
desde tkinter importar ttk
desde tkinter importar *
clase datos de producto (objeto):
def __init__(self):
Self.root = tk. Tk() #Inicialización
Self.root.title ('arca de datos') #Modificar el nombre del formulario.
self .root .resizable(ancho = falso, alto = falso) #El ancho y el alto de la interfaz del formulario son ajustables.
Self.root.geometry('900x500') #Tamaño predeterminado del formulario
self.canvas = tk. Canvas(self.root, ancho='1200', alto='1200')
self.image_file = tk. PhotoImage(file = "beijing.gif ")
self .imagen = self .create_image(0, 0, Anchor='nw ', image=self.image_file)
. self.canvas.pack(side='top ')
#Defina una variable para monitorear el número de filas eliminadas.
self.delhang=0
#Agregar tabla
Columnas = ("tipo de campo", "longitud del campo", "decimales", "único" , "nombre del campo")
self.treeview = ttk. Treeview(self.root, height=18, show="headings ", columns=columns)
#Representa una columna y no la muestra.
Self.treeview.column("tipo de campo", ancho=100, ancla='centro ')
Self.treeview.column("longitud de campo", ancho=100, ancla='centro ').
Self.treeview.column("Lugares decimales", ancho=100, anclaje='centro ')
Self.treeview.column("Es único", ancho=100 , ancla='centro ').
Self.treeview.column("nombre de campo", ancho=100, anclaje='centro ')
#Mostrar título
Self.treeview.heading ("tipo de campo", texto= "tipo de campo")
Self.treeview.heading("longitud del campo", texto= "longitud del campo")
Self.treeview.heading ("Lugares decimales", text= "Lugares decimales")
Self.treeview.heading("Es único", text= "Es único")
Self.treeview .heading ("nombre del campo", texto = "nombre del campo")
#Escribir datos
self.type = ['letra']
self. largo = ['10']
self.decimal = ['0']
Self.solo = ['Sí']
Self.nombre = [ 'ID de recurso']
Para el rango I(min(len(self. type), len(self.long), len(self.decimal), len(self.only), len( self.name))):
self.treeview.insert(' ', I, valores=(self.type[i], self.long[i], self.decimal[i], self .only[i], self.name[i]))
#Haga doble clic en el botón izquierdo para ingresar al estado de edición.
self . treeview . bind('<double-1>', self.set_cell_value)
#Agregar un botón "Agregar campo"
self . agregar _ campo _ botón = botón ttk (self. root, texto = 'agregar campo', ancho = 10, comando = self. nueva fila).
#Agregar un botón de "eliminar campo"
self . del _ field _ button = ttk . = self.del fila).
#Agregar un botón "Borrar campo"
self . delall _ field _ button = ttk button(self . root, text = 'Vaciar campo', ancho = 10, comando. = self.delall).
def buju(self):
#Agregar botón
self . agregar _ campo _ botón (x = 780, y=40) p>
Botón#Eliminar
self .del_campo_botón(x=780, y=80)
Botón#Borrar
self .delall _ campo _ botón .place(x = 780, y=120)
#Diseño de tabla
self.treeview.place(x=0, y=2, ancho =750, altura=200)
def set_cell_value(self, event):
Para elementos en self.treeview.selection():
# elemento = I001
item _ text = self . treeview . item(item, "values")
# print(item_text[0:2]) # Genera el valor de la fila seleccionada.
columna = self . vista de árbol . identificar _ columna(evento . x)# columna
fila = self .vista de árbol .
cn = int(cadena(columna)). reemplazar(' # ', ''))
rn = int(str(fila)). reemplazar('I ', '')-self.delhang
entryedit = Texto(self.root, ancho=10, alto=1)
entrada editar (x =. 2(cn-1)* 150, y=6 + rn * 20)
def saveit():
self.treeview.set(elemento, columna=columna, valor =entryedit.get(0.0, " end "))
entryedit.destroy()
okb.destroy()
okb = ttk.
botón(self.root, text='OK ', ancho=4, comando=saveedit)
okb . )
def newrow(self):
Self.type.append('Tipo de campo de entrada')
Self.long.append('Longitud del campo de entrada ')
Self.decimal.append('Ingrese decimales')
Self.only.append('¿Es único')
Self.name append('Ingrese la longitud del campo')
self.treeview.insert(' ', len(self.type) - 1, valores = (self . type[len(self . type) )-1] ,
self . largo[len(self . tipo)-1],
self . decimal[len(self . tipo)-1],
self solo[len(self . type)-1],
self .nombre[len(self . type)-1]))
self.treeview .update()
def delrow(self):
elementos _ seleccionados = self . treeview()
Para elementos en elementos seleccionados:
p>
self.treeview.delete(items)
self.delhang +=1
def delall(self):
todos los _ items = self .treeview .get_children()
Para elementos en todos los proyectos:
self.treeview.delete(item)
self.delhang =0
p>def main():
#Objeto de inicialización
p = productdata()
#Diseño
Ubicación
tk.mainloop()
if __name__=="__main__ ":
main()