I've use this code to get the top level windows. Is there a way to get only running program in the taskbar? Sorry for the format. Thanks
def windowEnumerationHandler(hwnd, resultList):
'''Pass to win32gui.EnumWindows() to generate list of window handle, window text tuples.'''
resultList.append((hwnd, win32gui.GetWindowText(hwnd)))
topWindows = []
win32gui.EnumWindows(windowEnumerationHandler, topWindows)
Related
I´ve built a simple GUI app, and I´m playing with pystray.
Actually, my script starts as usual, the first thing you see is the GUI.
If you klick on exit, the GUI minimize, and the tray Icon spawns.
For now, i search a way to start my script in this "tray Mode"
here are some informations:
class Hauptfenster:
# Define a function for quit the window
def quit_window(icon, item):
icon.stop()
fenster.destroy()
# Define a function to show the window again
def show_window(icon, item):
icon.stop()
fenster.after(0, fenster.deiconify())
# Hide the window and show on the system taskbar
#staticmethod
def hide_window():
fenster.withdraw()
image = Image.open(os.path.join(application_path, iconFile))
menu = (item('Beenden', Frontend.Hauptfenster.quit_window), item('Einstellungen', Frontend.Hauptfenster.show_window))
icon = pystray.Icon("name", image, "Quicksafe", menu)
icon.run()
Please ask me if you need some more Information and Thanks a lot !
Best regards
Background:
My programm should lay in the autostart of win10 , but i dont want to minimize the window each time i restart my pc
I just added my method hide_window to my main function,
when script starts, you see something moving on to screen for 2ms.. but it disapears very quick... so thats something I can live with
is it possible to un-minimize a minimized window using python on windows 10? (I'm using python 3.8)
I would add more detail but that's really all I need to say.
I combined information from multiple sources and got this to work (Miniconda Python 3.6, Windows 10)
import win32gui
import win32con
def windowEnumHandler(hwnd, top_windows):
top_windows.append((hwnd, win32gui.GetWindowText(hwnd)))
def bringToFront(window_name):
top_windows = []
win32gui.EnumWindows(windowEnumHandler, top_windows)
for i in top_windows:
# print(i[1])
if window_name.lower() in i[1].lower():
# print("found", window_name)
win32gui.ShowWindow(i[0], win32con.SW_SHOWNORMAL)
win32gui.SetForegroundWindow(i[0])
break
# Test with notepad
if __name__ == "__main__":
winname = "notepad"
bringToFront(winname)
The Handler is not optimal; it spits out various processes that are not the windows in the taskbar. However, as long as your window_name is specific I don't think you'll run into problems. If you remove the break, all matches will be "opened".
Sources: Mouse and Python Blog
Another StackOverflow question
I’m building a program in python that will copy large files using robocopy. Since the gui freezes while the copy is done i only have two options:
1. Learn how to do multithreading and design the gui to show the progress and not freeze.
2. Keep the console on after compiling with pyinstaller as an alternative to show robocopy progress while the gui freezes.
I am open to doing multithreading but i’m a beginner and is pretty hard to understand how to make another subprocess for robocopy and from there extract the progress into a label from gui. The option i thought about is to have the cmd console active only while the copy is done. Is it possible? The scenario will be like this:
Open the program (the console will be hidden)
Press the copy button (console pops up and shows the copy progress while the gui freezes)
After the copy is done hide the console again
As i said above. I’m not totally excluding adding multithreading but for that i will need some help.
Thanks!
Please try this code, should be working, please let me know if something wrong:
import tkinter as tk
import os
import subprocess
import threading
main = tk.Tk()
main.title('Title')
frame_main = tk.Frame(main)
frame_main.grid(columnspan=1)
src = 'D:/path/to/the/folder'
dest = 'D:/path/to/the/folder2'
selection_platf = len(os.name)
def copy_build_button():
if selection_platf < 11:
subprocess.call(["robocopy", src, dest, r"/XF", 'BT V_SyncPackage.zip', "/S"])
else: #for linux
subprocess.call(["robocopy", src, dest, "/S"])
def copy_thread():
thread_1 = threading.Thread(target=copy_build_button)
thread_1.start()
button_main1 = tk.Button(frame_main, text="copy_build_button", width=50, height=5, fg="green", command=copy_thread)
button_main1.grid(column=0, sticky='N'+'S'+'E'+'W')
main.mainloop()
I am currently working on a User Interface using TKinter on Python 2.7.12. I started out using Windows 10 where everything went fine. Now that I transfered the code to Ubuntu (which will be the target platform) I am having trouble with an OptionList Widget.
The code for the List looks the following:
...
optionList = ['None']+self.aPandasDataFrame.index.tolist()
omv = StringVar(master=aPopUpwindow)
omv.set("None")
self.om = OptionMenu(aPopUpWindow, omv, *optionList, command = update_aPopUpWindow)
self.om.config(width=15)
self.om.place(x=20,y=20,width=400)
self.om.grid(row=1, column=0)
...
The list seems to be too large for Ubuntu to display. When I click on it, only the silhouette of an OptionList appears without text. By clicking into it, the program behaves as if I actually did a selection. The only way to currently solve this, is to drastically remove items from the list, which is no option for the final program.
Do you have any idea how to make this work?
Thanks for your help.
Sarem
I have just written simple script to launch an applciation and I am trying to use "SendKeys" module to send keystrokes to this application. There is one "Snapshot" button, but I cant get Python to click "Snapshot" button, as the new window is not in focus. So I am planning to use Win32gui module's win32gui.FindWindow and win32gui.SetForegroundWindow functionality. But it gives me error- invalid handle. My app name is "DMCap"
Here is code snippet in Python:
handle = win32gui.FindWindow(0, "DMCap") //paassing 0 as I dont know classname
win32gui.SetForegroundWindow(handle) //put the window in foreground
Can anyone help me? Is this Python code correct? Can I send handle directly like this?
Your code should run just fine as-is, IF there is truly a window titled "DMCap." To get a list of handles and titles, run the code below:
import win32gui
def window_enum_handler(hwnd, resultList):
if win32gui.IsWindowVisible(hwnd) and win32gui.GetWindowText(hwnd) != '':
resultList.append((hwnd, win32gui.GetWindowText(hwnd)))
def get_app_list(handles=[]):
mlst=[]
win32gui.EnumWindows(window_enum_handler, handles)
for handle in handles:
mlst.append(handle)
return mlst
appwindows = get_app_list()
for i in appwindows:
print i
This will produce a list of tuples containing handle, title pairs.