简体   繁体   English

TypeError: int() 参数必须是字符串、类似字节的 object 或数字,而不是“TextInput”

[英]TypeError: int() argument must be a string, a bytes-like object or a number, not 'TextInput'

I am developing a Kivy password generator.我正在开发一个 Kivy 密码生成器。 The user is required to input a number (digit) into the app for password generate depending on how many characters are intended.用户需要在应用程序中输入一个数字(数字)以生成密码,具体取决于预期的字符数。 Then, the app will generate a random character from the randint, finally, the output will be displayed into the output entry for a copy to the click board.然后,应用程序会从 randint 中生成一个随机字符,最后,output 将显示到 output 条目中,以便复制到点击板。 Following are my code:以下是我的代码:

password_generator.py密码生成器.py

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.core.window import Window

from random  import randint


Builder.load_file('password_generator.kv')

Window.size = (350, 600)

class MainApp(App):
    title='Password Generator'
    def build(self):
        Window.clearcolor = (255/255, 255/255, 0, 1) 
        return Mylayout()  

    def password(self):
  
        self.root.ids.output_entry = ''

        pw_length = int(self.root.ids.input_entry)
       
        my_password = ''

        for x in range(pw_length):
            my_password += chr(randint(33, 126))

        self.root.ids.output_entry = my_password

  
    def clip_board(self):
        self.root.clipboard_clear()

        self.root.clipboard_append(self.root.ids.output_entry)

    def delete(self):
        self.root.ids.input_entry = ''
        self.root.ids.output_entry = ''
    
class Mylayout(Widget):    
    pass
       

if __name__ == '__main__':
    MainApp().run()

password_generator.kv密码生成器.kv

#:import Factory kivy.factory.Factory

<MyLayout>
              
    FloatLayout:
        
        Label:
            id: label_frame
            text: 'How Many Characters?'
            pos_hint: {'x': .8, 'y':5}
            size_hint: (2, .4)
            color: (0, 0, 0, 1)
            font_size: 24

       
        TextInput:
            id: input_entry
            text: ''
            multiline: False
            font_family: 'Helvatica' 
            font_size: 24
            pos_hint: {'x': .8, 'y':4.5}
            size_hint: (2, .4)
            halign: "center"
            focus: True
            color: 'black'

        Button:
            text:'Generate A Strong Password'
            on_press: app.password()
            pos_hint: {'x': .8, 'y':3.8}
            size_hint: (2, .4)
            font_size: 15

        TextInput:
            id: output_entry
            text: ''
            multiline: False
            font_family: 'Helvatica' 
            font_size: 24
            pos_hint: {'x': .8, 'y':3}
            size_hint: (2, .4)
            halign: "center"
            focus: True
            color: 'black'
            background_color : (255/255, 255/255, 0, 1) 
            #foreground_color:  self.background_color
            color : 'black'

        Button:
            text : 'Copy To Clickboard'
            on_release: Factory.MyPopup().open(),
            on_press: app.clip_board()
            pos_hint: {'x': .8, 'y':2.5}
            size_hint: (2, .4)
            font_size: 15
       
        Button:
            text : "Reset"
            on_press : app.delete()
            pos_hint: {'x': .8, 'y':1.5}
            size_hint: (2, .4)
            font_size: 15

<MyPopup@Popup>
    # auto_dismiss: False
    auto_dismiss: True
    size_hint: 0.6, 0.2
    pos_hint: {'x':0.2, 'top':0.6} 

    title: "Message Informed"

    BoxLayout 
        orientation: "vertical"
        size: root.width, root.height
        Label:
            text: "Password Copied!"
            font_size: 15
        Button: 
            text: "Close!"
            font_size: 15
            on_release: root.dismiss()

When I click Generate Strong Password button and was required to output a random password character in the output entry, however, the following error was incurred:当我点击 Generate Strong Password 按钮并要求 output 在 output 条目中输入一个随机密码字符时,出现了以下错误:

Traceback (most recent call last):
   File "c:\Users\Kelvin Loh\Documents\kivyMD\password_generator.py", line 53, in <module>
     MainApp().run()
   File "C:\Users\Kelvin Loh\Documents\kivyMD\kivy_venv\lib\site-packages\kivy\app.py", line 950, in run
     runTouchApp()
   File "C:\Users\Kelvin Loh\Documents\kivyMD\kivy_venv\lib\site-packages\kivy\base.py", line 582, in runTouchApp
     EventLoop.mainloop()
   File "C:\Users\Kelvin Loh\Documents\kivyMD\kivy_venv\lib\site-packages\kivy\base.py", line 347, in mainloop
     self.idle()
   File "C:\Users\Kelvin Loh\Documents\kivyMD\kivy_venv\lib\site-packages\kivy\base.py", line 391, in idle
     self.dispatch_input()
   File "C:\Users\Kelvin Loh\Documents\kivyMD\kivy_venv\lib\site-packages\kivy\base.py", line 342, in dispatch_input
     post_dispatch_input(*pop(0))
   File "C:\Users\Kelvin Loh\Documents\kivyMD\kivy_venv\lib\site-packages\kivy\base.py", line 248, in post_dispatch_input
     listener.dispatch('on_motion', etype, me)
   File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\Kelvin Loh\Documents\kivyMD\kivy_venv\lib\site-packages\kivy\core\window\__init__.py", line 1412, in on_motion        
     self.dispatch('on_touch_down', me)
   File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\Kelvin Loh\Documents\kivyMD\kivy_venv\lib\site-packages\kivy\core\window\__init__.py", line 1428, in on_touch_down    
     if w.dispatch('on_touch_down', touch):
   File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\Kelvin Loh\Documents\kivyMD\kivy_venv\lib\site-packages\kivy\uix\widget.py", line 545, in on_touch_down
     if child.dispatch('on_touch_down', touch):
   File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\Kelvin Loh\Documents\kivyMD\kivy_venv\lib\site-packages\kivy\uix\widget.py", line 545, in on_touch_down
     if child.dispatch('on_touch_down', touch):
   File "kivy\_event.pyx", line 709, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\Kelvin Loh\Documents\kivyMD\kivy_venv\lib\site-packages\kivy\uix\behaviors\button.py", line 151, in on_touch_down     
     self.dispatch('on_press')
   File "kivy\_event.pyx", line 705, in kivy._event.EventDispatcher.dispatch
   File "kivy\_event.pyx", line 1248, in kivy._event.EventObservers.dispatch
   File "kivy\_event.pyx", line 1132, in kivy._event.EventObservers._dispatch
   File "C:\Users\Kelvin Loh\Documents\kivyMD\kivy_venv\lib\site-packages\kivy\lang\builder.py", line 57, in custom_callback
     exec(__kvlang__.co_value, idmap)
   File "C:\Users\Kelvin Loh\Documents\kivyMD\password_generator.kv", line 30, in <module>
     on_press: app.password()
   File "c:\Users\Kelvin Loh\Documents\kivyMD\password_generator.py", line 24, in password
     pw_length = int(self.root.ids.input_entry)
   File "kivy\weakproxy.pyx", line 254, in kivy.weakproxy.WeakProxy.__int__
 TypeError: int() argument must be a string, a bytes-like object or a number, not 'TextInput'

