我在使用TK的stringVars时遇到了麻烦。
from tkinter import *
root = Tk()
strVar = StringVar()
tmp1 = ['one','two','three']
print(tmp1)
print(len(tmp1))
strVar.set(tmp1)
tmp2=strVar.get()
print(tmp2)
print(len(tmp2))
输出为:
['one', 'two', 'three']
3
('one', 'two', 'three')
23
如您所见,格式是不同的。显然,字符串列表在内部转换为带引号的一个字符串。是什么原因造成的,如何避免?对于我的脚本,我想要一个字符串列表以供进一步处理。
答案 0 :(得分:1)
@ tjt263,已经很久了,但是我想我像
一样解决了它 fdupesSV.set(fdupesList)
tmp = subprocess.run(['fdupes', '-r',directory.get()], stdout=subprocess.PIPE)
#first convert bytes to ascii, then split at '\n', then assign so fdupesOut
fdupesList=tmp.stdout.decode('ascii').split('\n')
fdupesSV.set(fdupesList)
我不确定我是否正确地记住了这个问题,但希望对您有帮助
欢呼 詹斯
答案 1 :(得分:0)
from tkinter import *
import ast
root = Tk()
strVar = StringVar()
tmp1 = ['one','two','three']
print(type(tmp1), tmp1, len(tmp1))
strVar.set(tmp1)
x = ast.literal_eval('[' + strVar.get()[1:-1] + ']')
print(type(x), x, len(x))