我正在嘗試學習如何執行緒使用tkinter和遇到問題。我的主要計劃是最終打開一個程式并使用熱鍵瀏覽所述程式的選單,以自動執行我正在執行的遞回程序。我知道這不是最有效的方法,但有理由說明為什么這是唯一可用的方法。
我現在正在嘗試簡單地撰寫一個記事本版本的代碼,該代碼將復制文本框用戶輸入字串并將它們輸入到記事本中。我聽說我需要使用執行緒來完成我的最終專案,但顯然我沒有做正確的事情,因為一旦記事本打開程式就不會繼續。下面是代碼:
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()
uj5u.com熱心網友回復:
subprocess.call
是一個阻塞功能,因為它等待它完成,在這種情況下意味著它會等到您關閉記事本,然后再繼續自動化。
你可能想用
subprocess.Popen
因為它在新行程中執行子程式,這意味著它將打開記事本,然后繼續自動化。
對于您的情況,您實際上只需要更改call為Popen,這實際上是唯一需要的更改。
有用:
subprocess.Popen檔案
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/391059.html
下一篇:由WindowsFormsSynchronizationContext和System.Events.UserPreferenceChanged引起的UI凍結
