Python Script Failing to Open Firefox.lnk file on Desktop Path - python

TLDR; I have a python script that uses autopygui to click on a file and open it, but it does not work with lnk files located in the same path.
I have a little script using pyautogui to screenshot a desktop icon and save it as a png image on the desktop and doubleclick it to open that image. This works perfect.
However..
I tried implementing the same script on an existing Firefox.lnk shortcut and it fails with the following error:
locateOnScreen Error
Things I have tried:
Running VS code as admin.
locateOnCenterScreen and locateOnScreen.
I have tried others, but I'm blanking because I have tried everything I can think of.
It should open my Firefox.lnk shortcut up, it does not. It only works on png file.

It's because the locateOnScreen(...) function only accepts paths to image files, and .lnk files are not image files. If you manually took a screenshot of the Firefox icon on your desktop, saved it as a PNG file, and passed that to your script, it would open fine.
If you want to launch a program using a .lnk file from Python, you don't need pyautogui at all. Consider using os.startfile(...) instead.

You should put the path of png file in locateCenterOnScreen(...) function instead.
Please also make sure you minimize all active windows so that pyautogui could locate your Firefox short icon.
import pyautogui
pyautogui.hotkey('winleft','d')
icon_location = pyautogui.locateCenterOnScreen('.../FireFox_icon_screenshot.png', confidence=0.9)
pyautogui.moveTo(icon_location)
pyautogui.click(clicks=2)

Related

How can i connect open cv file (for playing video) when i click push button in an UI file?

I tried importing cv2. But When I import it in my python file with ui, it overwrite the code, and it is the first one to run, which supposedly, it should be the other python file. It should just be run when only i click the push button. Kindly help me. :< Beginner here.
Or do you have other ideas on how to play external video like mp4 which is also saved in the same folder.
I'm using qtdesigner, python.

How do I remove the file path from the console window of python exe file?

I have made a simple “Hello World” app in python and turned it into a standalone exe file using pyinstaller. The problem is, the title bar of the console always shows the file path of the exe file. Is there any way to remove the filepath from the window? here is the image
Change the name of the console window in the code
import os
os.system('title MyTitleHere')
If you are using a GUI as part of your program, you can set the title (and icon) there, but for a console-based output, this is the best option. There is no .spec file option to do this.

Tkinter couldn't recognize data in image file

I was using the turtle module with Python to make a project. In the project, an image is displayed, with this line:
screen.bgpic("image.png")
This worked at first, but then I copied the files (both the python file and image.png) onto a flash drive. I tested it on another computer, and it worked fine there, but when I saved it onto the actual computer that I needed it to be on, it didn't work.
I know it's not a problem with the actual turtle, since it works before that part. I also am pretty sure it isn't a file problem since the same file worked before. It says:
Couldn't recognize data in image file "image.png"
Older versions of tkinter don't support .png files.
You can convert GIF or JPEG or another image file type with an external tool.

How do I handle the system windows with Python on Windows OS?

With Selenium Webdriver, I have to upload some files on a website but since the pop-up window for browsing the file location is handled by the operating system and not the browser, I cannot automate that part with Selenium.
So I want to know which framework or module I need to use to work with system windows of Windows OS. Can tkInter or wxPython be used for this?
My script will be used on Windows 7, 8 & 10.
Actually, you can upload files without interacting with upload prompt pop-ups.
To be able to handle file upload with selenium you should send path to file to appropriate input field without clicking on "Upload" button. Try following:
path_to_file = 'C:\\Files\\path\\to\\file' # use your specific path instead
driver.find_element_by_xpath('//input[#type="file"]').send_keys(path_to_file)
P.S. Let me know if this code doesn't work as you expect
You can call autoit3 framework from Python even to open the File Open dialog and fill in the values and press OK or do whatever with the windows. Autoit3 has a dll that can be loaded and called using ctypes. That's what I did in one or 2 projects.
If I understand your question correctly, wxpython or tk won't help you. They can be used to make the windowed UI, not to control other programs.

Start GUI application using subprocess

I am trying to open a file through python that once it is open takes you to a GUI. The link works fine when i just click on it and python seems to locate the file and open it, but the GUI doesn't appear. Please help. This is whay i have been using.
import subprocess
subprocess.Popen("C:/full/path")
I get no track back errors, but the GUI doesn't appear. Thoughts of how I can get it to appear, or what the problem might be?
Thanks
The file you're trying to 'start' is a cmd script. Use this code:
subprocess.Popen("cmd.exe /k C:\full\path\to\file.cmd")
.cmd files are not executable by themselves - you need to invoke cmd.exe to execute them. This is also what windows does when you double-click the file on the desktop.

Categories

Resources