简体   繁体   English

使用 Tkinter(线程)打开 Notepad.exe

[英]Opening Notepad.exe using Tkinter (threading)

I'm trying to learn how to thread using tkinter and running into issues.我正在尝试学习如何使用tkinter进行线程化并遇到问题。 My main plan is to eventually open a program and utilize hotkeys to navigate through the menus of said program to automate a recursive process I'm doing.我的主要计划是最终打开一个程序并利用热键浏览所述程序的菜单,以自动化我正在执行的递归过程。 I understand this isn't the most efficient way to do this, but there are reasons why this is the only available approach.我知道这不是最有效的方法,但这是唯一可用的方法是有原因的。

I'm trying at the moment to simply write a notepad version of the code that will copy the textbox user entry strings and type them into notepad.我目前正在尝试简单地编写一个记事本版本的代码,它将复制文本框用户输入字符串并将它们输入到记事本中。 I've heard I will need to use threading to do my final project, but clearly I'm not doing something correctly as once notepad opens the program doesn't proceed.我听说我将需要使用线程来完成我的最终项目,但显然我没有正确地做某事,因为一旦记事本打开程序就不会继续。 Below is the code:下面是代码:

import subprocess
from tkinter import *
import time
import threading
from threading import Thread

"""Sleep function"""
def SleepSec(var):
     time.sleep(var)


"""Hotkey function runs hotkeys to take measurement"""
def Notepad_Test():
     mouse = pynput.mouse.Controller()
     key = pynput.keyboard.Controller()

     """Open notepad program"""
     subprocess.call(['C:\\Windows\\System32\\notepad.exe'],shell=True)
     SleepSec(5)
     """Click on program window"""
     mouse.position = (400,400)
     mouse.click(pynput.mouse.Button.left, 1)

     """Sorting variables from inputs"""
     intervalEntry = E1.get() #Interval time
     dataEntry = dropVar.get() #RAW or Radiometric data
     pathEntry = E2.get() #Path file
     filenameEntry = E3.get() #filename
     startNumEntry = E4.get() #Starting measurement number
     countEntry = E5.get() #Number of measurements to take
     commentEntry = E6.get() #Comments about measurement

     SleepSec(1)
     key.type(intervalEntry)
     SleepSec(1)
     key.press(pynput.keyboard.Key.enter.value)
     SleepSec(1)
     key.type(dataEntry)
     SleepSec(1)
     key.press(pynput.keyboard.Key.enter.value)
     SleepSec(1)
     key.type(pathEntry)
     SleepSec(1)
     key.press(pynput.keyboard.Key.enter.value)
     SleepSec(1)
     key.type(filenameEntry)
     SleepSec(1)
     key.press(pynput.keyboard.Key.enter.value)
     SleepSec(1)
     key.type(startNumEntry)
     SleepSec(1)
     key.press(pynput.keyboard.Key.enter.value)
     SleepSec(1)
     key.type(countEntry)
     SleepSec(1)
     key.press(pynput.keyboard.Key.enter.value)
     SleepSec(1)
     key.type(commentEntry)
     SleepSec(1)

"""Create a GUI"""
root = Tk()

root.title('Measurement')

dropVar = StringVar(root)

options = [
     "RAW",
     "Radiometric"
]

dropVar.set(options[0])

"""Labels and entries for desired measurement parameters"""
L1 = Label(root, text = "Measurement Interval").grid(row = 0)
E1 = Entry(root, bd = 5)
E1.insert(10,"1")
E1.grid(row=0,column=1)

L2 = Label(root, text = "Pathname").grid(row = 1)
E2 = Entry(root, bd = 5)
E2.insert(10,"C:/Users/ASD User/Documents/ASD_data/test")
E2.grid(row=1,column=1)

L3 = Label(root, text = "Filename").grid(row = 2)
E3 = Entry(root, bd = 5)
E3.insert(10,"DefaultFilename")
E3.grid(row=2,column=1)

L4 = Label(root, text = "Measurement #").grid(row = 3)
E4 = Entry(root, bd = 5)
E4.insert(10,"0")
E4.grid(row=3,column=1)

L5 = Label(root, text = "Number of files to save").grid(row = 4)
E5 = Entry(root, bd = 5)
E5.insert(10,"30")
E5.grid(row=4,column=1)

L6 = Label(root, text = "Comment").grid(row = 5)
E6 = Entry(root, bd = 5)
E6.insert(10,"Comment here")
E6.grid(row=5,column=1)

O1 = OptionMenu(root, dropVar, *options).grid(row = 6)

"""Buttons for running measurement and quitting"""
B1 = Button(root,
          text = 'Quit',
          command = root.quit).grid(row = 7,
                                   column = 0,
                                   sticky=W,
                                   pady=4)

B2 = Button(root,
          text = "Measure", command= lambda: threading.Thread(target = Notepad_Test).start()).grid(row=7,
                                                       column=1,
                                                       sticky=W,
                                                       pady=4)


root.mainloop()
subprocess.call

Is a blocking function because it waits for it to finish which in this case means that it waits until you close notepad before proceeding on with the automation.阻塞 function因为它等待它完成,在这种情况下意味着它会等到您关闭记事本后再继续进行自动化。

You probably want to use您可能想使用

subprocess.Popen

because it Executes a child program in a new process , meaning that it will open notepad and then continue on with the automation.因为它在新进程中执行子程序,这意味着它将打开记事本,然后继续自动化。

For your case you really just need to change call to Popen , that is literally the only change required.对于您的情况,您实际上只需要更改对Popencall ,这实际上是唯一需要的更改。

Useful:有用:

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

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