简体   繁体   English

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

[英]Python - Move focus to simpledialogbox

So I'm working on a script that uses pyautogui to launch a web browser, and then go to a specific page where it will print multiple pdf's.所以我正在开发一个脚本,它使用 pyautogui 启动 web 浏览器,然后 go 到一个特定页面,它将打印多个 pdf。 However, before it prints, it will pop a simpledialogbox and ask for an integer, so it knows how many times to repeat the print loop.但是,在打印之前,它会弹出一个简单的对话框并询问 integer,因此它知道重复打印循环多少次。 At the moment the script works, but I would like to make so that when the simpledialogbox pop, that it will take focus from the webpage, so the users can just type in their number and hit enter.目前脚本可以工作,但我想让当 simpledialogbox 弹出时,它会从网页中获取焦点,因此用户只需输入他们的号码并按 Enter。

To make things easy I have created a simple script that launches notepad, and asks for an integer in the same fashion and then repeats a simple while loop.为了方便起见,我创建了一个简单的脚本来启动记事本,并以相同的方式请求 integer,然后重复一个简单的 while 循环。 This illustrates my problem but, also greatly reduced the amount of code you guys need to sort through.这说明了我的问题,但也大大减少了你们需要整理的代码量。

Disclaimer - Since my tkinter window is already lifted I can easily see the simpledialogbox, and the problem is the lag of focus and ability to just type in a number.免责声明 - 由于我的 tkinter window 已经被解除,我可以很容易地看到简单的对话框,问题是焦点滞后和只输入数字的能力。

Thx in advance, and please don't just close my question.提前谢谢,请不要只是关闭我的问题。

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()

Update: So I got two suggestions one from here, and one from Reddit.更新:所以我得到了两个建议,一个来自这里,一个来自 Reddit。 Stack: root.update_idletasks() Reddit: master.focus_force()堆栈:root.update_idletasks() Reddit:master.focus_force()

Both to be called just before my simpledialogbox, and both of them work for my dummy script, however I actually need to call chrome and not notepad in the full script.两者都在我的简单对话框之前被调用,并且它们都适用于我的虚拟脚本,但是我实际上需要在完整脚本中调用 chrome 而不是记事本。 That's a mistake on my part, and none of the suggestions worked when I called chrome.这是我的一个错误,当我打电话给 chrome 时,这些建议都没有奏效。 Then I messed around a little, and tried to call for focus twice, which worked...I don't know why, Anyways this is my final, and working code.然后我搞砸了一点,并试图两次调用焦点,这有效......我不知道为什么,无论如何这是我的最终和工作代码。 well free to comment it.可以自由发表评论。

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()

You need to call update_idletasks before.您需要先调用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()

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

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