0

As I type into Entry "e1", Label "l1" updates to display the entry. I plan to use this later to preview multiple entries in a single line.

My question: How can I get the Label "l1" to display with single quotes around it as I type into Entry "e1". The quotes should only appear when there is text in Entry "e1". They should also be removed if the content in Entry is deleted.

I'm quite new to this so simple answers please.

from tkinter import *

root = Tk()

def printEntry():
    complete = var1.get() + var2.get()
    print (complete)

var1 = StringVar()

var2 = StringVar()

e1 = Entry(root, width = 15, textvariable = var1)
e1.pack()

l1 = Label(root, textvariable = var1)
l1.pack()

e2 = Entry(root, width = 15, textvariable = var2)
e2.pack()

l2 = Label(root, textvariable = var2)
l2.pack()

b = Button(root, text = "CONFIRM", command = printEntry)
b.pack()

mainloop()
j_4321
  • 15,431
  • 3
  • 34
  • 61
Paulo19
  • 35
  • 9
  • where are the quotes you are talking about? When you enter a string into an entry there is no need for putting quotes to specify it is a string that is implied by the fact that it is holding characters and tied to a `StringVar` – Tadhg McDonald-Jensen Dec 12 '16 at 17:00
  • wait so if I type for example `hello` (without quotes) into `e1` entry you would want `l1` label to show `'hello'` with quotes? And then be blank when there is nothing typed into `e1`? Why is this desired behaviour? – Tadhg McDonald-Jensen Dec 12 '16 at 17:01
  • There won't be a simple answer to this question. You will either need to create a custom widget, or create a trace on the variables. Neither of those are particularly difficult, but neither are they simple. – Bryan Oakley Dec 12 '16 at 17:18
  • The quotes are required because the final string will find/replace content on a website. The quotes will be added to the input later as I do not want to confuse the user. The label will be used as a preview so it needs to display the quotes. – Paulo19 Dec 13 '16 at 09:15
  • e.g. $(".sd-wrap .text, h1 span").html(function (){return $(this).html().replace('ENGLISH TEXT','GERMAN TEXT')}); – Paulo19 Dec 13 '16 at 09:26

1 Answers1

1

Using the trace method of the StringVar can be used to trigger a function each time the entry is edited:

from tkinter import *

root = Tk()

def printEntry():
    complete=var1.get() + var2.get()
    print(complete)

def update_l1(*args):
    txt = var1.get()
    if txt:
        l1.configure(text="'%s'" % txt)
    else:
        l1.configure(text='')

def update_l2(*args):
    txt = var2.get()
    if txt:
        l2.configure(text="'%s'" % txt)
    else:
        l2.configure(text='')

var1 = StringVar()
var1.trace("w", update_l1)

var2 = StringVar()
var2.trace("w", update_l2)

e1 = Entry(root, width=15, textvariable=var1)
e1.pack()

l1 = Label(root)
l1.pack()

e2 = Entry(root, width=15, textvariable=var2)
e2.pack()

l2 = Label(root)
l2.pack()

b = Button(root, text="CONFIRM", command=printEntry)
b.pack()

mainloop()

UPDATE: Answer to the comment about putting the quotes inside the entry too:

from tkinter import *

root = Tk()

def printEntry():
    complete=var1.get()
    print(complete)

def update1(*args):
    txt = var1.get().strip("'") # remove surronding quotes if present
    if txt:
        var1.set(r"'%s'" % txt) # add the quotes
    else:
        var1.set("")
    l1.configure(text=var1.get()) # update the label contents

var1 = StringVar()
var1.trace("w", update1)

e1 = Entry(root, width=15, textvariable=var1)
e1.pack()

l1 = Label(root)
l1.pack()

b = Button(root, text="CONFIRM", command=printEntry)
b.pack()

root.mainloop()

For some reason I don't understand, the label did not display the same text as the entry if I used l1 = Label(root, textvariable=var1) (the quotes did not appear immediately), so instead I update the text of l1 inside the function update1.

j_4321
  • 15,431
  • 3
  • 34
  • 61
  • This works great but I have two questions. 1. Is it possible to have both Labels (l1 & l2) reference one quote function instead of using a function for each Label. 2. Can you explain what configure() does? – Paulo19 Dec 13 '16 at 09:34
  • 1. I don't know, but this question seems relevant http://stackoverflow.com/questions/27369546/how-can-i-use-the-same-callback-function-to-trace-multiple-variables?rq=1, 2. All tkinter widget have a `configure` method to change their options such as text, command, background, width ..., to list all available options, use the `keys` method. – j_4321 Dec 13 '16 at 09:58
  • Could a similar function be used to wrap the Input and Label in quotes? The quotes in the Input would need to be permanent so the user could not delete them? – Paulo19 Dec 13 '16 at 10:47