How do I place a preset into the text box?

Colin McPhail colin.mcphail at mac.com
Fri Aug 28 07:46:25 EDT 2020


Hi Steve,

> On 28 Aug 2020, at 11:03, Steve <Gronicus at SGA.Ninja> wrote:
> 
> 
> The following program compiles but does not quite do what I would like it to
> do. Line 19 is the preset information but I do not seem to be able to get it
> into the form by code.  My purpose is to let the user make changes without
> having to re-enter the entire code.

I'm no Tk expert but does the following do what you want? (Strictly speaking, the parentheses in ("1234-abcd") are not wrong just unnecessary.)

#===========================================================
import tkinter as tk
from tkinter import ttk
import sys

window = tk.Tk()
window.title("Python Tkinter Text Box")
window.minsize(600,400)

def Submit():
   label.configure(text= 'The new code is: ' + NewCode.get())

def ClickExit():
   #This exit closes the program but the form remains and is still active.
   # I want only to close the form.
   print("Exiting")
   #sys.exit()
   window.destroy()

#OldCode = ("1234-abcd")
OldCode = "1234-abcd"

label = ttk.Label(window, text = "Enter the new code")
label.grid(column = 1, row = 1)

#NewCode = tk.StringVar()
NewCode = tk.StringVar(value=OldCode)

CodeEntered = ttk.Entry(window, width = 15, textvariable = NewCode)
CodeEntered.grid(column = 2, row = 3)

button = ttk.Button(window, text = "Submit", command = Submit)
button.grid(column= 2, row = 5)

button = ttk.Button(window, text = "Quit", command = ClickExit)
button.grid(column= 2, row = 7)

window.mainloop()

x = (NewCode.get())
print("The new code entered is: " + x)

#=================================================

Regards,
Colin



More information about the Python-list mailing list