1
我試圖讓我的程序打開用戶輸入的文件路徑。此條目保存在我的data [0]
列表中,然後使用os.system
函數調用。而不是打開保存的文件路徑,瀏覽器打開並將我帶到「我的文檔」。 1.爲什麼會發生這種情況?2.如何更改此設置讓瀏覽器打開保存的用戶數據?Python:程序在資源管理器中打開不正確的文件路徑?
import Tkinter as tk
import pickle
import os
data = []
class MyPath(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.parent = parent
self.pack()
MyPath.make_widgets(self)
def make_widgets(self):
self.parent.title("MyPath")
#self.textbox = Text(root)
#self.textbox.pack()
# create a prompt, an input box, an output label,
# and a button to do the computation
self.prompt = tk.Label(self, text="Enter your Filepath:", anchor="w")
self.textbox = tk.Text(self)
self.entry = tk.Entry(self)
self.open = tk.Button(self, text="Open", command = self.open)
self.view = tk.Button(self, text="View", command = self.view)
self.submit = tk.Button(self, text="Save", command = self.save)
self.output = tk.Label(self, text="")
self.clear_button = tk.Button(self, text="Clear text", command=self.clear_text)
# lay the widgets out on the screen.
self.prompt.pack(side="top", fill="x")
self.entry.pack(side="top", fill="x", padx=20)
self.output.pack(side="top", fill="x", expand=True)
self.textbox.pack(side="top", fill="x")
self.view.pack(side="bottom")
self.submit.pack(side="bottom")
self.clear_button.pack(side="bottom")
self.open.pack(side="top")
def clear_text(self):
self.entry.delete(0, 'end')
self.textbox.delete(1.0, 'end')
def save(self):
# get the value from the input widget
try:
a = self.entry.get()
result = data.append(self.entry.get())
except ValueError:
result = "Please enter filepaths only"
#saves list to pickle file
#pickle.dump(data, open("save.p", "wb"))
# set the output widget to have our result
self.output.configure(text=result)
print (data)
def view(self):
pickle.load(open("save.p", "rb"))
self.textbox.insert("1.0", str(pickle.load(open("save.p", "rb"))))
print pickle.load(open("save.p", "rb"))
def open(self):
#src = "/Users/matt/Documents/Python/MyApp.py"
dst = data
#path = pickle.load(open("save.p", "rb"))
os.system('Explorer "data[0]"')
if __name__ == "__main__":
root = tk.Tk()
MyPath(root).pack(fill="both", expand=True)
root.mainloop()
任何幫助非常感謝!
致電時實例方法,做'self.method()'而不是'Class.method(self)'。後一種形式通常用於靜態方法。 –