我正在制作一个自动答题器,它会问两个问题,延迟和每秒的点击次数。但是,当我尝试输入条目的代码时,它会说每秒不能点击多少,因为它说
Traceback (most recent call last):
clicks = int(entry)
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Entry'
这是代码:
import pynput
import time
import tkinter as tk
root = tk.Tk()
Height = 500
width = 600
def test_function(delay):
from pynput.mouse import Button,Controller
mouse = Controller()
time.sleep(float(delay))
mouse.click(Button.left, (int(clicks)))
canvas = tk.Canvas(root, height=Height, width=width)
canvas.pack()
entry = tk.Entry(root)
entry.place(relx=0.2, rely=0.21)
entry2 = tk.Entry(root)
entry2.place(relx=0.4, rely=0.4)
label = tk.Label(root, text="Tech AutoClicker")
label.place(relx=0.4, rely=0)
label2 = tk.Label(root, text="Clicks per sec:")
label2.place(relx=0.02, rely=0.2,)
label3 = tk.Label(root, text="Delay until starting (in seconds):")
label3.place(relx=0.02, rely=0.4)
button = tk.Button(root, text="Start Auto clicker", bg="#84f47c", fg="green", command=lambda: test_function(entry2 and entry.get()))
button.pack()
clicks = int(entry)
root.mainloop()
答案 0 :(得分:0)
您的entry
变量包含小部件类Entry
的对象。您需要使用小部件的get()
方法获取小部件的文本值,然后将其传递给int()
函数:
clicks = int(entry.get())
在您的示例中,在您尝试获取Entry
的文本的行中,它没有任何值(空字符串),因此无法将其转换为int。
这是一个如何正确使用Entry
小部件的示例:
import tkinter
def fun():
clicks = int(entry.get())
print(clicks)
root = tkinter.Tk()
button = tkinter.Button(root, text='click me', command=fun)
button.pack()
entry = tkinter.Entry(root)
entry.pack()
root.mainloop()
答案 1 :(得分:0)
从tk
的import语句被取出,并在python样式对流here订阅时放在脚本的开头。删除鼠标在botton上时产生的点击和lambda函数,即可自动运行脚本输出。
import tkinter as tk
import pynput, time
from pynput.mouse import Button,Controller
def test_function():
mouse = Controller()
# time.sleep(float(delay)) # if delay is set to e.g. 2.5. You get results after you close the window.
mouse.click(Button.left, 1) # on MacOS 1= 2.
print ('User-input 1: "%s" , Ui2: "%s".' % (e1.get(), e2.get()))
# Main GUI
root = tk.Tk()
Height = 500
width = 600
canvas = tk.Canvas(root, height=Height, width=width)
canvas.pack()
label = tk.Label(root, text="Tech AutoClicker")
label.place(relx=0.4, rely=0)
# GUI entity 1 - user input
label2 = tk.Label(root, text="Clicks per sec :")
label2.place(relx=0.02, rely=0.2)
e1 = tk.Entry(root)
e1.place(relx=0.2, rely=0.21)
#print ('2 : %s' % e1)
# GUI entity 2 - user input
label3 = tk.Label(root, text="Delay until starting (in seconds) :")
label3.place(relx=0.02, rely=0.4)
e2 = tk.Entry(root)
e2.place(relx=0.4, rely=0.4)
# GUI entity 3 - button
button = tk.Button(root, text="Start Auto Click", bg="#84f47c", fg="green", command=test_function)
button.pack()
root.mainloop()
如果clicks = str(entry)
,则elgaspar的答案将起作用:
import tkinter
def fun():
clicks = str(entry)
print(clicks)
root = tkinter.Tk()
button = tkinter.Button(root, text='click me', command=fun)
button.pack()
entry = tkinter.Entry(root)
entry.pack()
root.mainloop()
答案 2 :(得分:0)
子类化Entry小部件并使用'get'方法:
class IntEntry(tkinter.Entry):
def get(self):
val = super().get()
return int(val)
entry = IntEntry(root)
entry.pack()
# type number in
my_number = entry.get()
请不要忘记处理用户未输入整数的情况。