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)'。后一种形式通常用于静态方法。 –