Just wondering if there is any kind of framework or method of making a Gui that will override (Stay on top of) all other windows in python. Including Games or other programs that seem to "Take over" the computers Graphical processes. Any point in the right direction would be much appreciated...
PS. The OS in question is Windows 7, but a cross platform solution would be appreciated.
You need the SetWindowPos function from the Win32 API. Something like the following should work (see the API link for more details):
import win32gui, win32con
hwnd = get_my_window_handle()
win32gui.SetWindowPos(hwnd, win32con.HWND_TOPMOST, 0, 0, 0, 0, win32con.SWP_NOMOVE | win32con.SWP_NOSIZE)
For a cross-platform solution, you could use wxPython with a wxSTAY_ON_TOP style bit in a main window. I believe that this will give you the behavior you desire on Mac and Unix GUIs as well as Microsoft Windows ones.
Related
i wanted to make a caps-lock and num-lock indicator using python on windows,
but i dont know how to start with and what all modules and library i need to implement my desired output.
THE OUTPUT i desire is that whenever i press CAPS-LOCK the borders of screen should go green and for *NUM-LOCK** blue.
also, does it require a GUI?
(but i want it to be transparent. i don't want minimise,close and maximise buttons on gui) and this all process should be done in background.
Please guide me on how should i approach this.
THANKS A LOT.
There's the WinApi package for python here.
Not very well documented, but after quick look at demos looks like this code works:
import win32api
import win32con
print(win32api.GetKeyState(win32con.VK_CAPITAL))
For green frame you'll need probably some GUI library (GTK+, Qt), but I don't know which one could achieve such effect.
Edit: I figured you can actually use the same WinApi to draw on the screen.
import win32api
import win32gui
import win32con
pen = win32gui.CreatePen(win32con.PS_SOLID, 7, win32api.RGB(255, 0, 0))
dc = win32gui.GetDC(0)
win32gui.SelectObject(dc, pen)
win32gui.MoveToEx(dc, 0, 0)
win32gui.LineTo(dc, 1920, 0)
I have a window that can't be minimized.
I have to prevent users to access it while the gui automation.
How to handle this kind of problems with pywinauto?
It works with autoit and the WinSetTrans() function.
Is there something similar in the pywinauto module?
There is no such method in pywinauto, but it's easy to implement. Thank you for the feature request!
So far you may workaround it so:
import win32gui, win32api, win32con
hwnd = app.dlg.ctrl.handle
ex_style = app.dlg.ctrl.ExStyle()
win32gui.SetWindowLong (hwnd, win32con.GWL_EXSTYLE, ex_style | win32con.WS_EX_LAYERED )
win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(0,0,0), 180, win32con.LWA_ALPHA)
P.S. It was quickly adapted from the known answer (I didn't check it yet). We will include something like SetTransparent method into pywinauto 0.5.3 (planning this week).
I am scripting a SystemC simulation demo under VLAB.
How can I display an image on the screen, as one of the things the demo does at it runs?
I tried opening a window and displaying an image using wx, but this required me to create a wxApp, which blocked the thread of my demo.
Edit:
This question almost amounts to the same question that is often asked: "How can I display an image in Python?", because the VLAB interpreter is Python. However, I'm looking for a solution that doesn't require me to point VLAB at third party libraries.
One simple easy way turns out to be to call on an external app to do this.
On Windows:
import os
os.startfile("myimage.png")
On Linux (Ubuntu):
import os
os.system("xdg-open myimage.png")
This is a little "hacky" in that it is not portable, but for a demo it does the job for me.
Edit:
I found a way that could be considered more portable:
import webbrowser
webbrowser.open("/full/path/to/myimage.png")
Anything more elegant very welcome.
I'm pretty new to programming, and I'm creating a simple text-based game.>
I'm wondering if there is a simple way to create my own terminal-type window with which I can place coloured input etc.
Is there a graphics module well suited to this?
I'm using Mac, but I would like it to work on Windows as well
Thanks
The Tkinter Text Widget will do what you ask. the IDLE main window is implemented as one, if you want to play with an example.
You could use the termcolor library - it that what you're looking for?
On Windows things are trickier. See this SO answer - you should resort to win32console and some ctypes. The answer has some code and links to other articles.
For game programming with Python, I would always recommend PyGame.
It is not very complex and enables you to easily use input, graphics and sound.
As a start:
http://www.penzilla.net/tutorials/python/pygame/
I have a wxPython based app which I am porting to Mac OS X, in that I need to show some alerts which should look like native mac alerts, so I am using pyobjc for that e.g.
import Cocoa
import wx
app = wx.PySimpleApp()
frame = wx.Frame(None, title="mac alert test")
app.SetTopWindow(frame)
frame.Show()
def onclick(event):
Cocoa.CFUserNotificationDisplayAlert(0, 3, 0, 0, 0, "Should i mix wxpython and objc", "hmmm...",
"Cool", "Not Cool", "Whatever")
frame.Bind(wx.EVT_LEFT_DOWN, onclick)
app.MainLoop()
Is there any thing wrong in such mixing of wx and objc code, any failure points ?
I don't think that will work too well, mixing the event loops...
[EDIT: I thought this initially, because the dialog is model and the window behind it is not, and there might be two event loops fighting for control. (Because each window has its own, which is why you can have Carbon and Cocoa windows in (an application of mostly the other type).
On the other hand, the front window - your dialog box - controls the entire event loop if it's model, so it could work actually.]
I'd really suggest you read the Carbon/Cocoa Integration guide. Now, this is more difficult because you're in Python and not C, but it may explain some concepts.
I think on a previous project we implemented our own dialog like this, including customizable texts. (Since we were using wxWidgets/C++ we just implemented this using Carbon APIs with a wxWidgets layer and we looked pretty good. (... and we had a pretty heavily modified version of wx...))
One possible question to ask; I believe you must be using wx version for Mac that rests atop Carbon, because I think the Cocoa version hasn't been released yet. Once the cocoa version is released (for wx) then I would think there would have to be "fewer" issues. A mix of carbon and cocoa sounds problematic to me, but I can't point out specific gotchas.
Any reason you don't just write a custom WX dialog, that inherits from wx.Dialog? The WX demo has a very nice example of that. A little more work, but a cleaner approach.