我正在制作一个打开文件并对它做一些事情的程序,我想知道是否有一种方法可以让你点击一个文件,它会在程序中打开它,而不是进入程序,点击打开,然后浏览文件以找到它,或者只是一种您可以单击“打开方式...”并选择您的程序的方式。如果有帮助,这里是代码:
from tkinter import *
from tkinter import filedialog
from subprocess import *
import os
root = Tk()
root.title("Snake converter")
def open_file():
filename = filedialog.askopenfilename(filetypes = (("Snake files", "*.sim"),("Python Files", "*.py"),("All files", "*.*")))
filenametmp = filename + ".tmp"
print filename + " is being compiled. Please wait..."
tf = open(filenametmp, "a")
f = open(filename, "r")
filecontents = f.read()
tf.write("from simincmodule import *" + "\n")
tf.write(filecontents)
os.chdir("C:\Snake\info\Scripts")
print os.getcwd()
call(["pyinstaller", filenametmp])
os.remove("C:/Snake/info/Scripts/build")
f.close()
tf.close()
print "\n\n----------------------------------------------------------------------\n\nFinished compiling " + filename + ". Find the program under [filename]/[filename].exe"
openbutton = Button(root, text = "Open", width = 10, command = open_file)
openbutton.pack()
root.mainloop()
任何帮助或建议将不胜感激。
谢谢!