Create fields dynamically

Asked

Viewed 43 times

0

I’m learning Python created a screen and inserted some fields, but when I try to create a routine to automate, make more generic I can’t. follow the code, if you can help me, thank you.

from Tkinter import *

def btn_gravar_click(): print (ent_name1.get()) print (ent_name2.get())

def cria_lbl_ent (in: Object, widgets: tuple, X1: int, x2: int, Y1:int, Y2: int, an: str = W, bg: str = None) -> int: """ Build corresponding Label and Entry on the same line.

Recebe um codinome e adiciona a este os prefixos lbl_ e ent_
    respectivamente para as Labels e Entry, criando-as
    conforme posição informada ou calculada segundo os
    parametros fornecidos.

:param em: object: Objeto que contera os wigdgets.
:param widgets: tupla: Tupla contendo codinome dos campos, conteudo
                            da Label e comprimento da Entry.
:param x1: int: Deslocamento horizontal da margem ate as Labels.
:param x2: int: Deslocamento horizontal da margem ate as Entry.
:param y1: int: Altura inicial da primeira linha.
:param y2: int: Distancia a incrementar para a proxima linha.
:param an: str: Anchor, Justificacao da Label(padrao = W esquerda).
:param bg: str: Cor de fundo das Labels (padrao = None - nenhuma).
:returns y: int: proxima posição a preencher.

Exemplo.
---------

from tkinter import *
gui0 = Tk()
texto = Dados do cliente
lfr_cli = LabelFrame(gui0, text = texto, relief=GROOVE,
                        borderwidth=2)
lfr_cli.place(x=10, y=10, width=520, height=80)

# Definindo as Labels e Entry.
widgets = (('nome', 'Nome', 400),
           ('end', 'Endereço', 400))

pos = cria_lbl_ent(lfr_dir, widgets, x1=10,x2=100,y1=10,y2=20, bg='red')

Efeito:
Criara na lfr_cli os objetos:
    lbl_nome = Label(fr_cli, text='Nome', anchor=W,bg='red')
    lbl_nome.place (x=10, y=10, width=90)   
    ent_nome = Entry(em)
    ent_nome.place (x=100, y=10, width=400)
    
    lbl_end = Label(fr_cli, text='Endereço', anchor=W,bg='red')
    lbl_end.place (x=10, y=30, width=90)   
    ent_end = Entry(em)
    ent_end.place (x=100, y=30, width=400)

print(pos)
>>> 50

"""

# Largura das Labels
w1 = x2-x1 -2
for nome,texto,w0 in widgets:
    # Montando objetos.
    v_obj = 'lbl_' + nome
    locals()[v_obj] = Label(em, text=texto, anchor=an,bg=bg)
    locals()[v_obj].place (x=x1, y=y1, width=w1)
    v_obj = 'ent_' + nome
    locals()[v_obj] = Entry(em)
    locals()[v_obj].place (x=x2, y=y1, width=w0)
    y1 = y1 + y2

    print(v_obj)

locals()
return y1

Window Manager.

gui1 = Tk()

Make the window transitory.

gui1.()

Window size.

s_wid = 420 s_heig = 250

Calculating the window coordinates.

x_wid = int((gui1.winfo_screenwidth()-s_wid)/2) y_heig = int((gui1.winfo_screenheight()-s_heig)/2)

Positioning the window.

screen = str(s_wid) + 'x' + str(s_heig) + '+' + str(x_wid) + '+' + str(y_heig) gui1.Geometry(screen)

Window title.

gui1.title ('Settings')

Put the window in modal.

gui1.grab_set()

Transfer the focus to the window.

gui1.Focus()

Creating the Frame

text = 'Personal data' lfr_cli1 = Labelframe(gui1, text=text , Relief=GROOVE, borderwidth=2) lfr_cli1.place(x=10, y=10, width=400, height=100)

Defining the Client Labels and Entry.

widgets = (('nome1', 'Name', 300), ('end1', 'address', 300), ('rg1', 'RG', 100))

in = lfr_cli1 X1 = 10 # Left margin. x2 = X1 + 80 # Internal margin. Y1 = 10 # Upper margin. Y2 = 20 # Increment/between line. an = CENTER # W,CENTER,E bg = 'red' # None,'red','blue'

Creating the Labels and Entry.

w1 = x2 - X1 - 2 for name,text,W0 in widgets: # Special Objects. v_obj = 'lbl_' + name locals()[v_obj] = Label(em, text=text, Anchor=an, bg=bg) locals()[v_obj]. place (x=X1, y=Y1, width=w1) v_obj = 'ent_' + name locals()[v_obj] = Entry(in) locals()[v_obj]. place (x=x2, y=Y1, width=W0) Y1 =Y1+Y2

------------------------------------------------------

text = 'Personal data 2' lfr_cli2 = Labelframe(gui1, text=text , Relief=GROOVE, borderwidth=2) lfr_cli2.place(x=10, y=110, width=400, height=100)

Creating the Labels and Entry.

widgets = (('Nome2', 'Name', 300), ('end2', 'Address', 300), ('rg2', 'RG', 100))

in = lfr_cli2 X1 = 10 # Left margin. x2 = X1 + 80 # Internal margin. y0 = 0 # Handling. Y1 = 10 # Upper margin. Y2 = 20 # Increment/between line. an = W # W,CENTER,E bg = 'blue' # None,'red','blue'

pos = creator_lbl_ent(in, widgets, X1, x2, Y1, Y2, an, bg)

#-------------------------------------------------------------

bg = None text = 'Add content to the name fields and n press "Save and exit"' lbl_warning = Label(gui1, text=text, Anchor=an, bg=bg) lbl_warning.place (x=X1, y=210)

btn_write = Button(gui1, text='Save and quit', command=btn_gravar_click) btn_write.place(x=340, y=220, height=20, width=70)

  • It’s my first question I think I made a mess of the question!

  • can remove or edit the question?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.