Tkinter | Adicionando estilo ao texto inserido usando o widget ttk.Entry

Widgets de Login são usados para recuperar uma entrada de um usuário. Ele pode ser criado assim:

 entry = ttk.Entry (master, option = value, ...) 

Código # 1: Criando um widget Entry e obtendo entrada do usuário (obtendo apenas dados de String).

# tkinter importa

de tkinter importar *

de tkinter import ttk

from tkinter.messagebox import askyesno


# criando root

root = Tk()


# especificando a geometria

< p> root.geometry ( ’200x100’ )


# Isso é usado para obter entrada do usuário
# e mostre no Entry Widget.
# Quaisquer dados que recebemos do teclado
# serão tratados como uma string.

input_text = StringVar()

entry1 = ttk.Entry (root, textvariable = input_text, justify = CENTER)


# focus_force usado para foco
# assim que o aplicativo iniciar
entry1.focus_force ()

entry1.pack (side = TOP, ipadx = 30 , ipady = 6 >)

salvar = ttk.Button (root, text = ’Salvar’ , comando = lambda : askyesno (

’ Confirmar’ , ’Deseja salvar? ’ ))

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


root.mainloop ()

Saída:

Na saída acima, depois de executar o código, a janela , uma mensagem de confirmação aparecerá perguntando se você deseja salvar o texto ou não (o texto não será salvo, é usado apenas para exibir a funcionalidade do botão).

Código nº 2: adicione estilo ao texto inserido no widget de entrada.

< td class = "code">

# tkinter import

from tkinter import *

from tkinter importar ttk

de tkinter.messagebox import askyesno


# cria raiz

root = Tk()

root.geometry ( ’ 200x100’ )

input_text = StringVar()


# Esta classe é usada para adicionar estilo
# para qualquer widget disponível

estilo = ttk.Style ( )

style.configure ( ’TEntry’ , foreground = ’green’ )

entry1 = ttk. Entry (root, textvariable = input_text, justificar = CENTER,

fonte = ( ’courier’ , 15 , ’bold’ ))

entry1.focus_force ()

entry1.pack (lado = TOP, ipadx = 30 , ipady = 10 )

salvar = ttk. Botão (raiz, texto = ’Salvar’ , comando = lambda : askyesno (

’Confirmar’ , ’ Deseja salvar? ’ ))

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


root.mainloop()

Saída:

Na saída acima, você pode notar que a cor da fonte mudou, a família da fonte mudou, o texto é la rger que o normal, e o texto está em negrito. Isso ocorre porque estamos adicionando estilo ao texto que inserimos.

Esperamos que este artigo tenha ajudado você a resolver o problema. Além de Tkinter | Adicionando estilo ao texto inserido usando o widget ttk.Entry, confira outros tópicos relacionados a Python functions.

Quer se destacar em Python? Veja nossa análise dosmelhores cursos on-line de Python 2023. Se você estiver interessado em Ciência de Dados, veja também como aprender programação em R.

A propósito, este material também está disponível em outros idiomas:



Ken Innsbruck

Tallinn | 2023-03-30

Eu estava me preparando para minha entrevista de codificação, obrigado por esclarecer isto - Tkinter | Adicionando estilo ao texto inserido usando o widget ttk.Entry em Python não é o mais simples. Verificado ontem, funciona!

Anna Galleotti

Texas | 2023-03-30

Obrigado por explicar! Eu fiquei preso com Tkinter | Adicionando estilo ao texto inserido usando o widget ttk.Entry por algumas horas, finalmente consegui fazer isso 🤗. Vou usá-lo na minha tese de bacharelado

Schneider Robinson

Prague | 2023-03-30

Eu estava me preparando para minha entrevista de codificação, obrigado por esclarecer isto - Tkinter | Adicionando estilo ao texto inserido usando o widget ttk.Entry em Python não é o mais simples. Verificado ontem, 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