Opening Full-Screen Processes From Tkinter Hangs First Time - python

I am writing a game-launching menu in Python 2.6 which uses subprocess.popen() to run your selected game. My problem is that the first time I run any game from the menu, it hangs: I get an hour glass over my Python app, and the game which I launched does nothing. I an see the game's icon on the task bar, but clicking on it does nothing. I have to kill the game's process in Task Manager. After this happens once, I can then launch any game, including the one I just had a problem with, and it works fine. I can even exit from my Python app and re-run it, and I don't have the problem. But I will get this problem again after a reboot. I've narrowed the offending code down to:
import os
import subprocess
from Tkinter import *
def reportEvent(event):
if event.keysym == "Alt_L":
os.chdir("\\path\\to\\game")
si = subprocess.STARTUPINFO()
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
subprocess.Popen("game.exe", startupinfo=si).wait()
root = Tk()
root.option_readfile("optionDB")
root.bind('<KeyPress>', reportEvent)
root.mainloop()
The following code does not have this problem (initializes TK but does not use its main loop):
import os
import subprocess
from Tkinter import *
root = Tk()
root.option_readfile("optionDB")
os.chdir("\\path\\to\\game")
si = subprocess.STARTUPINFO()
si.dwFlags |= subprocess.STARTF_USESHOWWINDOW
subprocess.Popen("game.exe", startupinfo=si).wait()
I can use my launcher to open non-fullscreen (windowed) applications without this problem occurring. It only happens for full-screen games. Any ideas? Thank you.
Edit: Based on a suggestion below, I tried the same code with Python 3.4, but I still get the same behavior.

Related

Tkinter not working the first time it runs with withdraw()

I am trying to make a Tkinter script to select files through Windows File Explorer. I don't need the Tkinter window to show, just the File Explorer interface.
import tkinter
from tkinter import filedialog
import os
window = tkinter.Tk()
#window.geometry("1x1")
window.withdraw()
def open_files():
files = filedialog.askopenfiles(mode='r')
global filenames
filenames = [os.path.abspath(file.name) for file in files]
window.destroy() # analysis:ignore #says "window" is undefined becasue of "del window" below
window.after(0, open_files)
window.mainloop()
del window
The first time I run this in Spyder, if window.withdraw() is not commented out, the console just shows runfile(*my_file_name*) and the code does... something... in the background, but nothing seems to actually happen. Nothing changes on-screen, but I cannot type in the console so I know the code is running.
If I open a new console tab and run the code with window.withdraw() commented out, everything works, and the Tkinter GUI is visible. If I then run this code again in the same tab, with window.withdraw() not commented out, then the code works as intended, with only the File Explorer window opening up, and the Tkinter GUI staying hidden. This is the case even if I click the "Remove all variables" button in Spyder, so as far as I understand the code is not saving any variables that allow it to run properly after the first time.
My question is, why does this code work the 2nd, 3rd, 4th, etc. time I run it, but not the first time?
I kept playing around, and changed -alpha to alpha and got this error:
TclError: wrong # args: should be "wm attributes window ?-alpha ?double?? ?-transparentcolor ?color?? ?-disabled ?bool?? ?-fullscreen ?bool?? ?-toolwindow ?bool?? ?-topmost ?bool??"
So I ended up changing window.attributes('-alpha',0) to window.attributes('-topmost',True, '-alpha',0), and this works! It brings up File Explorer on the first run without showing the Tkinter window. Thank you #Thingamabobs for your help.
My final code is:
import tkinter
from tkinter import filedialog
import os
window = tkinter.Tk()
window.attributes('-topmost',True, '-alpha',0)
filenames = [os.path.abspath(file.name) for file in filedialog.askopenfiles(mode='r')]
window.destroy()
del window

How to make my Program Display Always on top

