Tkinter | Aggiunta di stile al testo inserito utilizzando il widget ttk.Entry

I widget di accesso vengono utilizzati per recuperare una voce da un utente. Può essere creato in questo modo:

 entry = ttk.Entry (master, opzione = valore, ...) 

Codice n. 1: Creazione di un widget Inserimento e ricezione dell’input dell’utente (ottenimento solo di dati String).

# tkinter importa

da tkinter import *

da tkinter import ttk

da tkinter.messagebox import askyesno


# creazione radice

radice = Tk()


# specificando la geometria

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


# Viene utilizzato per ottenere input dall’utente
# e mostralo nell’Entry Widget.
# Qualunque dato che riceviamo dalla tastiera
# sarà trattato come una stringa.

input_text = StringVar ()

entry1 = ttk.Entry (root, textvariable = input_text, giustifica = CENTRO)


# focus_force utilizzato per il focus
# non appena l’applicazione viene avviata
entry1.focus_force ()

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

salva = ttk.Button (root, testo = ’Salva’ , comando = lambda : askyesno (

’Conferma’ , ’Vuoi salvare? ’ ))

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


root.mainloop()

Output:

Nell’output sopra, una volta eseguito il codice, viene visualizzata la finestra , compare un messaggio di conferma che ti chiede se vuoi salvare il testo oppure no (il testo non verrà salvato, è solo usato per visualizzare la funzionalità del pulsante).

Codice n. 2: aggiungi uno stile al testo inserito nel widget di input.

< td class = "code">

# tkinter import

from tkinter import *

da tkinter import ttk

da tkinter.messagebox import askyesno


# crea radice

radice = Tk ()

root.geometry ( ’ 200x100’ )

input_text = StringVar()


# Questa classe viene utilizzata per aggiungere uno stile
# a qualsiasi widget disponibile

stile = ttk.Style ( )

style.configure ( ’Tentry’ , primo piano = ’verde’ )

entry1 < /codice> = ttk. Voce (root, textvariable = input_text, giustifica = CENTRO,

carattere = ( ’corriere’ , 15 , ’grassetto’ ))

entry1.focus_force ()

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

salva = ttk. Pulsante (radice, testo = ’Salva’ , comando = lambda : askyesno (

’Conferma’ , ’ Vuoi salvare? ’ ))

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


root.mainloop ()

Output:

Nell’output sopra, potresti notare che il colore del carattere è cambiato, la famiglia di caratteri è cambiata, il testo è la più del normale e il testo è in grassetto. Questo perché stiamo aggiungendo uno stile al testo che inseriamo.

Ci auguriamo che questo articolo ti abbia aiutato a risolvere il problema. Oltre a Tkinter | Aggiunta di stile al testo inserito utilizzando il widget ttk.Entry, controlla altri argomenti relativi a Python functions.

Vuoi eccellere in Python? Consulta la nostra recensione dei migliori corsi online Python 2023. Se sei interessato alla scienza dei dati, controlla anche come imparare a programmare in R.

A proposito, questo materiale è disponibile anche in altre lingue:



Anna Wu

Boston | 2023-03-30

Semplice e chiaro. Grazie per la condivisione. Il mio punto debole è sempre stato il Tkinter | Aggiunta di stile al testo inserito utilizzando il widget ttk.Entry e altri problemi con il Python functions 😁. Tornerò domani con un feedback

Schneider Galleotti

Moscow | 2023-03-30

Forse ci sono altre risposte? Cosa significa esattamente Tkinter | Aggiunta di stile al testo inserito utilizzando il widget ttk.Entry significa esattamente?. Tornerò domani con un feedback

Jan Williams

Shanghai | 2023-03-30

Forse ci sono altre risposte? Cosa significa esattamente Tkinter | Aggiunta di stile al testo inserito utilizzando il widget ttk.Entry significa esattamente?. Spero solo che non emerga più

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