win32gui似乎无法在同一进程上工作

时间:2019-01-31 22:20:01

标签: python-3.x win32gui

我有这段代码,它的工作是将名为“ Ingresa tu nombre”的窗口置于前台。它按预期工作。但是我需要它在我制作的tk代码中工作,这样我才能获得键盘焦点。

import win32gui
import win32com.client


def windowEnumerationHandler(hwnd, top_window):
    top_window.append((hwnd, win32gui.GetWindowText(hwnd)))


top_windows = []
win32gui.EnumWindows(windowEnumerationHandler, top_windows)


def bringToTop(window_name):
    flag = False
    for i in top_windows:
        if i[1] == window_name:
            shell = win32com.client.Dispatch("WScript.Shell")
            shell.AppActivate(window_name)
            shell.SendKeys("\%i", 0)
            win32gui.BringWindowToTop(i[0])
            win32gui.SetActiveWindow(i[0])
            win32gui.SetForegroundWindow(i[0])
            flag = True
    if not flag:
        print("No se encontro la ventana")


def searchForProcess(window_name):
    for i in top_windows:
        if i[1] == window_name:
            print("Se encontro: ", window_name, "!")



if __name__ == '__main__':
    bringToTop("Ingresa tu nombre")

问题:它似乎无法正常工作,我猜是它在“生成”窗口之前调用了GUI(上面的.py文件)中的函数。那么,我该怎么做呢?

PD:调用前面提到的窗口的一段代码:

class PopupWindow(object):

    def press(self, event):
        key = event.keysym
        if key == "Left":
            self.mover_izquierda()
        elif key == "Right":
            self.mover_derecha()
        elif key == "space":
            self.seleccionar()
        elif key == "f":
            self.cleanup()
        else:
            print(key, "no es una tecla valida")

    def deletrear(self, modo, char):
        letras = []
        item = int(-1)
        if modo == 0:
            for items in letters:
                item = item + 1
                letras.append(items)
        elif modo == 1:
            for items in letters:
                if char != item:
                    letras.insert(int(item), backup_letters[item])
                elif char == item:
                    letras.insert(int(item), ("[%s]" % (backup_letters[item])))
                item = item + 1
        return letras

    def mover_derecha(self):
        global cursor, letters
        if cursor == 42:
            cursor = 0
        cursor = cursor + 1
        letters = self.deletrear(1, cursor)
        letters.clear
        n_texto = str("")
        for items in letters:
            n_texto += items
        texto['text'] = n_texto

    def mover_izquierda(self):
        global cursor, letters
        cursor = cursor - 1
        if cursor == (0-1):
            cursor = 41
        letters.clear
        letters = self.deletrear(1, cursor)
        n_texto = str("")
        for items in letters:
            n_texto += items
        texto['text'] = n_texto

    def __init__(self, master):
        global texto, labelname
        letras = str("")
        for items in letters:
            letras += items

        master = self.top = tk.Toplevel(master)
        master.title("Ingresa tu nombre")
        master.geometry("500x300")
        master.focus_set()

        texto = tk.Label(master, text=letras)
        texto.pack()

        right = tk.Button(master, text="->", command=lambda: self.mover_derecha())
        right.pack()
        left = tk.Button(master, text="<-", command=lambda: self.mover_izquierda())
        left.pack()
        select = tk.Button(master, text="O", command=lambda: self.seleccionar())
        select.pack()
        labelname = tk.Label(master, text="")
        labelname.pack()
        okbutton = tk.Button(master, text="OK", command=lambda: self.cleanup())
        okbutton.pack()

        master.bind('<Key>', self.press)
        GUI.bringToTop("Ingresa tu nombre")

    def seleccionar(self):
        labelname['text'] = labelname['text'] + backup_letters[cursor]

    def cleanup(self):
        self.value = labelname['text']
        self.top.destroy()

0 个答案:

没有答案