编译python程序后,如何输入参数 [英] after compiling python program, how to input arguments

查看:80
本文介绍了编译python程序后,如何输入参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import sys 之后,我使用sys.argv 来获取输入参数.

After import sys, I use sys.argv to get input arguments.

但是在我用pyinstaller编译我的程序后,exe程序不会接受我的输入.

But after I compile my program with pyinstaller, the exe program will not accept my input.

它只会使用我为程序设置的默认值.如何编译它并让 exe 文件接受我的输入?

It will just use the default value I set up for the program. How can I compile it and let the exe file accept my input?

我希望我不需要为它编写 gui.抱歉回复晚了.代码如下:

I hope I don't need to write a gui for it. Sorry for late reply. Here is the code:

import sched, time

import sys



s = sched.scheduler(time.time, time.sleep)




def do_something(sc,wait_time): //wait_time is an integer representing how many seconds to wait.

    //  Here will be the code for doing something every after "wait_time " seconds

    sc.enter(wait_time,1,do_something,(sc,wait_time))  

try:
    wait_time = int(sys.argv[1])
except IndexError:
    wait_time = 5    


# s.enter(wait_time, 1, do_something, (s,))


s.enter(wait_time,5,do_something,(s,wait_time))
s.run()    

如果我使用python this_script.py"运行它,它将等待我输入的等待秒数.但是在我用pyinstaller编译后,我双击了exe文件.我没有地方可以输入一个数字,也就是wait_time.

If I run it with "python this_script.py", it will wait for my input of how many seconds for wait. But after I compiled it with pyinstaller, I double clicked the exe file. There is no place for me to input a number which is the wait_time.

推荐答案

如果点击exe打开:

通常,当您双击 exe 时,只有一个参数是 .为该exe创建快捷方式.在该快捷方式的属性中,您将看到一个名为 Target 的属性,其中包含 将其更改为 <arg1>.当你使用这个快捷方式打开exe时,它会调用目标,也就是这个调用<arg1>.然后您可以使用 sys.argv

Usually, when you double click the exe, there is only one argument which is <EXEfilename>. Create a shortcut for that exe. In the properties for that shortcut, you will see a property called Target which will contain <EXEfilename> change that to <EXEfilename> <arg1> <arg2>. When you use this shortcut to open the exe, it calls the target, which is this call <EXEfilename> <arg1> <arg2>. You can then access arg1 and arg2 using sys.argv

如果您使用命令行:

只需将其称为 C:\><EXE文件名><arg1>

这篇关于编译python程序后,如何输入参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