Python - 将焦点移至简单对话框

Python - Move focus to simpledialogbox

所以我正在编写一个脚本,该脚本使用 pyautogui 启动网络浏览器,然后转到特定页面,它将打印多个 pdf。但是,在打印之前,它会弹出一个简单的对话框并要求输入一个整数,因此它知道要重复打印循环多少次。目前脚本可以运行,但我想在弹出简单对话框时将焦点从网页上移开,这样用户只需输入他们的号码并按回车键即可。

为了方便起见,我创建了一个启动记事本的简单脚本,并以相同的方式请求一个整数,然后重复一个简单的 while 循环。这说明了我的问题,但也大大减少了你们需要整理的代码量。

免责声明 - 由于我的 tkinter window 已经解除,我可以很容易地看到简单的对话框,问题是焦点滞后和只能输入数字的能力。

提前致谢,请不要关闭我的问题。

from tkinter import simpledialog
import tkinter as tk
import pyautogui
import time


master = tk.Tk()
master.geometry("300x100+1620+0")


master.lift()
master.attributes('-topmost',True)


def move_mouse():
    pyautogui.hotkey("win")
    time.sleep(2)
    pyautogui.typewrite("notepad")
    time.sleep(2)
    pyautogui.hotkey("enter")
    time.sleep(2)
    pyautogui.hotkey("win", "up")
    time.sleep(2)
    count = 0
    number = simpledialog.askinteger("Input", "How many rotations of the while loop? ",
                                   parent=master, minvalue=0, maxvalue=10)
    time.sleep(2)
    pyautogui.moveTo(900, 500)
    pyautogui.click()
    while count <= number-1:
        pyautogui.typewrite("notepad")
        time.sleep(1)
        pyautogui.hotkey("enter")
        time.sleep(1)
        count = count + 1


test_button = tk.Button(master, text="Test", command=move_mouse)
test_button.pack()

      
master.mainloop()

更新: 所以我得到了两条建议,一条来自这里,一条来自 Reddit。 堆栈:root.update_idletasks() Reddit:master.focus_force()

两者都在我的 simpledialogbox 之前调用,它们都适用于我的虚拟脚本,但实际上我需要调用 chrome 而不是完整脚本中的记事本。这是我的错误,当我调用 chrome 时,none 的建议奏效了。然后我有点乱七八糟,试着调用两次焦点,这奏效了……我不知道为什么!无论如何,这是我最终的工作代码,请随意评论它。

from tkinter import simpledialog
import tkinter as tk
import pyautogui
import time


master = tk.Tk()
master.geometry("300x100+1620+0")


master.lift()
master.attributes('-topmost',True)


sleep_time = 2


def launch_meebook():
    pyautogui.press("win")
    pyautogui.typewrite("chrome")
    time.sleep(sleep_time)
    pyautogui.press("enter")
    time.sleep(sleep_time)
    pyautogui.hotkey("win", "up")
    pyautogui.typewrite("https://app.meebook.com/arsplaner/")
    pyautogui.press("enter")
    time.sleep(sleep_time)
    pyautogui.moveTo(1240, 520)
    pyautogui.click()
    pyautogui.moveTo(1850, 50)
    master.focus_force()
    master.focus_force()

    
def rotation():
    count = 0
    y = 260
    antal = simpledialog.askinteger("Input", "Hvor mange årsplaner er der på siden? ",
                                   parent=master, minvalue=0, maxvalue=10)
    pyautogui.moveTo(1885, 785)
    pyautogui.click()
    while count <= antal-1:
        pyautogui.press('pageup')
        pyautogui.press('down')
        pyautogui.press('down')
        pyautogui.press('down')
        pyautogui.press('down')
        pyautogui.press('down')
        pyautogui.press('down')
        pyautogui.press('down')
        pyautogui.press('down')
        #Move cursor plan and click it
        pyautogui.moveTo(750, y)
        pyautogui.click()
        time.sleep(sleep_time)
        #Call printer and press print
        pyautogui.hotkey("ctrl", "p")
        time.sleep(sleep_time)
        pyautogui.moveTo(1470, 895)
        #pyautogui.click()
        pyautogui.press("esc")
        time.sleep(sleep_time)
        pyautogui.moveTo(960, 270)
        pyautogui.click()
        time.sleep(sleep_time)
        count = count + 1
        y = y + 65


def test():
    launch_meebook()
    rotation()
    time.sleep(sleep_time)
    pyautogui.hotkey("alt", "f4")


rotation_button = tk.Button(master, text="Print årsplaner", command=rotation)
rotation_button.pack()


launch_meebook_button = tk.Button(master, text="Gå til Meebook.com/arsplaner", command=launch_meebook)
launch_meebook_button.pack()


test_button = tk.Button(master, text="Test", command=test)
test_button.pack()

      
master.mainloop()

您需要先致电update_idletasks

from tkinter import *
from tkinter import simpledialog

root = Tk()
root.update_idletasks()
number = simpledialog.askinteger("Input", "How many rotations of the while loop? ",
                                 parent=root, minvalue=0, maxvalue=10)


root.mainloop()