0
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?

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Daniel Saggo
  • 108
  • 1
  • 9

2 Answers2

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(' ', '-'))
Sunitha
  • 11,777
  • 2
  • 20
  • 23
1

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()

Joost01
  • 11
  • 1