Impostare il testo predefinito del widget Tkinter Entry
-
Metodo
insertper impostare il testo predefinito del widgetEntry -
Metodo Tkinter
StringVarper impostare il testo predefinito del widget TkinterEntry
Tkinter ha due metodi per impostare il testo predefinito del widget Tkinter Entry.
- Metodo Tkinter
insert - Metodo Tkinter
StringVar
Metodo insert per impostare il testo predefinito del widget Entry
Il widget Entry di Tkinter non ha una proprietà text specifica per impostare il testo predefinito come text="example". Ha il metodo insert per inserire il testo del widget Entry in modo che possa equivalentemente impostare il testo predefinito di Entry se chiamiamo il metodo insert dopo che l’oggetto Entry è stato inizializzato.
Codici di lavoro completi per impostare il testo predefinito di Entry con metodi insert
import tkinter as tk
root = tk.Tk()
root.geometry("200x100")
textExample = tk.Entry(root)
textExample.insert(0, "Default Text")
textExample.pack()
root.mainloop()

textExample.insert(0, "Default Text")
Il metodo insert inserisce il testo nella posizione specificata. 0 è il primo carattere in modo che inserisca il Default Text all’inizio.
Metodo Tkinter StringVar per impostare il testo predefinito del widget Tkinter Entry
textvariable associa il contenuto del widget Entry con una variabile Tkinter StringVar. Potrebbe impostare la StringVar per impostare il testo predefinito del widget Entry dopo aver creato l’associazione appropriata.
Codici di lavoro completi per impostare il testo predefinito in Entry con textvariable
import tkinter as tk
root = tk.Tk()
root.geometry("200x100")
textEntry = tk.StringVar()
textEntry.set("Default Text")
textExample = tk.Entry(root, textvariable=textEntry)
textExample.pack()
root.mainloop()
textEntry = tk.StringVar()
textEntry.set("Default Text")
textExample = tk.Entry(root, textvariable=textEntry)
textEntry è una variabile StringVar ed è associata al contenuto di testo dell’oggetto Entry da textvariable = textEntry.
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn Facebook