Python tkinter和全球。全球一起工作

2024-04-26 21:05:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我是tkinter新手,正在尝试打开explorer(在windows上),以便选择要在程序中使用的文件夹。我为tkinter找到了一个模板,并对其进行了修改,使其与我的函数以及我需要的filepath配合使用。在我尝试使用tkinter来“选择我的文件夹”之前,我已经在glob.glob函数中手动编写了这个目录,就像这样glob.glob(r'C:\Users\Desktop\Spyder\*.log')(而且它是有效的)。因此,我的新ide将路径名输入从r'C:\Users\Desktop\Spyder\*.log'替换为存储相同路径名的variabel,但现在它使用tkinters askdirectory()来查找目录inteed。你知道吗

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 loopfilepath_search工作(它打印“ok”)。但是for循环中的run这个词没有打印出来,我猜是因为它在那之前被卡住了?一个有更多经验的人能帮我吗?谢谢


Tags: thetoimportselfrightlogfortkinter
1条回答
网友
1楼 · 发布于 2024-04-26 21:05:55

我猜问题是由传递给glob.glob的内容引起的,因为它找不到任何内容。似乎这主要与您在右日志文件路径的末尾添加'字符有关。你知道吗

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)

相关问题 更多 >