简体   繁体   中英

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. 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. 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.

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. 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.

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. Stack: 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. That's a mistake on my part, and none of the suggestions worked when I called 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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