首先,我的代碼:
when = tk.Tk()
when.title("when (sec)")
when.geometry('250x120')
def WhenInput():
print(when.get(1.0, "end"))
when = tk.Text(when, height = 10, width = 10)
when.pack()
when.mainloop()
StartIn = WhenInput
ClickDelay = 0.05
ClickRepeat = 20
mouse = Controller()
time.sleep(StartIn)
def repeat():
mouse.press(Button.left)
time.sleep(ClickDelay)
mouse.release(Button.left)
for i in range(ClickRepeat):
repeat()
我想要它做的是打開一個文本框,我插入一個數字,例如 5,我關閉視窗并將 5 放入“StartIn”變數中。
我希望我的代碼在準備好后執行的操作:它依次打開 3 個文本框,我首先在其中輸入時間,然后是速度,然后是多少。這是一個自動點擊器。
如果您愿意,您可以提出更好的解決方案。
uj5u.com熱心網友回復:
import tkinter as tk
import time
from pynput.mouse import Controller, Button
StartIn = 0
when = tk.Tk()
when.title("when (sec)")
when.geometry('250x120')
def WhenInput():
return int(when_text.get(1.0, "end-1c"))
when_text = tk.Text(when, height = 10, width = 10)
when_text.pack()
def on_closing():
global StartIn
StartIn = WhenInput()
when.destroy()
when.protocol("WM_DELETE_WINDOW", on_closing)
when.mainloop()
ClickDelay = 0.05
ClickRepeat = 20
mouse = Controller()
time.sleep(StartIn)
def repeat():
mouse.press(Button.left)
time.sleep(ClickDelay)
mouse.release(Button.left)
for i in range(ClickRepeat):
repeat()
解釋
首先你的代碼有什么問題。
- 您的主根名稱 is
when = tk.Tk()和文本輸入名稱相同,即when. - 您嘗試在根被銷毀后獲取輸入文本值。這是給你這個錯誤
_tkinter.TclError: invalid command name ".!text"。
我改變了什么。
- 首先,我更改了相同的變數名稱。
- 添加一個事件,
protocol("WM_DELETE_WINDOW", on_closing)當有人試圖退出或銷毀視窗時執行該事件。您注意到我on_closing作為引數傳遞。這是因為當有人試圖退出或破壞視窗時,會protocol("WM_DELETE_WINDOW", on_closing)執行該on_closing函式。 - 在函式內部,我寫了第一行,
global StartIn因為我想訪問外部范圍變數并想改變它(你注意到第四行代碼是StartIn = 0)。 on_closing函式內的第二行代碼是StartIn = WhenInput(). 這里我呼叫了以 .WhenInput形式回傳文本值的函式integer(int)。- 而且,第三行代碼就是
when.destroy銷毀視窗。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/469152.html
上一篇:Python、tkinter如何讓Linux的視窗點擊
下一篇:獲取組合框值python
