17.2 Métodos
método | describir |
---|---|
agregar (hijo, ** opciones) | Agregar subcomponentes. child: opciones de identificación de componentes secundarios : método para agregar componentes secundarios. vea la tabla de abajo |
configuración() | Asistir descripción de la propiedad. |
olvidar (niño) | |
eliminar (niño) | Eliminar una ventana secundaria. |
Niño | ID de la ventana secundaria |
identificar (x, y) | Dada una coordenada (x, y), devuelve el nombre del elemento en esa coordenada. Si la coordenada está en un componente secundario, devuelve una cadena vacía . Si la coordenada está en una línea dividida, devuelve una tupla de 2 (n, 'sash '), si n es 0, significa la primera línea divisoria. Si la coordenada está en el mango, devuelve una tupla de 2 (n, 'mango'), y n es 0, lo que significa que el primer mango |
panecget(hijo, opción) | Obtener el valor de la subventana opcional |
paneconfig(hijo, **opciones) paneconfigure(hijo, **opciones) |
Establezca las propiedades de la ventana secundaria. Para la definición de atributos específicos, participe en la función add(child, **options) |
paneles() | devolver la lista de la subventana |
proxy_coord() | Obtener las coordenadas del agente |
proxy_olvidar() | eliminar agente |
lugar_proxy(x, y) | colocar un agente en las coordenadas (x,y) |
sash_coord(índice) | Obtener las coordenadas de posición de la línea divisoria especificada por índice |
sash_mark(índice, x, y) | Registre la posición del ratón en (x,y). |
sash_place(índice, x, y) | Mueva la línea divisoria a la posición especificada por las coordenadas (x,y). |
Agregar método de subcomponente
método | ilustrar |
---|---|
después | Agrega un nuevo subcomponente después del subcomponente especificado |
antes | Agrega un nuevo subcomponente antes del subcomponente especificado |
altura | Especifica la altura del componente secundario. |
tamaño mínimo | Esta opción controla el valor que el panel no debe ser inferior a. Si orient=HORIZONTAL, el ancho del panel no debe ser inferior al valor de esta opción. Si orient=VERTICAL, la altura del panel no debe ser inferior a el valor de esta opción. |
Padx | Especifica el espaciado horizontal de los componentes secundarios. |
Arrozal | Especifica el espaciado vertical de los componentes secundarios. |
pegajoso | Cuando el tamaño del panel es mayor que el del subcomponente, esta opción especifica la posición del subcomponente en el panel. Los valores opcionales son: E, S, W, N (sur, este, oeste, norte) y sus valores combinados |
ancho | Especifica el ancho del componente secundario. |
17.2.1 agregar (hijo, kw) | |
Agregar subcomponentes. Después de que el panel defina el estilo y el tamaño de la partición, use el método add() para agregar los componentes específicos en cada subpanel. Los parámetros para agregar subcomponentes son: | |
(1) Después | |
Agregue después del control secundario especificado por después. Por ejemplo: | |
derecha=tk.Label(panel,texto='derecha') | |
izquierda = tk.Label (panel, texto = 'izquierda') | |
panel.add(derecha,después=izquierda) | |
Esta declaración significa que el subcontrol derecho se agrega después del subcontrol izquierdo. Si los paneles están dispuestos horizontalmente, el control derecho está a la derecha del control izquierdo. | |
(2) antes | |
Agregue antes del control secundario especificado por before. Por ejemplo: | |
derecha=tk.Label(panel,texto='derecha') | |
izquierda = tk.Label (panel, texto = 'izquierda') | |
panel.add(derecha,antes=izquierda) | |
También es un panel distribuido horizontalmente En este momento, el control derecho está en el lado izquierdo del control izquierdo. Porque se especifica que el control derecho se inserta en el panel antes que el control izquierdo. | |
(3) altura | |
Establece la altura del control secundario. Luego, algunos subcontroles tienen una altura, como Texto, etc., ¿cuál prevalecerá? La respuesta es que la altura del control es mayor que eso. En otras palabras, si la altura del subcontrol Texto es mayor que la altura especificada por altura, se utilizará Texto. Si el valor de altura es grande (si las unidades son inconsistentes, se convertirá al valor de altura de la misma unidad para comparar), entonces use el valor definido por altura. | |
(4) tamaño mínimo | |
Especifica el valor numérico para el panel más pequeño. La configuración del panel no será menor que este valor. |
import tkinter as tk
from tkinter import ttk
root=tk.Tk()
root.geometry('320x240')
b1=tk.PanedWindow(showhandle=True,
sashrelief=tk.SUNKEN,
width=240,sashwidth=20)
left=tk.Text(b1,height=5,width=5,bg='red')
b1.add(left,minsize=140)
right=tk.Text(b1,height=5,width=5)
b1.add(right)
b1.pack()
root.mainloop()
结果:
图一是没有使用minsize参数的情况,图二是使用了minsize的情况,可以看见是有不同的。right子控件使用了minsize之后,由于要保证最小的宽度是150个像素,明显比没有使用的时候要宽了。对于水平分布的窗格,minsize是指的宽度,而对于垂直分隔的窗格,minsize指的是高度。使用的时候要注意区分窗格的分布。
(5)padx
设置水平方向上的子控件的内边距值。
import tkinter as tk
from tkinter import ttk
root=tk.Tk()
root.geometry('320x240')
b1=tk.PanedWindow(showhandle=True,
sashrelief=tk.SUNKEN,width=240,
sashwidth=20,bg='blue')
left=tk.Text(b1,height=5,width=5)
b1.add(left,minsize=40)
right=tk.Text(b1,height=5,width=5,bg='red')
b1.add(right,padx=10)
b1.pack()
root.mainloop()
结果:
说明:padx=10,可以发现在右侧的子控件的两边有蓝色的边,而左侧的子控件没有使用padx,就没有明显的蓝色的边。
(6)pady
垂直方向的内边距值。用法参见padx。
(7)sticky
sticky的用法与grid布局管理器的用法是一致的。如果子控件比窗格要小的话,就可以使用sticky来放置子控件到相应的方位上。具体的用法参见第三章的grid布局管理器。
(8)width
定义子控件的宽度。与height用法类似。
17.2.2 config()
动态改变窗格的属性。属性见17.1节的定义。
17.2.3 forget(child) 和remove(child)
移除一个窗格。
import tkinter as tk
from tkinter import ttk
root=tk.Tk()
root.geometry('320x240')
b1=tk.PanedWindow(showhandle=True,
sashrelief=tk.SUNKEN,
width=240,sashwidth=20,bg='blue')
left=tk.Text(b1,height=5,width=5)
b1.add(left,minsize=40)
right=tk.Text(b1,height=5,width=5,bg='red')
b1.add(right,padx=10)
b1.pack()
def remove():
b1.remove(right)
b2=tk.Button(root,text='移除窗格',command=remove)
b2.pack()
root.mainloop()
结果:
17.2.4 identify(x, y)
返回给定坐标点的处于什么窗格的部件上:
(1)空字符串
表示位于子控件上
(2)(n,‘sash’)
表示位于分隔条上。n表示第几个分隔条,从0开始计数。
(3)(n,’handle’)
表示位于手柄上。n表示第几个手柄,从0开始计数
import tkinter as tk
from tkinter import ttk
root=tk.Tk()
root.geometry('320x240')
b1=tk.PanedWindow(showhandle=True,
sashrelief=tk.SUNKEN,
sashwidth=90,width=240,bg='blue')
left=tk.Text(b1,height=5,width=5)
b1.add(left,minsize=40)
right=tk.Text(b1,height=5,width=5,bg='red')
b1.add(right,padx=10)
b1.pack()
b3=tk.Label(root)
b4=tk.Entry(root,width=5)
b5=tk.Entry(root,width=5)
def identify():
r=b1.identify(b4.get(),b5.get())
if r == '':
b3['text']='子控件'
else:
b3['text']=str(r)
b2=tk.Button(root,text='identify',command=identify)
b2.pack()
b4.pack(side='left')
b5.pack(side='right')
b3.pack()
def b(event):
print(event)
root.bind('<Button-1>',b)
root.mainloop()
结果:
说明:先点中分隔条,根据在命令行窗口得到的鼠标位置,把x坐标值输入左边的输入框,y坐标值输入右边的输入框,点击按钮后,就可以得到相应的结果了。手柄和子控件同样的方法。
17.2.5 panecget(child, option)
获取子窗格的属性,属性的定义在17.2.1中已经详细介绍了。
import tkinter as tk
from tkinter import ttk
root=tk.Tk()
root.geometry('320x240')
b1=tk.PanedWindow(showhandle=True,
sashrelief=tk.SUNKEN,
sashwidth=1,width=240,bg='blue')
left=tk.Text(b1,height=5,width=5)
b1.add(left,minsize=40)
right=tk.Text(b1,height=5,width=5,bg='red')
b1.add(right,padx=10,before=left)
b1.pack()
b3=tk.Label(root)
b4=tk.Entry(root)
def cget():
b3['text']=b1.panecget(right,b4.get())
b2=tk.Button(root,text='cget',command=cget)
b2.pack()
b4.pack()
b3.pack()
root.mainloop()
结果:
17.2.6 paneconfigure(child, **options)和paneconfig(child, **options)
import tkinter as tk
from tkinter import ttk
root=tk.Tk()
root.geometry('320x240')
b1=tk.PanedWindow(showhandle=True,
sashrelief=tk.SUNKEN,sashwidth=1,
width=240,bg='blue')
left=tk.Text(b1,height=5,width=5)
b1.add(left,minsize=40)
right=tk.Text(b1,height=5,width=5,bg='red')
b1.add(right,padx=10,before=left)
b1.pack()
b3=tk.Label(root)
b4=tk.Entry(root,width=10)
b5=tk.Entry(root,width=10)
def config():
key={}
key[b4.get()]=b5.get()
b3['text']=b1.paneconfig(right,key)
b2=tk.Button(root,text='PaneConfig',command=config)
b2.pack()
b4.pack(side='left')
b5.pack(side='left')
b3.pack()
root.mainloop()
结果:
动态调整子窗格的属性。
17.2.7 panes()
返回子窗口列表。
import tkinter as tk
from tkinter import ttk
root=tk.Tk()
root.geometry('320x240')
b1=tk.PanedWindow(showhandle=True,
sashrelief=tk.SUNKEN,
sashwidth=1,width=240,bg='blue')
left=tk.Text(b1,height=5,width=5)
b1.add(left,minsize=40)
right=tk.Text(b1,height=5,width=5,bg='red')
b1.add(right,padx=10,before=left)
b1.pack()
b3=tk.Label(root)
def panes():
b3['text']=b1.panes()
b2=tk.Button(root,text='Panes',command=panes)
b2.pack()
b3.pack()
root.mainloop()
结果:
17.2.8 sash_coord(index)
获得分隔线的坐标。index的值是0,因为一个窗格实例,只有一个分隔线。
import tkinter as tk
from tkinter import ttk
root=tk.Tk()
root.geometry('320x240')
b1=tk.PanedWindow(showhandle=True,
sashrelief=tk.SUNKEN,
sashwidth=1,width=240,bg='blue')
left=tk.Text(b1,height=5,width=5)
b1.add(left,minsize=40)
right=tk.Label(b1,text='实验室')
b1.add(right)
b1.pack()
b3=tk.Label(root,text=str(b1.sash_coord(0)))
b3.pack()
root.mainloop()
结果:
17.2.9 sash_mark(index)
返回鼠标上次拖拽分隔线的坐标。
import tkinter as tk
from tkinter import ttk
root=tk.Tk()
root.geometry('320x240')
b1=tk.PanedWindow(showhandle=True,
sashrelief=tk.SUNKEN,
sashwidth=1,width=240,bg='blue')
left=tk.Text(b1,height=5,width=5)
b1.add(left,minsize=40)
right=tk.Label(b1,text='实验室')
b1.add(right)
b1.pack()
def mark():
right['text']=str(b1.sash_mark(0))
b3=tk.Button(root,text='mark',command=mark)
b3.pack()
root.mainloop()
结果:
17.2.10 sash_place(index,x,y)
移动分隔到指定的位置(x,y)处。
import tkinter as tk
from tkinter import ttk
root=tk.Tk()
root.geometry('320x240')
b1=tk.PanedWindow(showhandle=True,
sashrelief=tk.SUNKEN,
sashwidth=1,width=240,bg='blue')
left=tk.Text(b1,height=5,width=5)
b1.add(left,minsize=40)
right=tk.Label(b1,text='实验室')
b1.add(right)
b1.pack()
b4=tk.Entry(root)
def place():
(x,y)=b4.get().split(',')
b1.sash_place(0,x,y)
b3=tk.Button(root,text='Place',command=place)
b3.pack()
b4.pack()
root.mainloop()
结果: