Python脚本(tkinter应用程序)在通过IDLE运行时工作正常,但是在命令提示符下运行相同的python脚本时抛出错误

时间:2019-06-05 16:42:33

标签: python tkinter

我有一个Python脚本,该脚本在通过IDLE运行时将打开一个小的tkinter应用程序,并为每个按钮分配了不同的功能。但是在通过命令提示符运行与python scriptname.py相同的脚本时,它也会打开应用程序,但是如果我第一次单击一个按钮,它将打开另一个相同的应用程序(重复一个),并且如果我第二次单击相同的按钮它将运行时错误抛出为“在当前进程完成其引导阶段之前尝试启动一个新进程”

我试图创建一个bat文件或exe文件来运行我的脚本,而不是通过IDLE运行。

也尝试通过pyinstaller创建exe文件,但是只要单击该按钮,它就会启动一个新的tkinter应用程序

from Tkinter import *
import os
import datetime
import pyscreenshot as ImageGrab

def func_1():
    curdatetime=str(datetime.datetime.now().strftime('%Y%m%d%H%M%S%f'))
    images_dir = 'C:\\Users\\xxx\\xxx\\xxx\\xxx'
    images_path = 'C:\\Users\\xxx\\xxx\\xxx\\xxx\\' + curdatetime + '.jpg'
    **if os.path.isdir(images_dir):
        ImageGrab.grab_to_file(images_path)
    else:
        os.mkdir(images_dir)
        ImageGrab.grab_to_file(images_path)**

def func_2():
    pass


app = Tk()
app.title("Sample Application")
app.geometry("400x50+100+100")
t = Text(app,height=1,width=10)
button1 = Button(app, text="sample1", width=6, command=func_1)
button2 = Button(app, text="sample2", width=12, command=func_2)
b = Button(app, text="Quit", width=6, command=app.quit)
t.pack(side='left',padx=1,pady=1)
button1.pack(side='left',padx=1,pady=1)
button2.pack(side='left',padx=1,pady=1)
b.pack(side='left',padx=1,pady=1)
app.mainloop()

Error while running through Command Prompt:


C:\Users\xxx\xxx\xxx\xxx>python evidence.py
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1541, in __call__
    return self.func(*args)
  File "C:\Users\xxx\xxx\xxx\xxx\evidence.py", line 13, in func_1
    ImageGrab.grab_to_file(images_path)
  File "C:\Python27\lib\site-packages\pyscreenshot\__init__.py", line 81, in grab_to_file
    backend=backend, filename=filename)
  File "C:\Python27\lib\site-packages\pyscreenshot\__init__.py", line 46, in _grab
    _grab_simple, imcodec.codec, to_file, backend, bbox, filename)
  File "C:\Python27\lib\site-packages\pyscreenshot\procutil.py", line 31, in run_in_childprocess
    p.start()
  File "C:\Python27\lib\multiprocessing\process.py", line 130, in start
    self._popen = Popen(self)
  File "C:\Python27\lib\multiprocessing\forking.py", line 258, in __init__
    cmd = get_command_line() + [rhandle]
  File "C:\Python27\lib\multiprocessing\forking.py", line 358, in get_command_line
    is not going to be frozen to produce a Windows executable.''')
RuntimeError:
            Attempt to start a new process before the current process
            has finished its bootstrapping phase.

            This probably means that you are on Windows and you have
            forgotten to use the proper idiom in the main module:

                if __name__ == '__main__':
                    freeze_support()
                    ...

            The "freeze_support()" line can be omitted if the program
            is not going to be frozen to produce a Windows executable.

0 个答案:

没有答案