为什么python不';不接受我从文本文件中获取的颜色?Python Tkinter
我将颜色保存在文本文件中,当我从中获取颜色时,Python说颜色变量有引号。我不明白为什么,因为当我打印变量时没有引号 下面是一些代码:为什么python不';不接受我从文本文件中获取的颜色?Python Tkinter,python,user-interface,tkinter,Python,User Interface,Tkinter,我将颜色保存在文本文件中,当我从中获取颜色时,Python说颜色变量有引号。我不明白为什么,因为当我打印变量时没有引号 下面是一些代码: from tkinter import * window = Tk() with open('test.txt', 'r') as r: backround = r.readline() l = Label(window, bg=backround) l.pack() 下面是上面执行的代码的错误: test.txt如下所示: red 如果我
from tkinter import *
window = Tk()
with open('test.txt', 'r') as r:
backround = r.readline()
l = Label(window, bg=backround)
l.pack()
下面是上面执行的代码的错误:
test.txt如下所示:
red
如果我打印相同的变量:
with open('test.txt', 'r') as r:
backround = r.readline()
print(backround)
输出仅为红色,没有任何引号。从错误图片上看,问题与新行有关。您认为您发送的是红色,但实际发送的是红色\n,而不是颜色 在第二个示例中,您应该尝试:
print("[" + backround + "]")
然后,您可以查看red和]
正如khelwood所写的,您可以只编写strip()并去掉换行符
l = Label(window, bg=backround.strip())
错误似乎不是由引号引起的。引号只是用来分隔字符串。错误似乎是由于字符串末尾的新行造成的。您可以使用
.strip()
将其删除。