简体   繁体   English

有没有办法我可以使用 python 以某种方式拉出我在屏幕上打开的所有选项卡? 在 windows

[英]Is there a way I can somehow pull all tabs I have open on my screen using python? On windows

I am making a small project of mine and I got stuck here, so basically I need a way to get my program a list of all the tabs I have open.我正在制作我的一个小项目,但我被困在这里,所以基本上我需要一种方法来让我的程序列出我打开的所有选项卡。 So I need a way to tell my program all the tabs I have open (exmpl. firefox, chrome, spotify...) and then it needs to find all tabs starting with a specific word.所以我需要一种方法来告诉我的程序我打开的所有选项卡(例如 firefox、chrome、spotify...),然后它需要找到以特定单词开头的所有选项卡。

import win32gui

def winEnumHandler( hwnd, ctx ):
    if win32gui.IsWindowVisible( hwnd ):
        print (hex(hwnd), win32gui.GetWindowText( hwnd ))

win32gui.EnumWindows( winEnumHandler, None )

I tried this yes it works but I need it to find all the windows starting with the same word but then have other words.我试过了,是的,它可以工作,但我需要它来找到所有以相同单词开头但有其他单词的 windows。 So for example EVE is a game when you join in on a account that windows is called EVE - (then the name of the account) but when you have multiple accounts they r not always gonna be the same name so I need it to find all the tabs starting with EVE.因此,例如,EVE 是一个游戏,当您加入 windows 称为 EVE 的帐户时 - (然后是帐户的名称)但是当您有多个帐户时,它们 r 并不总是同名,所以我需要它来查找所有以 EVE 开头的选项卡。

This is the output of this code.这是这个代码的output。

0x100ec 
0x1014e 
0x2033a Python - Get Started - Untitled (Workspace) - Visual Studio Code
0x40370 C:\Users\Leon\Desktop\Evetest2screenshot.py - Notepad++
0x102b0 Is there a way I can somehow pull all tabs I have open on my screen using python? On windows - Stack Overflow 
- Google Chrome
0x1041a Signal
0x22082a New Tab - Google Chrome
0x3038c Command Prompt
0x30426
0x1084c Setup
0x101e4
0x101d8
0x101c6
0x101a4
0x101a2
0x70060 Microsoft Text Input Application
0x203aa Spotify Premium
0x20178 Program Manager

You can use psutil for this.您可以为此使用psutil I have made the following which gives you a dictionary with a path if you desire to check it and a set/list of program names.我做了以下内容,如果您想检查它和程序名称的集合/列表,它会为您提供带有路径的字典。

import psutil

main_programs = {}
program_list = set()

for proc in psutil.process_iter():
    try:
        if proc.cmdline() != []:
            main_programs[proc.name()] = proc.cmdline()[0]  # Get the name and path to a dictionary
            program_list.add(proc.name()) # Get the name to a set
    except psutil.AccessDenied:
        print("No perms")


search = "p"
for item in program_list:
    if item.startswith(search):
        print(item)

Output: Output:

powershell.exe
pythonw.exe
pycharm64.exe

EDIT编辑

list_programs = []
def winEnumHandler( hwnd, ctx ):
    win32gui = {}
    if win32gui.IsWindowVisible( hwnd ):
        list_programs.append(win32gui.GetWindowText( hwnd ))
        print (hex(hwnd), win32gui.GetWindowText( hwnd ))
search = "P"

for item in list_programs:
    if item.startswith(search):
        print(item)

Output Output

Python - Get Started - Untitled (Workspace) - Visual Studio Code
Program Manager

Yes it works, but it also gives me a list of all the processes.是的,它有效,但它也给了我所有进程的列表。 But is there a way to implement the search function into this?但是有没有办法实现搜索 function 到这个?

    import win32gui

    def winEnumHandler( hwnd, ctx ):
    if win32gui.IsWindowVisible( hwnd ):
    print (hex(hwnd), win32gui.GetWindowText( hwnd ))

    win32gui.EnumWindows( winEnumHandler, None )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python-我可以以某种方式将字符串插入到Open函数中吗? - Python - Can I Instert A String Into The Open Function, Somehow…? 我能否以某种方式合并这些类,这样我就不必一次又一次地编写它,并且我的 python 代码不会变得多余 - Can I somehow merge these classes such that I don't have to write this again and again and my python code doesn't get redundant 有没有办法用 Python 删除所有 Windows 的还原点? - Is there a way I can remove all Windows's restore points with Python? 如何从 python 打开多个 Chrome 标签页? - How can I open multiple Chrome tabs from python? 我无法使用 Pandas 在 python 上打开我的 Excel 文件 - I can't open my Excel file on python, using pandas 如何使用 Python 在 Web 浏览器中打开网站? - How can I open a website in my web browser using Python? 如何使用 Python 3 在我的 web 浏览器中打开网站? - How can I open a website in my web browser using Python 3? 我可以使用Python打开Windows任务计划程序吗? - Can I open windows task scheduler with Python? 使用 Python 获取所有打开的标签页的 URL - Get URLs of all open tabs using Python 对于 Python 和 C++ 中的编码问题,我有两个相同的答案,但不知何故 Python 通过了所有测试用例,而 C++ 解决方案没有 - I have two identical answers for a coding question in Python and C++ but somehow Python passes all the testcases and C++ solution doesn't
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM