【发布时间】:2021-12-15 07:16:17
【问题描述】:
我使用 tkinter 包中提供的 Checkbox 类创建了一个具有 3 个复选框的图形用户界面。我想检索每个复选框的屏幕坐标。我一直在尝试使用 pyautogui.position() 来检索坐标。但是,这些值似乎是错误的,因为当我使用 pyautogui 引擎的这些坐标单击框时,它没有单击。此外,当我调试它时,似乎包含复选框的窗口不允许我选中这些框。还有什么其他可能的方法可以解决这些问题?
from tkinter import *
import SpeechToDictation as Std
import pyautogui
import time
def speech_to_dictation():
speech_inst = Std.SpeechToDictation()
dictation.append(speech_inst.read_audio())
def user_speech_to_dictation():
main_window = Tk()
button = Button(text='Recording')
button.pack()
# have a while true at here so that when the function executes, it quits out of the loop
button.config(command=speech_to_dictation)
main_window.mainloop()
class Test(Frame):
def __init__(self, parent=None, picks=[]):
Frame.__init__(self, parent)
self.vars = []
self.checkboxes = []
Label(self, text='Lab Works').grid(row=0, padx=10, pady=10)
i = 1
for pick in picks:
var = IntVar()
chk = Checkbutton(self, text=pick, variable=var)
chk.grid(row=i, pady=4, padx=10)
self.vars.append(var)
self.checkboxes.append(chk)
i += 1
def state(self):
return map((lambda var: var.get()), self.vars)
def full_screen(window):
width = window.winfo_screenwidth()
height = window.winfo_screenheight()
window.geometry("%dx%d" % (width, height))
def allstates():
print(list(lng.state()))
def make_test(window):
full_screen(window=root)
window.grid(row=1, column=0)
Button(root, text='Quit', command=root.quit).grid(row=10, padx=10, pady=10)
Button(root, text='Peek', command=allstates).grid(row=12, padx=10, pady=10)
if __name__ == '__main__':
# store dictation at dictation[]
dictation = []
user_speech_to_dictation()
is_string_complete = dictation[0]['text'][:8]
if is_string_complete == 'complete':
start_time = time.time() # returns number of seconds passed since epoch
max_loop_time = 1 # 1 seconds
while True:
if (time.time() - start_time) >= max_loop_time:
root = Tk()
lng = Test(root, ['Blood Count', 'Lipid Panel', 'Hemoglobin A1C'])
make_test(window=lng)
root.state('zoomed')
root.update()
x_position, y_position = pyautogui.position()
print(x_position, y_position)
max_loop_time = time.time() - start_time + max_loop_time
# Coordinate of each boxes: they seem wrong
locations = [(53, 158), (84, 228), (36, 302)]
blood_count_string = dictation[0]['text'][9:]
if blood_count_string == 'blood count':
x_coordinate = locations[0][0]
y_coordinate = locations[0][1]
pyautogui.click(x_coordinate, y_coordinate)
allstates()
root.destroy()
# quit the program after the window is destroyed
if max_loop_time > 2:
break
【问题讨论】:
-
您为什么使用
pyautogui与您的GUI 交互?tkinter小部件具有以编程方式与它们交互的方法 -
我知道我可以通过使用 toggle() 函数来更改复选框的值,但我想看看我是否真的可以通过单击它来选中复选框。
-
pyautogui.position()返回什么?如果它是复选按钮的一角,那么也许您可以尝试在 x 和 y 坐标上添加一点。检查按钮周围可能有一些无法点击的填充。 -
它返回我在位置数组中的内容。另外我认为由于我每秒只更新一次窗口,因此信息或窗口与我的点击不对应