這是它應該掃描和列舉目錄并對它找到的任何檔案進行散列并將它們附加到字典的代碼所有代碼都單獨作業但我無法在執行緒代碼中觸發散列部分沒有錯誤
import threading
import tkinter as tk
import tkinter.ttk as ttk
import hashlib
import glob
class global_variables:
def __init__(self):
pass
DirSelected = ''
Manifest = []
class App(tk.Tk):
def __init__(self):
super().__init__()
self.title("Progress bar example")
self.button = tk.Button(self, text="Start", command=self.start_action)
self.button.pack(padx=10, pady=10)
def start_action(self):
self.button.config(state=tk.DISABLED)
t = threading.Thread(target=self.contar)
t.start()
self.windows_bar = WinProgressBar(self)
def contar(self):
directories_scanned = [str, 'File,Md5!'] # , separator value ! delineator adds new line
target_dir = '/Users/kyle/PycharmProjects/' # this wont run
files = glob.glob(target_dir '/**/*.*', recursive=True) # Line adds search criteria aka find all all to the
# directory for GLOB
for file in files:
with open(file, "rb") as f:
file_hash = hashlib.md5()
while chunk := f.read(8192):
file_hash.update(chunk)
directories_scanned.append(
file ',' str(file_hash.hexdigest() '!')) # Appends the file path and Md5
global_variables.Manifest = directories_scanned
for x in range(len(global_variables.DirSelected)): # testing to view files TODO REMOVE this
print(global_variables.DirSelected[x])
for i in range(10): #to here
print("Is continuing", i)
print('stop')
self.windows_bar.progressbar.stop()
self.windows_bar.destroy()
self.button.config(state=tk.NORMAL)
class WinProgressBar(tk.Toplevel):
def __init__(self, parent):
super().__init__(parent)
self.title("Progress")
self.geometry("300x200")
self.progressbar = ttk.Progressbar(self, mode="indeterminate")
self.progressbar.place(x=30, y=60, width=200)
self.progressbar.start(20)
if __name__ == "__main__":
global_variables()
app = App()
app.mainloop()
print("Code after loop")
for x in range(len(global_variables.DirSelected)): # testing to view files TODO REMOVE this
print(global_variables.DirSelected[x])
這次代碼將運行,不會顯示任何錯誤
uj5u.com熱心網友回復:
這對我來說可以。我認為您只是檢查了錯誤的變數DirSelected,而該變數實際上是一個空字串。(見我的代碼)
另外,我移動了你的,WinProgressBar以便在執行緒執行時它可用。(對此有例外)。
import threading
import tkinter as tk
import tkinter.ttk as ttk
import hashlib
import glob
class global_variables:
def __init__(self):
pass
DirSelected = ''
Manifest = []
class App(tk.Tk):
def __init__(self):
super().__init__()
self.title("Progress bar example")
self.button = tk.Button(self, text="Start", command=self.start_action)
self.button.pack(padx=10, pady=10)
def start_action(self):
self.button.config(state=tk.DISABLED)
self.windows_bar = WinProgressBar(self)
t = threading.Thread(target=self.contar)
t.start()
def contar(self):
directories_scanned = [str, 'File,Md5!'] # , separator value ! delineator adds new line
target_dir = '/home/jsantos/workspace/blog.santos.cloud/' # this wont run
files = glob.glob(target_dir '/**/*.*', recursive=True) # Line adds search criteria aka find all all to the
# directory for GLOB
for file in files:
with open(file, "rb") as f:
file_hash = hashlib.md5()
while chunk := f.read(8192):
file_hash.update(chunk)
directories_scanned.append(
file ',' str(file_hash.hexdigest() '!')) # Appends the file path and Md5
global_variables.Manifest = directories_scanned
for x in range(len(global_variables.Manifest)): # testing to view files TODO REMOVE this
print(global_variables.Manifest[x])
for i in range(10): #to here
print("Is continuing", i)
print('stop')
self.windows_bar.progressbar.stop()
self.windows_bar.destroy()
self.button.config(state=tk.NORMAL)
class WinProgressBar(tk.Toplevel):
def __init__(self, parent):
super().__init__(parent)
self.title("Progress")
self.geometry("300x200")
self.progressbar = ttk.Progressbar(self, mode="indeterminate")
self.progressbar.place(x=30, y=60, width=200)
self.progressbar.start(20)
if __name__ == "__main__":
global_variables()
app = App()
app.mainloop()
print("Code after loop")
for x in range(len(global_variables.Manifest)): # testing to view files TODO REMOVE this
print(global_variables.Manifest[x])
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/345016.html
上一篇:StringVar()withListboxmultipleorextended-串列轉換為字串
下一篇:如何在函式執行完成后立即終止視窗
