簡體   English   中英

python-tkinter和glob.glob工作togheter

[英]Python - tkinter and glob.glob working togheter

我是tkinter新手,並嘗試打開explorer (在Windows上),以便我可以選擇要在程序中使用的文件夾。 我找到了tkinter的模板,並對其進行了更改,使其可以與我的功能以及我需要的文件filepath一起使用。 在嘗試使用tkinter來“選擇我的文件夾”之前,我已經在glob.glob函數中手動寫入了該目錄,例如glob.glob(r'C:\\Users\\Desktop\\Spyder\\*.log') (並且可以正常工作) )。 因此,我的新r'C:\\Users\\Desktop\\Spyder\\*.log'是將r'C:\\Users\\Desktop\\Spyder\\*.log'輸入的路徑名替換為存儲相同路徑名的variabel,但現在它使用tkinters askdirectory()來查找目錄inte。

import glob
import os
from itertools import zip_longest
import tkinter as tk
from tkinter import filedialog

#-------------Connect to Access2013------------------ 
class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.pack()
        self.create_widgets()

    def create_widgets(self):

        self.select_folder = tk.Button(self)
        self.select_folder["text"] = "Open WindowsExplorer"
        self.select_folder["command"] = self.ask_directory_to_folder
        self.select_folder.pack(side="top")

        self.quit = tk.Button(self, text="QUIT", fg="red",
                              command=root.destroy)
        self.quit.pack(side="bottom")

    def ask_directory_to_folder(self):
        clerdatabase() # a funktion that resets the autonumber and deleats all data from every table
        print("Open!")
        filepath = filedialog.askdirectory()
        log_filepath = "r'"+ str(filepath +"/*.log'")
        right_log_filepath = log_filepath.replace('/','\ ').replace(' ','')
        find_filenames(right_log_filepath)

root = tk.Tk()
app = Application(master=root)
app.mainloop()

#--------------Scan selected folder for .log files and starts to scan files---------
def find_filenames(right_log_filepath): #finds every file in the chosen filepath

    print(right_log_filepath) # r'C:\Users\Desktop\Spyder\*.log'
    print("ok")
    filenames = [] # list for all the found filenames
    for filepath_search in glob.glob(str(right_log_filepath), recursive=True): #A for loop that opens every .log file in the chosen directory folder 
        print('run') 

我的問題是,我沒有讓for loop filepath_search工作(它打印“確定”)。 但是在for循環中run的單詞不能打印,我想是因為它在此之前被卡住了嗎? 有人對tkinter有更多的經驗可以幫助我嗎? 謝謝

我猜是由什么傳遞給glob.glob引起的問題,因為它找不到任何東西。 似乎這與您在right_log_filepath的開頭和結尾處添加'字符的事實有關。

ask_directory_to_folder函數中,替換為:

log_filepath = "r'"+ str(filepath +"/*.log'")
right_log_filepath = log_filepath.replace('/','\ ').replace(' ','')
find_filenames(right_log_filepath)

附:

from os import path  # should be at the top of your file
log_filepath = path.join(filepath, "*.log")
find_filenames(log_filepath)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM