我有一个askopenfilenames()
函数,它返回一个元组并将其放在一个名为files
的变量中。
当我print files
时,它会像这样打印出来:
('/home/hoeter/PycharmProjects/Renpy/rpatool.py', '/home/hoeter/PycharmProjects/Renpy/unrpa.py', '/home/hoeter/PycharmProjects/Renpy/window.py')
现在我想要的是将此值放在,
但没有()
的条目中,我正在尝试以下操作:
files_path.set(files)
entry3 = Entry(f3, width=50, textvariable=files_path)
但是在条目中它出现了:
/home/hoeter/PycharmProjects/Renpy/rpatool.py /home/hoeter/PycharmProjects/Renpy/unrpa.py /home/hoeter/PycharmProjects/Renpy/window.py
但是我预计它会像print
中那样出现。为什么不呢?
简而言之,我在条目中的所需输出是:
'/home/hoeter/PycharmProjects/Renpy/rpatool.py', '/home/hoeter/PycharmProjects/Renpy/unrpa.py', '/home/hoeter/PycharmProjects/Renpy/window.py'
条目中我当前的输出是:
/home/hoeter/PycharmProjects/Renpy/rpatool.py /home/hoeter/PycharmProjects/Renpy/unrpa.py /home/hoeter/PycharmProjects/Renpy/window.py
我现在希望在条目中出现但不是,
('/home/hoeter/PycharmProjects/Renpy/rpatool.py', '/home/hoeter/PycharmProjects/Renpy/unrpa.py', '/home/hoeter/PycharmProjects/Renpy/window.py')
答案 0 :(得分:3)
StringVar.set
接受一个元组,我有点惊讶。
获取所需字符串的简单方法是将元组转换为字符串并对其进行切片以删除括号:
files_path.set(str(files)[1:-1])
另一种方法是使用str.join
方法,向它传递一个生成器表达式,该表达式生成元组中每个项目的表示。
s = ', '.join(repr(u) for u in files)
files_path.set(s)
但实际上没有必要这样做,因为第一个版本基本上是做同样的事情。
请注意,对于这两个版本,如果任何文件名包含单引号字符,则表示将使用双引号引用。如果文件名包含单引号和双引号,则其表示将是单引号,内部单引号将反斜杠转义,如下所示:\'
。但希望你不需要处理这些邪恶的文件名。 :)
答案 1 :(得分:0)
括号和逗号不是字符串的一部分,它们只是List的可视部分。
你想要这样的东西吗?
list_of_string = ('/home/hoeter/PycharmProjects/Renpy/rpatool.py', '/home/hoeter/PycharmProjects/Renpy/unrpa.py',
'/home/hoeter/PycharmProjects/Renpy/window.py')
output = ""
for string in list_of_string:
output += "('"+string+"')" + ", "
print(output)
输出:
('/home/hoeter/PycharmProjects/Renpy/rpatool.py'), ('/home/hoeter/PycharmProjects/Renpy/unrpa.py'), ('/home/hoeter/PycharmProjects/Renpy/window.py'),
答案 2 :(得分:0)
试试这个:
from tkinter import *
root = Tk()
entries = []
entries2 = []
#--------------1st row input----------------
for i in range(4):
entry = IntVar()
entry.set("")
en = Entry(root,textvariable=entry)
en.grid(row=i+1, column=0)
entries.append(en)
#----------------2nd row output -------------
for i in range(4):
entry = IntVar()
en2 = Entry(root,textvariable=entry)
en2.grid(row=i+1, column=2)
entries2.append(en2)
def saveData():
entrylist = []
for entry in entries:
getentry = int(entry.get())
entrylist.append(getentry)
print(entrylist)
for entry in entries2:
for x in entrylist:
entry.set(x)
button = Button(root, text = "summit", command = saveData).grid(row = 12, column = 0)
root.mainloop()