如何将Tkinter Entry内容转为整数?自动点击器类型错误解决
Hey there, let's sort out the errors in your autoclicker code and get it working properly!
The Root Cause of Your TypeError
The error TypeError: int() argument must be a string, a bytes-like object or a number, not 'Entry' happens because you're trying to convert the Tkinter Entry component itself to an integer with clicks = int(entry), instead of grabbing the text the user typed into that entry box. To get input content from an Entry, you need to use the .get() method, like entry.get().
On top of that, you've got a couple other logic gaps:
- You're trying to use
clicksbefore the user has even had a chance to input anything (it runs when the program starts, not when the button is clicked) - The button's command uses
entry2 and entry.get()which doesn't pass the two values correctly to your function - Your
test_functiononly accepts adelayparameter but relies on a globalclicksvariable, which isn't good practice
Corrected Code
Here's the fixed version with explanations of the key changes:
import pynput import time import tkinter as tk from pynput.mouse import Button, Controller # Move import to top for cleaner code root = tk.Tk() HEIGHT = 500 WIDTH = 600 def test_function(): # Get values from entry boxes when the button is clicked (not on program start) try: delay = float(entry2.get()) clicks_per_sec = int(entry.get()) if clicks_per_sec <= 0: raise ValueError("Clicks per second must be a positive number") except ValueError as e: print(f"Invalid input: {e}") return # Exit function if input is bad mouse = Controller() print(f"Starting autoclicker after {delay} seconds, clicking {clicks_per_sec} times per second...") time.sleep(delay) # Loop to click at the specified rate (adjust this if you want a stop condition) try: while True: mouse.click(Button.left, 1) # Click once per loop iteration time.sleep(1 / clicks_per_sec) except KeyboardInterrupt: print("Autoclicker stopped.") # UI Setup 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) # Simplified button command - no messy lambda parameter passing button = tk.Button(root, text="Start Auto clicker", bg="#84f47c", fg="green", command=test_function) button.pack() root.mainloop()
Key Changes Made:
- Cleaned up imports: Moved the pynput mouse import to the top for better code organization
- Input handling overhaul: The function now grabs input values when the button is clicked (not on program launch), and includes error handling for invalid inputs like non-numeric values or negative click rates
- Fixed click logic: Instead of clicking multiple times in one go, the loop clicks once every
1/clicks_per_secseconds to match your "clicks per second" requirement - Simplified button command: No more confusing lambda tricks— the function handles input retrieval directly
- Added guardrails: Tells the user if they entered invalid values instead of crashing silently
Quick Improvement Tip
If you want to add a stop button later, you could use a global flag variable to break the loop instead of relying on KeyboardInterrupt. Adding placeholder text to the Entry boxes (like entry.insert(0, "5")) can also make the UI more user-friendly.
内容的提问来源于stack exchange,提问作者ibrahim

