简体   繁体   English

如何制作一个可以用文件打开的程序? (蟒蛇)

[英]How to make a program that can be opened with a file? (python)

I was making a program that opens a file and does a few things to it, and I wondered if there was a way that you could click on a file, and it would open it in the program, instead of going into the program, clicking open, and navigation through the files to find it, or just a way where you can click "Open With..." and select your program. 我正在创建一个程序来打开一个文件并做一些事情,我想知道是否有一种方法可以点击一个文件,它会在程序中打开它,而不是进入程序,点击打开,浏览文件以查找它,或者只是单击“打开方式...”并选择您的程序。 Here is the code if it helps: 这是代码,如果它有帮助:

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()

Any help or suggestion will be highly appreciated. 任何帮助或建议将受到高度赞赏。

Thanks! 谢谢!

"Open with..." usually sends the pathname of the file to sys.argv . “用...打开”通常会将文件的路径名发送到sys.argv So at an appropriate place of your program, add this: 因此,在程序的适当位置添加以下内容:

if len(sys.argv) > 1:
    open_file(sys.argv[1])

(As I said in the comment, you really want to let your open_file take an argument, and have another function like open_file_dialog to open the dialog.) (正如我在评论中所说,你真的想让你的open_file接受一个参数,并且有另一个函数,比如open_file_dialog来打开对话框。)

That leaves the question of how to make something you can "Open with..." in the first place. 这就留下了如何制作一些你可以“开放......”的问题。 If you are on Windows, you should be able to achieve finer control with file association by editing the registry: see this MSDN page for details. 如果您使用的是Windows,则应该能够通过编辑注册表来实现更好的文件关联控制:有关详细信息,请参阅此MSDN页面

Alternatively, a quick-and-dirty way to do it is to make a .bat script that takes an argument and passes it to your python program. 或者,快速而肮脏的方法是创建一个.bat脚本,该脚本接受一个参数并将其传递给您的python程序。 I remember doing this some time ago, but I haven't used Windows seriously for a long time, so I can't tell you how to write the script right off my head. 我记得前一段时间这样做了,但我很长时间没有认真使用Windows,所以我不能告诉你如何编写脚本。

I am a Fan of Autohotkey Tools And Python Launguages, 我是Autohotkey Tools和Python Launguages的粉丝,

An Other Way to do it, is: 另一种方法是:

If you want to run a program, and then Want to opened it with a file ("Open With...") 如果要运行程序,然后想要用文件打开它(“打开方式...”)

You can Think about, 你可以想想,

Making your own Computer Movements Scripts With Keyboard Shortcuts Macros. 使用键盘快捷键宏创建自己的计算机移动脚本。

Step 1: Install (Python27) on your Windows System. 第1步:在Windows系统上安装(Python27)。 Click Here 点击这里

Step 2: Then Install the Python Packages - pyautogui and pywinauto 第2步:然后安装Python包 - pyautogui和pywinauto

You can Use this Msdos Batch Script: 您可以使用此Msdos批处理脚本:

Install.bat INSTALL.BAT

C:\Python27\scripts\pip.exe install pyautogui
pause
C:\Python27\scripts\pip.exe install pywinauto
pause

Now You are Ready, To Make and use this Python Script: 现在您准备好了,制作并使用这个Python脚本:

Example1.pyw Example1.pyw

#run Notepad with "Open..." 
#######################
import pywinauto
pywinauto.Application().start(r"C:\Windows\System32\Notepad.exe c:\test\test.txt")
#######################

Example2.pyw Example2.pyw

#run Notepad
#######################
import pywinauto
pywinauto.Application().start("C:\Windows\System32\Notepad.exe")
#######################


#Open a File - "Open With..." 
#######################
import pyautogui
import time
time.sleep(2)
pyautogui.hotkey('ctrl','o') #Many Programs use Shortcut Ctrl+o to "Open With..." 
time.sleep(.750)
pyautogui.typewrite('c:\\test\\test.txt',0)
time.sleep(2)
pyautogui.hotkey('enter')
#######################

# you can send any text or Keyboard Shortcuts Combinations - Example Copy - pyautogui.hotkey('ctrl', 'c')

Note: If you use a File Path with typewrite Command - You Can Not put a ( single backslash \\ ) you must replace it into a ( double backslash \\\\ ) 注意:如果您使用带有typewrite命令的文件路径 - 您不能放置(单个反斜杠\\),则必须将其替换为(双反斜杠\\\\)

Tip : Python Languages together with AutoPythonlauncher Software is a good Combination - If You Want to make, Toolbars On your Windows Desktop - Executeable Pictures with Python Scripts For Mouse or Touch Device. 提示:Python语言与AutoPythonlauncher软件是一个很好的组合 - 如果您想在Windows桌面上制作工具栏 - 使用Python脚本执行鼠标或触摸设备的可执行图片。 - For more info look at Homepage - 欲了解更多信息,请查看主页

How I would do it: 我该怎么做:

  1. Make it so that your program reads from stdin and writes to stdout 使它成为你的程序从stdin读取并写入stdout
  2. Use the power of the shell. 使用shell的力量。 If you are in a unix shell, just simply do cat infile | ./python myProgram.py > outfile 如果你在unix shell中,只需简单地做cat infile | ./python myProgram.py > outfile cat infile | ./python myProgram.py > outfile

This will give the contents of infile to your program on stdin, then write the output of stdout to outfile . 这将在stdin上为你的程序提供infile的内容,然后将stdout的输出写入outfile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM