Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在tkinter中使用replace()将文本输入从空格更改为-_Python_Python 3.x_Tkinter_Str Replace - Fatal编程技术网

Python 在tkinter中使用replace()将文本输入从空格更改为-

Python 在tkinter中使用replace()将文本输入从空格更改为-,python,python-3.x,tkinter,str-replace,Python,Python 3.x,Tkinter,Str Replace,这就是我想做的。我需要将所有空格从用户输入到“-”。因此,与其说“你好”,不如说“你好”。我试过很多不同的方法。我甚至试过Text1=Text1.replace(“,“-”) 有人能帮我吗 您必须使用Text1获取当前文本。获取,替换内容并使用text更新。替换` Text1 = Text(root,height=1,width=15,background='grey') Text1.pack() Text1.replace(" ","-") 必须使用Text1获取当前文本。获取,替换内容并使

这就是我想做的。我需要将所有空格从用户输入到“-”。因此,与其说“你好”,不如说“你好”。我试过很多不同的方法。我甚至试过
Text1=Text1.replace(“,“-”)


有人能帮我吗

您必须使用
Text1获取当前文本。获取
,替换内容并使用
text
更新。替换`

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

必须使用
Text1获取当前文本。获取
,替换内容并使用
text
更新。替换`

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

这就是你要找的吗

代码:

Tkinter输出:


这就是你要找的吗

代码:

Tkinter输出:


是的,就是这样。完美的非常感谢。我不知道我必须先给它下定义是的就是这样。完美的非常感谢。我不知道我必须先定义它,我也会试试这个。你能解释一下为什么是tkinter.END吗?我也会试试这个。你能解释一下为什么是tkinter.END吗?
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()