Tkinter | Agregar estilo al texto ingresado usando el widget ttk.Entry

Los widgets de inicio de sesión se utilizan para recuperar una entrada de un usuario. Se puede crear así:

 entrada = ttk.Entry (maestro, opción = valor, ...) 

Código # 1: Creando un entrada del widget y obtener la entrada del usuario (obteniendo solo datos de cadena).

# tkinter importa

de tkinter importar *

de tkinter import ttk

from < /código> tkinter.messagebox importar askyesno


# creando raíz

raíz = Tk()


# especificando geometría

< p> raíz.geometría ( ’200x100’ )


# Esto se usa para obtener información del usuario
# y mostrarlo en el widget de entrada.
# Cualquier dato que recibamos del teclado
# será tratado como una cadena.

input_text = StringVar()

entrada1 = ttk.Entry (raíz, variable de texto = input_text, justificar = CENTRO)


# focus_force usado para enfocar
# tan pronto como se inicie la aplicación
entry1.focus_force()

entrada1.paquete (lado = TOP, ipadx = 30 , ipady = 6 )

guardar = ttk.Button (raíz, texto = ’Guardar’ , comando = lambda : askyesno (

’ Confirmar’ , ’¿Quieres guardar? ’ ))

save.pack (side = TOP, pady = 10 )


root.mainloop()

Salida:

En el resultado anterior, una vez que ejecuta el aparece la ventana , aparece un mensaje de confirmación preguntando si desea guardar el texto o no (el texto no se guardará, es solo usado para mostrar la funcionalidad del botón).

Código # 2: agrega estilo al texto ingresado en el widget de entrada.

< td class = "código">

# tkinter import

from tkinter importar *

de tkinter import ttk

de tkinter.messagebox importar askyesno


# crear raíz

raíz = Tk ()

root.geometry ( ’ 200x100’ )

input_text = StringVar()


# Esta clase se usa para agregar estilo
# a cualquier widget disponible

estilo = ttk.Estilo ( )

estilo.configure ( ’TEntry’ , primer plano = ’verde’ < código clase ="simple ">)

entrada1 < /código> = ttk. Entrada (raíz, variable de texto = texto_entrada, justificar = CENTRO,

fuente = ( ’mensajero’ , 15 , ’negrita’ ))

entrada1.focus_force ()

entrada1.paquete (lado = TOP, ipadx = 30 , ipady = 10 )

guardar = ttk. Botón (raíz, texto = ’Guardar’ , comando = lambda : askyesno (

< clase de código = "cadena"> ’Confirmar’ , ’ ¿Desea guardar? ’ ))

save.pack (side = TOP, pady = 10 )


root.mainloop()

Salida:

En el resultado anterior, puede notar que el color de la fuente ha cambiado, la familia de fuentes ha cambiado, el texto es la más grande de lo normal y el texto está en negrita. Esto se debe a que estamos agregando estilo al texto que ingresamos.

Esperamos que este artículo le haya ayudado a resolver el problema. Además de Tkinter | Agregar estilo al texto ingresado usando el widget ttk.Entry, consulta otros temas relacionados con Python functions.

¿Quiere sobresalir en Python? Consulte nuestra reseña de los mejores cursos en línea de Python 2023. Si está interesado en Data Science, consulte también cómo aprender programación en R.

Por cierto, este material también está disponible en otros idiomas:



Oliver Chamberlet

Paris | 2023-03-30

Me estaba preparando para mi entrevista de codificación, gracias por aclarar esto - Tkinter | Agregar estilo al texto ingresado usando el widget ttk.Entry en Python no es el más simple. Lo usare en mi tesis de licenciatura

Jan Nickolson

California | 2023-03-30

¿Tal vez haya otras respuestas? ¿Qué significa el Tkinter | Agregar estilo al texto ingresado usando el widget ttk.Entry significa exactamente?. Simplemente no estoy muy seguro de que sea el mejor método

Xu Galleotti

Abu Dhabi | 2023-03-30

Gracias por la explicación. Estuve atascado con el Tkinter | Agregar estilo al texto ingresado usando el widget ttk.Entry durante algunas horas, finalmente lo conseguí 🤗. Comprobado ayer, ¡funciona!

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically