Python Tkinter with Pyscreenshot script doesn't run after compiling with Pyinstaller - python

My script runs smoothly. However after compiling with Pyinstaller, launching the .exe and clicking the start button, the GUI opens once again and crashes.
def grabberfunc(*args):
im = ImageGrab.grab()
savedir=str(mappa)
savefile="Screenshot_"+str("{:%Y_%m_%d-%H_%M_%S}".format(datetime.datetime.now()))+".png"
savedirfile=join(savedir,savefile)
im.save(str(savedirfile))
def scanning():
interval=deftimeInput.get()
if running:
grabberfunc()
root.after(int(interval)*1000, scanning)
if __name__=='__main__':
root = tkinter.Tk()
...
There is a basic GUI with 2 buttons: 'Start' sets the 'running' variable 'True', the 'Stop' vice versa.
If the script runs the GUI doesn't open up again and runs as I want it to.

Finally found the solution.
Instead of importing the pyscreenshot module, the ImageGrab module should be imported from PIL
So the correct import is:
from PIL import ImageGrab
After compiling the script by pyinstaller, the exe runs fine.

Related

How to embed Tkinter internally with C++?

I'm writing a C++ .dll project, which is injected into another process, and I'm trying to implement embedded Python code. As far as I have described it, this is working. For instance when I inject the .dll, I run the following code:
Py_Initialize();
FILE* PScriptFile = std::fopen("D:\\test.py", "r");
if (PScriptFile) PyRun_SimpleFile(PScriptFile, "D:\\test.py");
test.py is a simple script which opens and writes to a text file. Thus, I know that my .dll is running the script because I can see that this text file is created in the directory of the main executable. Now, I want to run the following script:
import tkinter as tk
window = tk.Tk()
window.protocol("WM_DELETE_WINDOW",window.destroy)
window.mainloop()
I know that this script works on its own. However, when I try to embed it, no Tkinter window pops up.
Thanks

The following code is running through python IDE but not from CMD or by direct execution

import webbrowser
import pyautogui
import time
webbrowser.open("https://meet.google.com/huq-etkk-pwv",new=0,autoraise=True)
#auto_input
time.sleep(10)
pyautogui.hotkey('ctrl','d')
pyautogui.hotkey('ctrl','e')
time.sleep(2)
#custom coordinates are adviced
#coordinates work on standard chrome window with 1920*1080 screen size 16:9 ratio
pyautogui.click(972,428)
When i run this via CMD i get an error in the module pyautogui with Tk() name not defined and when i try to execute this directly it just opens a window of CMD and then stops and does nothing but works perfectly when i run it via the Python IDE.
Your code works fine for me on my Kubuntu (18.04.)
Which IDE do you use? Is it possible, that it by default runs a different version of python than your CMD (e.g. 3.7 in the IDE and 2.7 in the CMD)?
P.S.
would have posted it in a comment but lack the reputation

Import module error when running os.system virtualenv OpenCV

I'm trying to build a simple Python GUI around OpenCV so I can run facial recognition commands easily. I'm using a Raspberry Pi thus Raspbian to do this
It's a case of clicking a button and an os.system command executes allowing various functions to run.
The issue is with trying to run these functions whilst in the Virtual Python Environment. I need to be inside the virtualenv to gain access to the required modules however I understand every time a os.system command is ran, a new shell is created therefore taking me out the virtual environment.
I've looked into running my functions in one os.system however I still get the import module error.
Something that i assumed would take a few mins to build is taking me days.
Any help on this would be amazing.
Thank you.
Here is my current code:
from tkinter import *
import os
from tkinter import messagebox
# creating tkinter window
root = Tk()
root.geometry('500x500')
root.title("Student Attendnace System")
def stillImage():
os.system("/home/pi/.virtualenvs/cv/bin/activate & python recognize_faces_image.py ---encodings encodings.pickle --detection-method hog --image examples/example_01.jpg")
btn3 = Button(root, text = 'Detect Faces From Image', command = stillImage)
btn3.grid(column=1, row=2)
mainloop()
The idea is to enter the virtual environment and execute another python script with added facial detection arguments.
NOTE: running this in the terminal works fine.
I would use the python from the virtualenv directly:
os.system("/home/pi/.virtualenvs/cv/bin/python recognize_faces_image.py ---encodings encodings.pickle --detection-method hog --image examples/example_01.jpg")
To elaborate a script run using a python executable from a virtualenv will look for libraries relative to the python executable, i.e. inside the virtual environment.

Making A simple GUI program in Sypder IDE on Anaconda

I followed this post to write a simple gui program in my Spyder IDE on anaconda environment.I am using python 3.7. I used below code :
from tkinter import Tk
from tkinter.filedialog import askopenfilename
Tk().withdraw()
filename = askopenfilename()
print(filename)
and
import easygui
print easygui.fileopenbox()
Both of the code seems to run indefinitely and doesn't open any window. Am i missing something? Is it even possible to make a gui program on anaconda environment?

Python program without console/terminal coming out

Is there any way to run a Python 3 script without the terminal or the console popping out?
I tried many ways to hide the terminal at first run but even through I used .pyw extension, included a hide() function and used the --windowed flag when converting my script to an .exe through pyinstaller, the terminal still pops out for a microsecond before disappearing.
import win32console, win32gui
def hide():
window = win32console.GetConsoleWindow()
win32gui.ShowWindow(window, 0)
return True
I've read about a method in which you could run the python script through a C program to hide the terminal before execution but I would like to keep it as simple as I can.
Do you know any way to avoid the terminal flashing out when the script run?
you can hide the console window by using the .pyw extension for the file

Categories