TypeError:int() 参数必须是字符串、类似字节的对象或数字,而不是'TextInput'



我正在开发一个Kivy密码生成器。用户需要在应用程序中输入一个数字(数字(,以便根据需要的字符数生成密码。然后,应用程序将从randint生成一个随机字符,最后,输出将显示在输出条目中,以复制到点击板上。以下是我的代码:

password_generator.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

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

当我单击"生成强密码"按钮并被要求在输出条目中输出随机密码字符时,出现了以下错误:

Traceback (most recent call last):
File "c:UsersKelvin LohDocumentskivyMDpassword_generator.py", line 53, in <module>
MainApp().run()
File "C:UsersKelvin LohDocumentskivyMDkivy_venvlibsite-packageskivyapp.py", line 950, in run
runTouchApp()
File "C:UsersKelvin LohDocumentskivyMDkivy_venvlibsite-packageskivybase.py", line 582, in runTouchApp
EventLoop.mainloop()
File "C:UsersKelvin LohDocumentskivyMDkivy_venvlibsite-packageskivybase.py", line 347, in mainloop
self.idle()
File "C:UsersKelvin LohDocumentskivyMDkivy_venvlibsite-packageskivybase.py", line 391, in idle
self.dispatch_input()
File "C:UsersKelvin LohDocumentskivyMDkivy_venvlibsite-packageskivybase.py", line 342, in dispatch_input
post_dispatch_input(*pop(0))
File "C:UsersKelvin LohDocumentskivyMDkivy_venvlibsite-packageskivybase.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:UsersKelvin LohDocumentskivyMDkivy_venvlibsite-packageskivycorewindow__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:UsersKelvin LohDocumentskivyMDkivy_venvlibsite-packageskivycorewindow__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:UsersKelvin LohDocumentskivyMDkivy_venvlibsite-packageskivyuixwidget.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:UsersKelvin LohDocumentskivyMDkivy_venvlibsite-packageskivyuixwidget.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:UsersKelvin LohDocumentskivyMDkivy_venvlibsite-packageskivyuixbehaviorsbutton.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:UsersKelvin LohDocumentskivyMDkivy_venvlibsite-packageskivylangbuilder.py", line 57, in custom_callback
exec(__kvlang__.co_value, idmap)
File "C:UsersKelvin LohDocumentskivyMDpassword_generator.kv", line 30, in <module>
on_press: app.password()
File "c:UsersKelvin LohDocumentskivyMDpassword_generator.py", line 24, in password
pw_length = int(self.root.ids.input_entry)
File "kivyweakproxy.pyx", line 254, in kivy.weakproxy.WeakProxy.__int__
TypeError: int() argument must be a string, a bytes-like object or a number, not 'TextInput'

如何解决?

首先,必须通过添加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"

当您使用类似self.root.ids.some_name(或self.ids.some_name(的东西时,您应该从root(或某个类(访问命名(some_name(小部件的弱引用。

现在获取属性。您可以像往常一样使用"如self.root.ids.some_name.prop(或self.ids.some_name.prop(。

这里,self.root.ids.output_entryTextInput实例。因此,要访问它的任何属性,您可以执行类似self.root.ids.output_entry.prop_name的操作。在此基础上,我将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

不要忘记在必要时应用所需的更改(如上所述(。

尝试将password()函数中的pw_length更改为:

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

input_entry是一个无法转换为int的TextInput。我认为您希望将其值(input_entry.text(转换为int,而不是小部件本身。要执行此操作,请如上所述更改password()方法中的pw_length = ...行。

相关内容

  • 没有找到相关文章

最新更新