How to resolve?如何解决?

Try changing pw_length in your password() function to:尝试将password() function 中的 pw_length 更改为:

pw_length = int(self.root.ids.input_entry.text or 0)

input_entry is a TextInput which can't be converted to an int. input_entry 是无法转换为 int 的 TextInput。 I think you want to convert its value ( input_entry.text ) to an int, not the widget itself.我认为您想将其值 ( input_entry.text ) 转换为 int,而不是小部件本身。 To do that, change the pw_length =... line in password() method as above.为此,如上更改password()方法中的pw_length =...行。

When you use something like self.root.ids.some_name (or, self.ids.some_name ) you are supposed to access aweak reference of the named ( some_name ) widget from root (or, some class).当您使用类似self.root.ids.some_name (或self.ids.some_name )的东西时,您应该从root (或某个类)访问命名( some_name )小部件的弱引用

Now to get an attr.现在得到一个attr。 of that proxy widget you can go as usual using '.'该代理小部件的您可以像往常一样使用“。” go。 syntax ie like self.root.ids.some_name.prop (or, self.ids.some_name.prop ).语法即self.root.ids.some_name.prop (或self.ids.some_name.prop )。

Here, self.root.ids.output_entry is a TextInput instance.这里, self.root.ids.output_entry是一个TextInput实例。 So to access any of its properties you can do something like, self.root.ids.output_entry.prop_name .因此,要访问它的任何属性,您可以执行类似self.root.ids.output_entry.prop_name的操作。 With this I modified password method as,有了这个,我修改了password方法,

    def password(self):
  
        self.root.ids.output_entry.text = ''
        
        input_text = self.root.ids.input_entry.text
        
        # Bellow you will handle user-input. You can use try-except or if-else or whatever that suits. I just implement a simple one.
        if input_text: # Means non-empty string.
            pw_length = int(input_text)
        else: # Raise Error or warn user via a message.
            pw_length = 0
            
       
        my_password = ''

        for x in range(pw_length):
            my_password += chr(randint(33, 126))

        self.root.ids.output_entry.text = my_password

Don't forget to apply the required changes (as described above) wherever necessary.不要忘记在必要时应用所需的更改(如上所述)。

first the text field has to be limited to support only integers by adding the input_filter: "int"首先,必须通过添加input_filter: "int"将文本字段限制为仅支持整数

TextInput:
    id: input_entry
    text: ''
    multiline: False
    font_family: 'Helvatica' 
    font_size: 24
    pos_hint: {'x': .8, 'y':4.5}
    size_hint: (2, .4)
    halign: "center"
    focus: True
    color: 'black'
    input_filter: "int"

暂无
暂无

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

相关问题 TypeError: int() 参数必须是一个字符串,一个类似字节的 object 或一个数字,而不是“NoneType”。 但我写了 int() - TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'. But I wrote the int() int()参数必须是字符串,类似字节的对象或数字,而不是“列表” - int() argument must be a string, a bytes-like object or a number, not 'list' int() 参数必须是字符串、类似字节的对象或数字,而不是“NoneType” - int() argument must be a string, a bytes-like object or a number, not 'NoneType' int()参数必须是字符串,类似字节的对象或数字,而不是“元组” - int() argument must be a string, a bytes-like object or a number, not 'tuple' int() 参数必须是字符串、类似字节的对象或数字,而不是 &#39;QueryDict&#39; - int() argument must be a string, a bytes-like object or a number, not 'QueryDict' int() 参数必须是字符串、类似字节的对象或数字,而不是“QuerySet” - int() argument must be a string, a bytes-like object or a number, not 'QuerySet' int() 参数必须是字符串、类似字节的 object 或数字,而不是 'dict' - int() argument must be a string, a bytes-like object or a number, not 'dict' Python 2-TypeError:int()参数必须是字符串,类似字节的对象或数字,而不是“列表” - Python 2 - TypeError: int() argument must be a string, a bytes-like object or a number, not 'list' Python:TypeError:int() 参数必须是字符串、类似字节的对象或数字,而不是“NoneType” - Python: TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType' TypeError at /students/exam/1/ int() 参数必须是字符串、类似字节的对象或数字,而不是“列表” - TypeError at /students/exam/1/ int() argument must be a string, a bytes-like object or a number, not 'list'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM