Powering off multiple monitors programmatically [duplicate] - python

This question already has an answer here:
Cpp how to turn off specific monitor?
(1 answer)
Closed 5 years ago.
I'm looking for a way to programmatically power off multiple monitors.
Note: This doesn't mean power off ALL monitors.
The languages I'm currently able to compile in with my current enviorment is python/C/C++. I am aware of the easy C++ way of.
SendMessage(GetConsoleWindow(), WM_SYSCOMMAND, SC_MONITORPOWER, 2);
However this powers off all the monitors the system has on, which isn't the result I'm after.
Let's say I have a Window with the name "Application Window", now I want to power off all of the monitors that does not have this app window open.
Note: Ideally it'd be implemented with python, however not needed.
Edit:
Found this, however I'm unable to recreate it and have the desired functionality.
Cpp how to turn off specific monitor?

Using ctypes you can access the winapi functions you mentioned:
import ctypes
WM_SYSCOMMAND = 0x0112
SC_MONITORPOWER = 0xF170
window = ctypes.windll.kernel32.GetConsoleWindow()
ctypes.windll.user32.SendMessageA(window, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
The msdn documentation:
SendMessage
GetConsoleWindow
WM_SYSCOMMAND
You should use GetDesktopWindow:
window = ctypes.windll.kernel32.GetDesktopWindow()

Related

How to check if a window is showing [duplicate]

This question already has answers here:
Can I detect if a window is partly hidden?
(2 answers)
How to determine if any part of the window is visible to Screen?
(1 answer)
How to get only the visible part of a window (Windows, gdi32, user32, etc)
(2 answers)
Closed 8 months ago.
I want to get a window's clip box so I can detect if the whole window is displayed.
package: win32gui
I know that IsWindowVisible() from the Windows API will return true if a window is NOT minimized, but it won't detect if the window is hidden behind other windows.
I also know that in C++, I could use GetWindowDC(); GetClipBox(); ReleaseDC(); to get a RECT struct with the coordinates of the smallest bounding rectangle of the window (I got this from another post on StackOverflow, but I haven't tested it).
In Python, I can use:
# That just returns a window by the title
handle = getWindowByTitle("Skype") # "Skype" is just an example
winDC = win32gui.GetWindowDC(handle)
# this function doesn't seem to exist/be implemented in win32gui
win32gui.GetClipBox(winDC, '''This also needs a RECT struct''')
win32gui.ReleaseDC(winDC)
How can I use GetClipBox() in Python? Or, is there any other way to check it?
I would like to not use any other package, because I want my app to have as few dependencies as possible.
I have also heard of ctypes, but I have never used them. I am not sure how it works, and/or if it can help here.

How do programming languages draw GUI like Tkinter for example?

I'm a comp sci student studying some compiler design and I have a quick question that bugs me to no end.
I'm currently writing an Interpreter in JavaScript (run on nodeJS) that takes statements like:
x = 4
print x
Which would result in the console output:
4
I can parse these statements pretty easily and have them output stuff to the console window. But how would this process work with GUI applications?
The way I understand it is, let's take Python for example.
When you run python in the command line, it launches a console application which takes in python commands and interprets them. (I know it translates to bytecode first, but it does eventually get interpreted).
So like if it sees 1+1, I understand how it can parse this and return 2 to the console window that it is already running. Python in this case is itself is a console app, so it's intuitive that console output from user-inputted instructions can also be on the console.
I've been able to do THAT. But this ALSO works in python:
from tkinter import*
t = Tk()
How does THAT work? Does the Python Interpreter/VM somehow call a Win32 API function? Or does it draw its own window?
Thank you in advance, for any help given to clarify this.
tkinter is essentially just a Python interface to the Tk library. This is an open source library that runs on all popular operating systems and provides a consistent API for GUI elements.
I don't think Python has any built-in GUI functions, it relies on external libraries like this.
Somebody at some point long ago wrote a library that could directly access the screen and turn pixels on or off. Then, they wrote a function that takes two x,y pairs and computes all the pixels that need to be turned on to draw a line. The library would then call the function to turn pixels on or off.
Someone else then created a library that sits on top of that library, and uses it to not just draw lines, but draw boxes or circles and so on.
Someone else creates a library on top of that which can read font descriptions and turn that into text on the screen. And then someone creates a library that combines the font library and the line library to create a library that creates windows and checkbuttons. And then someone figures out how to add color, and object rotation, and 3d effects, and on and on.
Ultimate we end up with something like tkinter, which is a library that has functions for creating an input widget, which calls a tcl/tk library which converts the python to tcl, and which calls an X11 or DirectX or Win32 or Cocoa or OpenGL library which takes the input and calls some other function that ultimately turns a pixel on or off on the physical display.
When you deal with programming, A LOT of what you are able to do comes down to existing libraries and APIs. If you had to reinvent the wheel every time, you'd never get anything meaningful done.
A simpler example is your print() call. This is mearly a wrapper that writes to stdout. The bash shell / OS you're using handles what happens to stdout. GUIs are more or less the same thing, with just a slightly more complicated path.
tkinter is a python library for generating GUI interfaces. It itself, is nothing more than a wrapper for the more general, Tk library. Tk is a general purpose GUI library that works across platforms. It does this by creating utilizing code that's customized for each operating system's GUI library. It's the OS* itself that ends up creating the GUI.
*This is kind of a generalization as in some operating systems (such as those that utilize something such as Gnome) the GUI interface is more decoupled from the OS than one would often think.

Is It Possible to Embed a Video In a Tkinter Window? [duplicate]

I posted a similar question a while ago but haven't received any answers so i thought i might ask a more generic question:
Does anyone know how to, through any means, output true video, not a rapidly changing sequence of images, in a Tkinter window with python?
You probably need an extension like TkVideo or Quicktime to do that, not sure there is a python style wrapper for it available yet.
https://github.com/patthoyts/tkvideo

How can I simulate ctrl alt del [duplicate]

This question already has answers here:
Control-Alt-Delete from python or command line
(7 answers)
Closed 7 years ago.
I am trying to generate a python script command line for the shortcut in ctrl alt del or go directly in "change a password", Or any executable file for that shortcut in windows 8.
I already refer to this question but its not working for me.
.NET Simulate Ctrl+Alt+Del Sendkeys
My target is create an app or way directly to change a password in windows 8 or ctrl alt del and then click change a password
Any suggestion/comments, thanks in advance.
As all of the answers on that question state, its very unlikely that you will be able to send this specific key combination.
Crtl+Alt+Del is a privileged key combination in Windows, and I'd guess any rudimentary attempt to simulate it won't be acknowledged by Windows - for a good reason.
Most answers I've seen suggest you'll need to either use the win32com library or ctypes in Python. I won't link to them, but they are searchable using "python simulate crtl alt del".
In either case, you still need to run the app at elevated privileges which still negates the ease of use.
I'd strongly recommend that you don't do this.

Only allowing one instance of a pyqt4 application [duplicate]

This question already has answers here:
PyQt - how to detect and close UI if it's already running?
(3 answers)
Closed 7 years ago.
I have created a pyqt4 app and I want to make it so only one instance (of QApplication) is allowed to run.
The program reads and writes audio files, and if more than 1 instance is running, Windows (linux is fine) throws errors that 2 programs are trying to access the same files. I see a lot of java and C apps that will display a simple dialog if the program is already running, I just want to know how to do this in pyqt4.
A little help?
This kind of programming pattern is called a "singleton" instance or a "singleton application".
Usually it is done with a global mutex or by locking a file early in the life of the program.
And when you program launches, if the file handle is already locked, then you exit.
Qt Solutions has it here: http://doc.qt.digia.com/solutions/4/qtsingleapplication/qtsingleapplication.html
https://qt.gitorious.org/qt-solutions/qt-solutions/source/841982ceec9d30a7ab7324979a0fd5c9c36fd121:qtsingleapplication
It would probably take a bit of work to get those global mutexes/locks to work in pyqt, since pyqt doesn't have the qt-solutions part in it yet as far as I could tell.
Here is an alternative that uses a cross platform python script:
Python: single instance of program
Hope that helps.
Thanks. I Used https://gitorious.org/qsingleapplication/qsingleapplication/source/ca13324b0f5bdfcaf4e379a78108f0bd85fed98a:qSingleApplication.py#L66 And Called QSingleApplication On My MainWindow And Works Fine

Categories