首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tkinter文件管理器

Tkinter文件管理器
EN

Stack Overflow用户
提问于 2021-10-26 05:50:01
回答 1查看 58关注 0票数 0

我一直收到这个错误。任何洞察力都会有所帮助。

代码语言:javascript
复制
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\14065\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1892, in __call__
    return self.func(*args)
  File "C:\Users\14065\Documents\GitHub\Python-Projects\FILE_TRANSFER_PART_3.py", line 70, in move_to
    for fname in os.listdir(sourceLocation):
TypeError: listdir: path should be string, bytes, os.PathLike or None, not StringVar
代码语言:javascript
复制
#importing packages
from tkinter import *
import shutil
import time
import os
from tkinter import filedialog

root = Tk()
root.title('File Manager')
root.geometry("600x200")

#tkinter widgets
def create_widgets():
    link_label = Label(root, text = "Select Files To Copy:")
    link_label.grid(row = 1, column = 0,
                    pady = 5, padx = 5)

    root.sourceText = Entry(root, width = 50,
                            textvariable = sourceLocation)
    root.sourceText.grid(row = 1, column = 1,
                         pady = 5, padx = 5,
                         columnspan = 2)

    source_browsebtn = Button(root,text = "Browse",
                              command = SourceBrowse, width = 15)
    source_browsebtn.grid(row = 1, column = 3,
                          pady = 5, padx = 5)

    destinationLabel = Label(root, text = "Select The Destination:")
    destinationLabel.grid(row = 2, column = 0,
                          pady = 5, padx = 5)
    root.destinationText = Entry(root, width = 50,
                                 textvariable = destinationLocation)
    root.destinationText.grid(row = 2, column = 1,
                              pady = 5, padx = 5,
                              columnspan = 2)
    dest_browsebtn = Button(root, text ="Browse",
                            command = DestinationBrowse, width = 15)
    dest_browsebtn.grid(row = 2, column = 3,
                        pady = 5, padx = 5)

    move_button = Button(root, text="Move Files", command= move_to, width = 15)
    move_button.grid(row = 3, column = 1,
                     pady = 5, padx = 5)

    check_button = Button(root, text="File Check", command= file_check, width = 15)
    check_button.grid(row = 3, column = 2,
                      pady = 5, padx = 5)
                             

def SourceBrowse():
    root.files_list = list(filedialog.askdirectory())
    root.sourceText.insert('1', root.files_list)

def DestinationBrowse():
    destinationdirectory = filedialog.askdirectory()
    root.destinationText.insert('1', destinationdirectory)
    

def move_to():
    file_list = root.files_list
    destination_location = destinationLocation.get()

    SECONDS_IN_DAY = 24 * 60 * 60
    now = time.time()
    before = now - SECONDS_IN_DAY
    def last_mod_time(fname):
        return os.path.getmtime(fname)
    for fname in os.listdir(sourceLocation):
        src_fname = os.path.join(sourceLocation, fname)
        if last_mod_time(src_fname) > before:
            dst_fname = os.path.join(destinationLocation, fname)
            shutil.move(src_fname, dst_fname)

def file_check():
    folderList = filedialog.askdirectory()
    sortlist = sorted(os.listdir(folderList))
    i=0
    print("Files in ", folderList, "folder are:")
    while(i<len(sortlist)):
        print(sortlist[i]+'\n')
        i+=1
    
sourceLocation= StringVar()
destinationLocation= StringVar()


create_widgets()
root.mainloop()
EN

回答 1

Stack Overflow用户

发布于 2021-10-30 13:46:37

正如@acw1668在注释中所述,您应该尝试os.listdir(sourceLocation.get()),因为这会将StringVar类型对象转换为可用作os.listdir函数中的参数的string

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69718023

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档