[英]Python - tkinter and glob.glob working togheter
Im new to tkinter
and trying to open the explorer
(on windows) so that i can chose what folder i want to use in my program. 我是
tkinter
新手,并尝试打开explorer
(在Windows上),以便我可以选择要在程序中使用的文件夹。 I found a template for tkinter
and altered it to work with my function and how i need the filepath
to be. 我找到了
tkinter
的模板,并对其进行了更改,使其可以与我的功能以及我需要的文件filepath
一起使用。 Before i tried using tkinter
to "select my folder", i had manually writen the directory in the glob.glob
function like this glob.glob(r'C:\\Users\\Desktop\\Spyder\\*.log')
(and it worked). 在尝试使用
tkinter
来“选择我的文件夹”之前,我已经在glob.glob
函数中手动写入了该目录,例如glob.glob(r'C:\\Users\\Desktop\\Spyder\\*.log')
(并且可以正常工作) )。 So my new ide was to replace the pathname input from r'C:\\Users\\Desktop\\Spyder\\*.log'
to a variabel that stored the same pathname but now it used tkinters askdirectory()
to finde the directory inteed. 因此,我的新
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')
My problem ist that i don´t get the for loop
filepath_search
to work (it prints "ok"). 我的问题是,我没有让
for loop
filepath_search
工作(它打印“确定”)。 But the word run
inside the for loop dose not print, i guess it´s because it gets stuck somewhere before that? 但是在for循环中
run
的单词不能打印,我想是因为它在此之前被卡住了吗? Someone who has more experience with tkinter that can help me? 有人对tkinter有更多的经验可以帮助我吗? Thanks
谢谢
I guess issue caused by what is passed to glob.glob
since it doesn't find anything. 我猜是由什么传递给
glob.glob
引起的问题,因为它找不到任何东西。 It seems that it is mostly related to the fact that you add '
characters at the beggining and end of your right_log_filepath. 似乎这与您在right_log_filepath的开头和结尾处添加
'
字符的事实有关。
In ask_directory_to_folder
function replace: 在
ask_directory_to_folder
函数中,替换为:
log_filepath = "r'"+ str(filepath +"/*.log'")
right_log_filepath = log_filepath.replace('/','\ ').replace(' ','')
find_filenames(right_log_filepath)
With: 附:
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.