【发布时间】:2016-01-17 13:47:07
【问题描述】:
我在 python 中使用 tkinter 插件,我正在“.get”来自文本小部件的输入文本,
当我这样做时,它决定添加一个换行符。
我不想要这个换行符,因为它弄乱了我的 CSV 保存文件,意味着我无法从 python 正确读取我的 CSV 文件。
如何去掉“\n”?
我试过“.rstrip”和“.splitlines”都不管用。
This is what is displayed by the code...
如何去掉换行符“\n”?
纯代码:
TextIDNumber = Text(App, height = "1", width = "25", bd = "4")
TextIDNumber.place(x = 550, y = 250)
TextIDNumber.config(font =("bold", 20))
TextSurname = Text(App, height = "1", width = "25", bd = "4")
TextSurname.place(x = 550, y = 350)
TextSurname.config(font =("bold", 20))
def CheckInputUI():
IDNumber = TextIDNumber.get("1.0", END)
Surname = TextSurname.get("1.0", END)
print (Surname, IDNumber)
print ("")
print (Surname + IDNumber)
重现您的问题的最短代码,我们可以在我们自己的机器上运行:
from tkinter import *
App = Tk()
#This maximises the window
w = App.winfo_screenwidth()
h = App.winfo_screenheight()
App.geometry("%dx%d+0+0" % ((w-10), (h-30)))
TextIDNumber = Text(App, height = "1", width = "25", bd = "4")
TextIDNumber.place(x = 550, y = 250)
TextIDNumber.config(font =("bold", 20))
TextSurname = Text(App, height = "1", width = "25", bd = "4")
TextSurname.place(x = 550, y = 350)
TextSurname.config(font =("bold", 20))
def PrintInfo():
IDNumber = TextIDNumber.get("1.0", END)
Surname = TextSurname.get("1.0", END)
print (Surname, IDNumber)
print ("")
print (Surname + IDNumber)
DoneButton = Button(App, text = " Done ", font = ("bold", 18), command = PrintInfo).place(x = 400,y = 800)
我使用打印件仅在 shell 上显示我的工作(这样我就可以找到问题的根源,换句话说,在哪里添加了换行符),它们没有其他用途。
【问题讨论】:
-
请发布纯代码而不是图片,以便重现您的问题。
-
所有代码?或者只是图片中包含的“纯代码”?
-
您是否将
CheckInputUI绑定到 Enter 键? -
我的 python 时代已经过去了,这只是一个有趣的时间浪费,
-
新行由
"\n"表示,而不是"/n"。