简体   繁体   English

如何将Tkinter条目从字符串转换为整数

[英]How to turn Tkinter entry from string to integer

I am making an auto clicker which asks two questions, delay and clicks per second. 我正在制作一个自动点击器,它会提出两个问题,即每秒延迟和点击次数。 But when I try to make the input of the entry go to the code that says how much clicks per second it can't because it says 但是,当我尝试输入条目时,请转到说明每秒有多少点击次数的代码,因为它说

Traceback (most recent call last):  
    clicks = int(entry) 
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Entry'

Here is the code: 这是代码:

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()

Your entry variable holds an object of the widget class Entry . 您的entry变量包含窗口小部件类Entry的对象。 You need to get the text value of the widget, using its get() method and then pass it to int() function: 您需要使用其get()方法get()窗口小部件的文本值,然后将其传递给int()函数:

clicks = int(entry.get())

In your example, in the line that you try to get the text of the Entry , it will have no value (empty string) so it won't be able to be converted to int. 在您的示例中,在您尝试获取Entry文本的行中,它将没有值(空字符串),因此它将无法转换为int。

This is an example of how you could use the Entry widget properly: 这是一个如何正确使用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()

The import statement from tk is taken out and put at start of the script as the python styling convetion here subscribes. 来自tk的import语句被取出并放在脚本的开头,因为这里的python样式会话订阅。 Removing clicks and lambda functions made when mouse is on the botton autorun your scripts output. 删除鼠标在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()

The answer from elgaspar will work if clicks = str(entry) : 如果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()

Subclass the Entry widget and use the 'get' method: 对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()

Don't forget to handle cases where the user did not put in an integer. 不要忘记处理用户没有输入整数的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM