get active window in python - python

I'm bored and just making something for the heck of it in Python. I saw someone typing with spaces between all their letters and decided to make a python script that does this. It was pretty easy, but then I wanted to take it a step further, because copy/pasting from console takes time, so I want to have this script put spaces after every keyboard press but only when I have Discord as the active window. The only things I could find that could give you the active window are from 5-15 years ago, and are all outdated. They say use win32gui, and I pipinstalled it, but it doesn't seem to work.
EDIT: For clarification, I ran "pip install win32gui" and it installed, I opened a python shell and typed "import win32gui" and it said no such module
I looked through the modules and found win32 and according to the help command win32gui is part of its package, so I tried win32.win32gui and it says there's no such attribute
I'm new to coding, I'm not entirely sure what I'm doing.

It seems win32gui isn't recognized in shell which I was using to test if it worked or not, however after just adding it to an actual python script it works just fine.

Related

PIP installed items don't want to work in Windows 10

I'm lost with Python. After troubleshooting with a friend through Discord for literally months I am about to give up. This is my last attempt at trying to get Python fixed on my Windows 10 laptop.
I'm using a Lenovo Legion 5, bought the laptop in November 2020. I've not been able to get anything related to Python to run in the CMD window. I can run Python no problem, but nothing I have installed through pip has ever worked. I can use virtualenvs, but only through PyCharm for example. Python has never really worked through the command line.
Yes I tried reopening the CMD window, rebooted the system many times, ran the CMD as administrator, installed the path variables for both Python and esptool.py but nothing seems to help.
I honestly don't know where to start because none of the 250+ websites I've visited to so far that suggested a fix for any kind of issue I've been experiencing with Python has been working. I can run Python fine by the way, just none of the things installed through pip will work.
Let's start with a use-case:
I'm trying to run esptool.py so installed it with pip install esptool. This install worked fine, and I can confirm it is installed with pip show -f esptool.
However, when running esptool.py version it told me:
'esptool.py' is not recognized as an internal or external command, operable program or batch file.
So I added the local folder from the previous step to the %PATH% variables, after running esptool.py version it gave me a popup asking me with what kind of program I should open this, I didn't select to open with this kind of program from now on. This makes it so that I do not get an error, what now happens is that another window quickly opens and then exits without an error code. So I have no clue what's happening.
What should happen is that it should tell me which version is installed in the CMD window.
There have been a few other things going on with my Windows 10 install, for one, the username that I used during the installation wasn't used to create the user directory. Windows 10 somehow instead chose a name that was related to the first 5 characters of my email address, which is totally strange as I haven't used that string in the installation of Windows 10 at all. This was a fresh install on a new laptop.
Now, after an update of Win10 my user icon doesn't display anymore and I had to change ownership of the 'Windows Apps' folder in order to be able to access it. Changing the ownership also changed the name I now see on the login screen when I boot up the laptop. It changed from the 5 first chars of my email address to my full name in the login screen, only because I took ownership of this folder so I could access it.
There have been a lot of things going on that I think should not be changing all the time, things to do with administrator rights, ownership, etc.
Now, since opening esptool.py doesn't open it, but also doesn't show me an error, I'm clueless and the only thing I can think of is doing a fresh system install, but I have a bunch of projects going on for which I need this laptop in working order and I don't have the mental health (due to corona) left to do a fresh system install. I'm worn down. Not in a dramatic way, I just don't have the spare energy to go through the whole process. So I'm hoping someone can point me in the right way to troubleshoot why my Python doesn't want to work natively.
What happens when running esptool.py version is that I can see it opens a Python window, but without showing any content it closes within a few milliseconds.
What is going on, how do I continue? I hope someone knows how to troubleshoot my system, to find the core of the problem.
It apparently was rather simple. First of all, thanks for the replies! And second of all, thanks for pointing me to superuser.com I wasn't aware of the site and will continue there.
The fix was to use python -m pip install esptool as suggested by Valentin Kuhn.
To answer AKD, I have a lot of experience with programming on my MacBook, but I'm not experienced with actually maintaining the system side, I'm a creative user. It's just that ever since I got a Windows laptop it's been nothing but trouble and after months of chatting about each individual issue with people on Discord nobody has been able to find a solution. I'm not expecting a GUI, just a simple "esptool.py v3.0" was the answer I was expecting from the command line.
Now what I don't understand is that I've never found any hint to anyone suggesting python -m. I will get on superuser to find out more about why the standard instructions that work for most people, don't work for me.
Sorry for using the wrong site for my question, I came on here through another related question and it was past my bedtime and I wasn't thinking clear.

Tkinter transparentcolor working with .py but not with .pyw

root.attributes("-transparentcolor", "white") won't work with a .pyw file extension
Up until yesterday all my tkinter GUIs have been working like they should but two days ago all of them stopped working like I want them to. The line root.attributes("-transparentcolor", "white") has always made the white color transparent, like it should, but now it doesn't work like it used to. It does work properly if I launch the program with a .py file extension but not with a .pyw. Obviously I don't want a console window to open along with all of my programs so this is not something I could just accept. Like I said, this has worked fine up until yesterday.
For the past day I've been doing all kinds of things to try to fix this, I even uninstalled python completely and reinstalled the latest version, but nothing has changed my problem.
I'm on Windows 10 and python 3.9.1 right now (after the reinstall, can't remember what version I had before)
I didn't do a single thing that could've made this happen, I didn't update or change anything before my problem showed up, I simply started my computer in the morning like always.
Does anyone have any clue on what I could try? Could anyone try to do the same thing and see if it works for you?
Code below creates a tkinter window with a transparent background if saved as a .py file, but creates a tkinter window with white background if saved like a .pyw file.
from tkinter import *
root = Tk()
root.config(bg= 'white')
root.attributes('-transparentcolor', 'white')
root.mainloop()
Update:
Hmm... I've discovered something interesting!
If I start my program through cmd prompt with pythonw my problem still appears, but if I do the same thing with a cmd prompt running as administrator it all works like before, I get a transparent background!
Update 2:
I've set pythonw.exe to always run as administrator, and this solves my problem. But if I do that I always get a uac prompt which I never got before, so I haven't fixed my problem, I've just found a tedious workaround.
Allrighty then... I've found a solution, even though I do not like it. I don't have anything against windows or Microsoft, but I really do not like the Microsoft store. But I have to give in, this does solve my problem:
I went and installed python 3.9 from the Microsoft store. I do not know why I did it. I do not know why it helped. I started my .pyw file with the installed program/app called "Python 3.9 (Windowed)" and badaboom, badabing, my problem for some frickin reason, disappeared...
I don't know if this is a viable solution long term (or at all for that matter) but it solves my problem, and after 2 days of continuously smashing my head against the keyboard and wanting to set my pc on fire, I have to accept this as a solution and move on...
Thank u #acw1668 and #martineau for trying to help, I really appreciate it!
Tl;dr:
Download python from Microsoft store and use that program to start the file

Can manim be used in pycharm?

I have been programming with python for about half a year, and I would like to try manim ( the animation programme of 3blue1brown from youtube), but I am not sure where to start. I have not installed it, but I have tried to read up on it. And to be honest I do not understand much of the requirements of the program, and how to run it.
Google has left me without much help, so I decided to check here to see if anyone here is able to help.
From what I understand, you run manim directly in python and the animations are based on a textfile with code i assume is LaTex. I have almost no experience with python itself, but I have learned to use it through Thonny, and later Pycharm.
My main questions are: (Good sources to how to do this without being a wizard would be really helpful if they exist☺️)
Is it possible to install manim in pycharm, and how? Do i need some extra stuff installed to pycharm in order to run it? (I run a windows 64-bit computer)
If i manage to do this in pycharm, Will I then be able to code the animations directly in pycharm (in .py or .txt files), or is it harder to use in pycharm?
All help or insights is very appreciated😅 As I said I am not extremely knowledgeable in computers, but I am enjoying learning how to code and applications of coding😊
I recommend you this playlist
I always uses pycharm for manim.
Firstly i setup python interpreter by just open File->Settings->Projet->Project Interpreter then just press on little gear icon to add python interpreter to Existing environment and locate C:\Python3x\python.exe
Then just open a terminal from left-down corner and run some basic commands to run manim as mentioned in tutorials or manim github page.
Something that works nicely for me is to run manimgl.exe from Python in PyCharm using the subprocess module. It also goes well with using the run shortcut while iterating with small edits.
I like to do this from the script in which my main scene is defined, for example, I have main.py which defines MyScene:
from manimlib import *
class MyScene(Scene):
def construct(self):
...
if __name__ == '__main__':
import subprocess
params = 'manimgl main.py MyScene -c WHITE'.split()
subprocess.run(params,
check=True,
capture_output=True,
text=True)
# Possibly look at captured output here
The code inside if __name__ ... does not execute when the same script is loaded by manim. What is nice is that one can easily add automation steps before or after the actual execution if needed and it keeps everything related in a single script.
Edit: I also end the animations in the construct() method of MyScene with exit() to terminate the preview. I honestly don't know if this is good practice, but it works well for my usage pattern.
Note that this does require that manimgl.exe reside somewhere that is in your path, in my case (Windows) I installed this for my global Python interpreter. I followed the instructions on GitHub and it works for me because the following is in my path:
C:\Users\XXX\AppData\Local\Programs\Python\Python38\Scripts\
It may vary depending on where your Python is installed. For a venv, you could do something like:
params = '.\venv\Scripts\manimgl.exe ...'.split()
Yes, you can
1.Write your code in pycharm
2.save it
3.copy that .py file to where you installed manim. In my case, it is
This pc>> C drive >> manim-master >> manim-master
4.select on the path and type "cmd" to open terminal from there
Type this on the terminal
python -m manim -pql projectname.py
This will do.
To play back the animation or image, open the media folder.

Module won't work in IDLE

This has been troubling me for the past hour. My python script won't run; however, if I select 'check module', input the code, and run the same thing it works. It's a very simple script:
import pyscreenshot as getImage
im = getImage.grab(bbox=(1300,800,1500,850))
im.save("screen.png")
Try running through terminal and it will work.
Although I do not know why this happens exactly (probably ask those who maintain it) I can tell you it is because of some terminal dependant function that is created while running.
The IDLE is not actually a terminal so it cannot run exactly like a terminal (although it outputs the same content, it is not a terminal). For example running os.get_terminal_size() under the IDLE will not work yet the terminal will. There are also some functions in PIL that perform in the same way.
Anyway this post shows a pretty similar code and it is mentioned it doesn't work under the IDLE.
You need to set childprocess=False as IDLE has problems with multiprocessing. IDLE runs user code in a separate process so with pyscreenshot IDLE hangs and seems like the module won't work. You may go through this to know more.
An option is to turn off multiprocessing by setting childprocess=False.
Try:
import pyscreenshot as getImage
im = getImage.grab(bbox=(1300,800,1500,850), childprocess=False)
im.save("screen.png")

Python - I can't see my what my error is because the window disappears immediately

I am pretty new to Python and I have been pretty annoyed with this problem. I am not sure if this matters, but I run my .py file with Python 2.7.6 with python installed on my computer, not using it on any online thing or other program. Every time I come across an error, my program works fine until it comes to the error, but the window disappears right before I can possibly read whatever the error said it was... Anyways, I haven't been able to find out what is wrong with my programming, and I am tired of guessing and guessing what is wrong. How can I extend the time so I can read the error message? Or something like that? Thanks
You should run it from command window/terminal instead of double clicking on the file.
Yes, as #shortfellow said, running it from the terminal should work:
python your_file.py
Another tip that helps in Python, if you are having a hard time with errors, is to use iPython for debugging/testing code on the spot. You can give it a try.
http://ipython.org/

Categories