简体   繁体   English

在 Python 中将最小化的窗口置于最前面

[英]Bring a minimized Window to the front in Python

I am currently writing on a program that is opening PuTTy Sessions on the click of a button.我目前正在编写一个通过单击按钮打开 PuTTy Sessions 的程序。 Now I want the program to recognise wether a Session is already open and to open the minimized window.现在我想让程序识别 Session 是否已经打开并打开最小化的窗口。 Unfortunately, I can't install new libraries via pip, as it doesn't work on the companies Laptop (win32gui seems to not be installed).不幸的是,我无法通过 pip 安装新库,因为它在公司笔记本电脑上不起作用(似乎未安装 win32gui)。

This is my current code: (I know it's not beautiful, I just started programming)这是我目前的代码:(我知道它不漂亮,我刚开始编程)

import tkinter
import os

def rt010ac(): 
    os.system("start C:\PROGRA~1\PuTTY\putty.exe -load rt010ac")
def rt020ac(): 
    os.system("start C:\PROGRA~1\PuTTY\putty.exe -load rt020ac")
def rt020bd(): 
    os.system("start C:\PROGRA~1\PuTTY\putty.exe -load rt020bd")
def core1(): 
    os.system("start C:\PROGRA~1\PuTTY\putty.exe -load core1")
def core2(): 
    os.system("start C:\PROGRA~1\PuTTY\putty.exe -load core2")
def rt030ac(): 
    os.system("start C:\PROGRA~1\PuTTY\putty.exe -load rt030ac")
def rt030bd(): 
    os.system("start C:\PROGRA~1\PuTTY\putty.exe -load rt030bd")

window = tkinter.Tk()
window.geometry("1920x1080")

bg=tkinter.PhotoImage(file="Background.png")
label=tkinter.Label(image=bg)
label.place(x=0,y=0)
photo=tkinter.PhotoImage(file="Router.png")

milchButton = tkinter.Button(
    text="rt020ac",
    font=("Arial",14),
    image=photo,
    command=rt020ac,
    highlightthickness=0,
    borderwidth=0,
    compound=tkinter.TOP
    )
milchButton2 = tkinter.Button(
    text="rt020bd",
    font=("Arial",14),
    image=photo,
    command=rt020bd,
    highlightthickness=0,
    borderwidth=0,
    compound=tkinter.TOP
    )
milchButton3 = tkinter.Button(
    text="rt010ac",
    font=("Arial",14),
    image=photo,
    command=rt010ac,
    highlightthickness=0,
    borderwidth=0,
    compound=tkinter.TOP
    )
milchButton4 = tkinter.Button(
    text="core1",
    font=("Arial",14),
    image=photo,
    command=core1,
    highlightthickness=0,
    borderwidth=0,
    compound=tkinter.TOP
    )
milchButton5 = tkinter.Button(
    text="core2",
    font=("Arial",14),
    image=photo,
    command=core2,
    highlightthickness=0,
    borderwidth=0,
    compound=tkinter.TOP
    )
milchButton6 = tkinter.Button(
    text="rt030bd",
    font=("Arial",14),
    image=photo,
    command=rt030bd,
    highlightthickness=0,
    borderwidth=0,
    compound=tkinter.TOP
    )
milchButton7 = tkinter.Button(
    text="rt030ac",
    font=("Arial",14),
    image=photo,
    command=rt030ac,
    highlightthickness=0,
    borderwidth=0,
    compound=tkinter.TOP
    )
    
milchButton.place(relx=0.01,rely=0.37,anchor=tkinter.NW)
milchButton2.place(relx=0.01,rely=0.63,anchor=tkinter.NW)
milchButton3.place(relx=0.3,rely=0.1,anchor=tkinter.NW)
milchButton4.place(relx=0.3,rely=0.47,anchor=tkinter.NW)
milchButton5.place(relx=0.5,rely=0.47,anchor=tkinter.NW)
milchButton6.place(relx=0.8,rely=0.63,anchor=tkinter.NW)
milchButton7.place(relx=0.8,rely=0.37,anchor=tkinter.NW)

window.mainloop()

Update: I was able to install PyGetWindow as.whl and complete the program using it.更新:我能够安装 PyGetWindow as.whl 并使用它完成程序。

import pygetwindow
def checkWindow(router):
    WindowList = pygetwindow.getAllTitles()
    for Window in WindowList:
        if str(Window) == '{} - PuTTY'.format(router):
            putty = pygetwindow.getWindowsWithTitle('{} - PuTTY'.format(router))[0]
            putty.activate()
            putty.restore()
            return 0
    os.system("start C:\PROGRA~1\PuTTY\putty.exe -load {}".format(router))

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM