我喜歡輸入帶有 Tkinter 條目的路徑,它成為使用 os.walk 的程式的一部分。
該程式列印所有條目及其路徑(稍后我將添加更多)。
我的問題是,我什至在 tkinter 中輸入內容之前就遇到了錯誤。
我的代碼如下所示:
import os
def run_exe():
root_path= root_path_entry
for root, subfolders, filenames in os.walk(root_path):
print( os.path.join(root, filename))
master = tk.Tk ()
root_path_label= tk.Label(master, text= "Root Here")
root_path_entry= tk.Entry(master)
Enter_Button= tk.Button(master, text="Start", command= run_exe())
root_path_label.grid (row=1, column=0)
root_path_entry.grid (row=1, column=1)
Enter_Button.grid(row=2, column=1)
mainloop()
有任何想法嗎?
Traceback 看起來像這樣:
TypeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_920/1950915600.py in <module>
12 root_path_entry= tk.Entry(master)
13
---> 14 Enter_Button= tk.Button(master, text="Start", command= run_exe())
15
16 root_path_label.grid (row=1, column=0)
~\AppData\Local\Temp/ipykernel_920/1950915600.py in run_exe()
4 def run_exe():
5 root_path= root_path_entry
----> 6 for root, subfolders, filenames in os.walk(root_path):
7 print( os.path.join(root, filename))
8
~\anaconda3\lib\os.py in walk(top, topdown, one rror, followlinks)
340 """
341 sys.audit("os.walk", top, topdown, one rror, followlinks)
--> 342 return _walk(fspath(top), topdown, one rror, followlinks)
343
344 def _walk(top, topdown, one rror, followlinks):
TypeError: expected str, bytes or os.PathLike object, not Entry
uj5u.com熱心網友回復:
試試這樣:
from tkinter import *
import os
def run_exe():
root_path= input_variable.get()
for root, dirnames, filenames in os.walk(root_path):
for filename in filenames:
print( os.path.join(root, filename))
window = Tk()
# Create widgets
input_variable = StringVar(window)
input_variable_label= Label(window, text= "Root Here").grid(row=1, column=0)
entry_variable = Entry(window, textvariable=input_variable).grid(row=1, column=1)
button_submit = Button(window, text="Start",command=run_exe).grid(row=2, column=1)
window.mainloop()
輸出 :
C:\Data\Config\test.ini
C:\Data\Config\nice\test.json
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/474321.html
上一篇:Tkinter得到檢查按鈕檢查
