'Using replace() in tkinter for changing text input from space to -

Text1 = Text(root,height=1,width=15,background='grey')
Text1.pack()
Text1.replace(" ","-")

So this is what i tried to do. I need to make all spaces typed in from the user to "-". So instead of "how are you" it should be "how-are-you". I've tried som different ways. i even tried Text1=Text1.replace(" ","-").

Anyone who could help me out here?



Solution 1:[1]

You have to get the current text using Text1.get, replace the contents and update it aagain using Text.replace`

Text1.replace("1.0", tkinter.END, Text1.get("1.0", tkinter.END).replace(' ', '-'))

Solution 2:[2]

Is this what you are looking for?

Code:

from tkinter import *

def replace_space():
    var = Text1.get('1.0','end')
    var = str.replace(var, ' ', '-')
    label['text'] = var

root = Tk()

Text1 = Text(root,height=1,width=15,background='grey')
Text1.pack()

button = Button(root, text = 'Go', command = replace_space)
button.pack()

label = Label(root)
label.pack()

root.mainloop()

Tkinter output:

str.replace()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sunitha
Solution 2 Joost01