У меня есть скрипт на python, который при запуске через IDLE откроет небольшое приложение tkinter и назначит разные функции для каждой кнопки. Но при запуске того же сценария через командную строку, что и python scriptname.py
, он также открывает приложение, но если я нажимаю кнопку в первый раз, она открывает другое такое же приложение (дублирующее), и если я нажимаю на ту же кнопку во второй раз, он выдает Ошибка времени выполнения: «Попытка запустить новый процесс до того, как текущий процесс завершит свою фазу начальной загрузки»
Я пытаюсь создать bat-файл или exe-файл для запуска моего скрипта вместо запуска через IDLE.
Также пытался создать exe-файл через pyinstaller, но он запускает новое приложение 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.