【发布时间】:2013-12-21 19:45:47
【问题描述】:
我正在尝试使用 tkinter 创建一个 GUI 来选择文本文件,最终结果是文件名以及与另一个程序一起使用的路径。文本文件可能不在同一个文件夹中。
GUI 目前能够:
- 在文件夹之间移动
- 在单独的框中显示文件夹和当前文件夹中的所有文本文件
- 选择文件并在单独的框中显示选定的文件名
我为路径和文件名携带了一个单独的 StringVar,它与显示所选文件名的列表框小部件的索引匹配,或者至少尝试这样做。我还没有找到一种像列表或数组那样简单地向 StringVar 添加或删除“行”的方法。因此,我尝试“提取”文件(通过将字符串转换回列表),以便使用 .set 将它们重写为相同的 StringVar,但这并不顺利。主要问题是每次我重写列表时,我都会将“\”的数量增加一倍。
如果有人知道一种更简单/更好的方法来实现我希望我所描述的内容,我将非常感谢您的意见或指向其他资源。如果您想使用它,请附上代码。我也是 python 和 tkinter 的新手,所以如果你看到任何可以做得“更好”的东西,我也想听听。感谢您的任何意见。
from tkinter import *
from tkinter import ttk
import data_Modules as dM
import os
################################################################################
# Change to a nested folder
################################################################################
def update(*arg):
print(entry1.get())
folders.set(value =
tuple([element for element
in os.listdir(entry1.get())
if os.path.isdir(os.path.join(entry1.get(), element))]))
dirFiles = [os.path.join(entry1.get(), element) for element
in os.listdir(entry1.get())
if element.split('.')[-1] == 'txt']
dirFiles.sort()
listFiles.set(value =
tuple([line.split('\\')[-1] for line in dirFiles]))
################################################################################
# Return to prior folder
################################################################################
def uplevel(*arg):
priorPath = hardDrive
for line in entry1.get().split('\\')[1:-1]:
priorPath = os.path.join(priorPath, line)
parentDir.set(priorPath)
update()
################################################################################
# Add file to 'memory'
################################################################################
def select(*arg):
dirFiles = [os.path.join(entry1.get(), element) for element
in os.listdir(entry1.get())
if element.split('.')[-1] == 'txt']
dirFiles.sort()
print('dirFiles')
print(dirFiles[int(boxlist2.curselection()[0])])
print(dirFiles[0])
temp = retVar(filePath)
print('temp In')
for line in temp:
print(line)
if all([element != dirFiles[int(boxlist2.curselection()[0])]
for element in temp]):
temp.append(dirFiles[int(boxlist2.curselection()[0])])
temp.sort()
print('temp Out')
for line in temp:
print(line)
filePath.set(value = tuple(temp))
selFiles.set(value = tuple([line.split('\\')[-1] for line in temp]))
################################################################################
# Remove file from 'memory'
################################################################################
def deselect(*arg):
temp = retVar(filePath)
temp.sort()
del temp[int(boxlist3.curselection()[0])]
filePath.set(value = tuple(temp))
selFiles.set(value = tuple([line.split('\\')[-1] for line in temp]))
################################################################################
# Attempt at retrieving data
################################################################################
def retVar(strVar):
if strVar.get() == '':
print('\n')
return []
else:
if strVar.get()[-2:] == ',)':
print([strVar.get()[2:-3]])
return [strVar.get()[2:-3]]
else:
print(strVar.get()[3:-3].split('\', \''))
return strVar.get()[3:-3].split('\', \'')
# Create window and Change name of window
root = Tk()
root.title('Main Window')
# Main hard drive for the computer
hardDrive = 'C:\\'
# Create necessary variables
parentDir = StringVar()
folders = StringVar()
listFiles = StringVar()
selFiles = StringVar()
filePath = StringVar()
parentDir.set(value = hardDrive)
dirFiles = []
# Generate all the widgets
entry1 = ttk.Entry(root, textvariable = parentDir)
entry1.grid(column = 0, row = 1, sticky = 'WE')
entry1.columnconfigure(0, weight = 1)
entry1.rowconfigure(0, weight = 1)
label1 = ttk.Label(root, text = 'Current Directory')
label1.grid(column = 0, row = 0, sticky = 'WE')
label1.columnconfigure(0, weight = 1)
label1.rowconfigure(0, weight = 1)
#Not currently used
#label2 = ttk.Label(root, text = 'Parent')
#label2.grid(column = 2, row = 1, sticky = 'WE')
#label2.columnconfigure(0, weight = 1)
#label2.rowconfigure(0, weight = 1)
label3 = ttk.Label(root, textvariable = parentDir.get())
label3.grid(column = 3, row = 2, columnspan = 2, sticky = 'WE')
label3.columnconfigure(0, weight = 1)
label3.rowconfigure(0, weight = 1)
label4 = ttk.Label(root, text = 'Folders')
label4.grid(column = 0, row = 2, sticky = 'WE')
label4.columnconfigure(0, weight = 1)
label4.rowconfigure(0, weight = 1)
label5 = ttk.Label(root, text = 'Files')
label5.grid(column = 3, row = 2, sticky = 'WE')
label5.columnconfigure(0, weight = 1)
label5.rowconfigure(0, weight = 1)
label6 = ttk.Label(root, text = 'Files')
label6.grid(column = 5, row = 2, sticky = 'WE')
label6.columnconfigure(0, weight = 1)
label6.rowconfigure(0, weight = 1)
button1 = ttk.Button(root, text = 'Go', command = update)
button1.grid(column = 1, row = 1, sticky = 'W')
button1.columnconfigure(0, weight = 1)
button1. rowconfigure(0, weight = 1)
button2 = ttk.Button(root, text = 'Back', command = uplevel)
button2.grid(column = 2, row = 1)
button2.columnconfigure(0, weight = 1)
button2. rowconfigure(0, weight = 1)
button3 = ttk.Button(root, text = 'deselect', command = deselect)
button3.grid(column = 5, row = 6)
button3.columnconfigure(0, weight = 1)
button3. rowconfigure(0, weight = 1)
button4 = ttk.Button(root, text = 'select', command = select)
button4.grid(column = 5, row = 4)
button4.columnconfigure(0, weight = 1)
button4. rowconfigure(0, weight = 1)
update()
boxlist1 = Listbox(root, listvariable = folders, height = 10,
font = 18, width = 40, selectmode = 'single')
boxlist1.grid(column = 0, row = 3, columnspan = 3, rowspan = 5)
boxlist1.columnconfigure(0, weight = 1)
boxlist1.rowconfigure(0, weight = 1)
boxlist2 = Listbox(root, listvariable = listFiles, height = 10,
font = 18, width = 40, selectmode = 'single')
boxlist2.grid(column = 3, row = 3, columnspan = 2, rowspan = 5)
boxlist2.columnconfigure(0, weight = 1)
boxlist2.rowconfigure(0, weight = 1)
boxlist3 = Listbox(root, listvariable = selFiles, height = 10,
font = 18, width = 40, selectmode = 'single')
boxlist3.grid(column = 6, row = 3, columnspan = 2, rowspan = 5)
boxlist3.columnconfigure(0, weight = 1)
boxlist3.rowconfigure(0, weight = 1)
boxlist1.bind('<Double-Button-1>', lambda e: print(boxlist1.get(int(boxlist1.curselection()[0]))))
boxlist1.bind('<Double-Button-1>', lambda e: parentDir.set(os.path.join(entry1.get(), boxlist1.get(int(boxlist1.curselection()[0])))))
boxlist1.bind('<Double-ButtonRelease-1>', update)
root.mainloop()
【问题讨论】:
标签: python user-interface python-3.x tkinter