我正在尝试使用 pyside6 编写一段代码,它有 3 个按钮,当我按其中一个按钮时,它会写出符号
from PySide6.QtWidgets import QApplication, QPushButton,QLineEdit
from PySide6.QtCore import QFile, Qt
from PySide6.QtUiTools import QUiLoader
import keyboard,time
app = QApplication([])
# Assuming the UI file is in the "test" folder
ui_file = QFile("test/ui_file.ui")
ui_file.open(QFile.ReadOnly)
loader = QUiLoader()
window = loader.load(ui_file)
ui_file.close()
# Set the window flag to make it always on top
window.setWindowFlags(window.windowFlags() | Qt.WindowStaysOnTopHint)
pushButton_arrow = window.findChild(QPushButton, "pushButton_arrow")
pushButton_checkmark = window.findChild(QPushButton, "pushButton_checkmark")
pushButton_cross = window.findChild(QPushButton, "pushButton_cross")
def write_1(a):
keyboard.write(a)
pushButton_arrow.clicked.connect(lambda: write_1("⇒"))
pushButton_cross.clicked.connect(lambda: write_1("✖"))
pushButton_checkmark.clicked.connect(lambda: write_1("✔"))
window.show()
app.exec()
但是当我按下按钮时,窗口将专注于我的程序并且不会写任何东西(我希望它专注于我的写作软件,即那个东西的类型) 我也尝试过使用 alt+tab 它可以工作,但这不是我想要的
keyboard.press('alt+tab') # Switch to the previous active window
keyboard.release('alt+tab')
time.sleep(0.2)
找到了使用 pygetwindow 的方法
import pygetwindow as gw
window_name=""
def activate_window_1(user_window):
window = gw.getWindowsWithTitle(user_window)[0]
global window_name
window_name = window.title
window.maximize()
window.activate()