Program Info & Problem
I have created a Python Program Using Pygame Module which displays the Ads on the monitor.
It shows The Ad on screen But as soon as I launch different applications like kodi or vlc or chrome, etc. It goes behind those applications.
The Problem is: The program runs but behind those applications if these applications are launched after my Ad Program.
Ideal Working
Program Laucnhed
Ad Displayed on screen
Launched Other Application
The Program still displayes the ad on top of screen.
System Info
OS: Linux - Ubuntu 20
Language: Python
Module: Pygame, Pymovie, GTK3+
Architecture: amd64
Desktop Enviroment: OpenBOX
Code Launch: CLI using a bash script which launches the python program of advertisment.
Sample Screenshot of Advertiesment
Please Help!
Thank you.
Looks like the best answer I can find is from this outdated website (https://www.mail-archive.com/pygtk#daa.com.au/msg01370.html)
they say to use code below, it should work on all OSs...things are never that easy
transient.set_transient_for(main_window)
Alternatively I have four other answers lol
Taken from (How to keep a python window on top of all others (python 3.1))
stackoverflow user pyfunc says for windows you can just do
import win32gui
import win32con
win32gui.SetWindowPos(hWnd, win32con.HWND_TOPMOST, 0,0,0,0,
win32con.SWP_NOMOVE | win32con.SWP_NOSIZE)
Since this only works for windows I think you should try python-tinker, I believe it works on linux
root = Tk()
root.wm_attributes("-topmost", 1)
Also whatnick says you can use PyGTK
gtk.Window.set_keep_above
reference: How to make python window run as "Always On Top"?
Let me know if any of these work for you, I will keep looking for a better answer.
I think PyWinCtl can make the trick in most cases. You can invoke alwaysOnTop() once for your window, or you can call it inside your loop to assure it stays on top after other apps open. Check this:
import tkinter as tk
import pywinctl as pwc
class Window(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.overrideredirect(True)
self.geometry('300x200')
self.config(background='black')
self.attributes('-alpha', 0.5)
self.label = tk.Label(text="Hello World!")
self.label.pack(fill=tk.BOTH, expand=1)
self.wait_visibility()
self.window = pwc.Window(int(self.frame(), base=16))
# Call it once at the beginning of your program...
try:
self.window.alwaysOnTop()
except:
pass
self.counter = 0
self.display()
def display(self):
if self.state() == "normal":
try:
# ... or call it repeatedly to assure it stays on top
self.window.alwaysOnTop()
except:
# On Linux, sometimes it takes more time to recognize the new window
self.window = pwc.Window(int(self.frame(), base=16))
self.label.config(text=str(self.counter))
self.counter += 1
self.after(1000, self.display)
root = Window()
root.mainloop()

Re-launch a software minimized

I run MediaPlayerClassic, as a minimized window in the taskbar (see here and the other answer too), with:
import subprocess, win32con, win32gui, win32process, win32api
info = subprocess.STARTUPINFO()
info.dwFlags = subprocess.STARTF_USESHOWWINDOW
info.wShowWindow = win32con.SW_MINIMIZE
app = subprocess.Popen(r'c:\path\to\mpc-h64.exe "d:\mp3\song.mp3"', startupinfo=info)
It works. But sometimes, I would like to replace the currently-playing-song by another one, and I'm sending the same previous code (with another mp3).
Since MediaPlayerClassic allows only 1 instance, re-running the previous code does replace the currently-playing-song by the new one indeed, and this is what I want, so it works too. Except that the new window is no more minimized. This is probably because no new process is started: it reuses the previously existing process, and MediaPlayerClassic "opens the window" when it receives a new MP3 to play (?).
Adding:
time.sleep(0.2) # required, if not the next action happens before the MP3 changes
def callback(hwnd, hwnds):
text = win32gui.GetWindowText(hwnd)
if win32gui.GetClassName(hwnd) == 'MediaPlayerClassicW':
win32gui.ShowWindow(hwnd, win32con.SW_MINIMIZE)
return True
win32gui.EnumWindows(callback, [])
nearly solves it, except that the window quickly flashes and then minimizes.
Question: how to relaunch a .exe already started like MediaPlayerClassic and keep it minimized? Should something else than subprocess.Popen be used here?

Python appscript OSAX display_dialog: how to move it to foreground

I am playing around with the example from here; esp. I have this code:
from osax import *
sa = OSAX()
print sa.display_dialog("Python says hello!",
buttons=["Hi!", "Howdy!", "Duuuude!"],
default_button=3)
The dialog always opens in the background. How can I open it in (or move it to) the foreground?
Add an activate command to give your Python process the window manager application focus.
sa = OSAX()
sa.activate()
print sa.display_dialog(...)
You can also choose to not GUIfy your Python process by targeting a faceless background app as described here.
This works for now:
def go_foreground():
from AppKit import NSApp, NSApplication
NSApplication.sharedApplication()
NSApp().activateIgnoringOtherApps_(True)
go_foreground()

How can I ensure that the application windows is always on top?

I have a simple Python script that runs in a console windows.
How can I ensure that the console window is always on top and if possible resize it?
Using Mark's answer I arrived at this:
import win32gui
import win32con
hwnd = win32gui.GetForegroundWindow()
win32gui.SetWindowPos(hwnd,win32con.HWND_TOPMOST,100,100,200,200,0)
To do this with the cmd window, you'll probably have to invoke a lot of win32 calls.
Enumerate all the windows using win32gui.EnumWindows to get the window handles
Find the "window title" that matches how you run your program. For example, doubling clicking on a .py file on my system the window title is "C:\Python26\python.exe". Running it on a command line, it is called c:\Windows\system32\cmd.exe - c:\python26\python.exe test.py
Using the appropriate title get the cmd window handle.
Using win32gui.SetWindowPos make your window a "top-most" window, etc...
import win32gui, win32process, win32con
import os
windowList = []
win32gui.EnumWindows(lambda hwnd, windowList: windowList.append((win32gui.GetWindowText(hwnd),hwnd)), windowList)
cmdWindow = [i for i in windowList if "c:\python26\python.exe" in i[0].lower()]
win32gui.SetWindowPos(cmdWindow[0][1],win32con.HWND_TOPMOST,0,0,100,100,0) #100,100 is the size of the window
If you are creating your own window, you can use Tkinter to create an "always on top" window like so:
from Tkinter import *
root = Tk()
root.wm_attributes("-topmost", 1)
root.mainloop()
And then put whatever you want to have happen within the main loop.
If you are talking about the command prompt window, then you will have to use some Windows-specific utilities to keep that window on top. You can try this script for Autohotkey.

Categories

Resources