几天前我为凯撒密码编程过Kivy接口,但遇到一个错误(请参阅标题),但似乎无法解决。我将把我的代码以及可能会有所帮助的完整追溯放在下面。
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
class Cypher:
def __init__(self):
MAX_KEY_SIZE = 26
'''def getMode(self)
while True:
print('Do you wish to encrypt or decrypt a message?')
mode = input().lower()
if mode in 'encrypt e decrypt d'.split():
return mode
else:
print('Enter either "encrypt" or "e" or "decrypt" or "d".')'''
def getMessage(self):
print('Enter your message:')
return input()
def getKey(self):
key = 0
while True:
print('Enter the key number (1-%s)' % (MAX_KEY_SIZE))
key = int(input())
if (key >= 1 and key <= MAX_KEY_SIZE):
return key
def getTranslatedMessage(self, mode, message, key):
'''if mode[0] == 'd':
key = -key'''
self.msg = ''
self.msg += message
translated = ''
for char in self.msg:
if char.isalpha():
num = ord(char)
num += key
if char.isupper():
if num > ord('Z'):
num -= 26
elif num < ord('A'):
num += 26
elif char.islower():
if num > ord('z'):
num -= 26
elif num < ord('a'):
num += 26
translated += chr(num)
else:
translated += char
return translated
def func(self):
mode = self.getMode()
message = self.getMessage()
key = self.getKey()
# print('Your translated text is:')
return self.getTranslatedMessage(mode, message, key)
class StartPageKivy(GridLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
c = Cypher()
self.cols = 2
self.encButton = Button(text='Encrypt')
self.encButton.bind(on_press = self.encryptionMode)
self.add_widget(self.encButton)
self.decButton = Button(text='Decrypt')
self.decButton.bind(on_press = self.decryptionMode)
self.add_widget(self.decButton)
self.add_widget(Label(text='Insert the key'))
self.key = TextInput(multiline=False)
self.add_widget(self.key)
self.add_widget(Label(text='Insert the message'))
self.message = TextInput(multiline=False).text
self.add_widget(self.message)
self.add_widget(Label(text='The result is :'))
self.mode = ''
while self.mode not in 'e d':
if self.encryptionMode() == True:
self.mode = 'e'
elif self.decryptionMode() == True:
self.mode = 'd'
self.result = c.getTranslatedMessage(self.mode, self.message, self.key)
self.add_widget(Label(text=self.result))
def encryptionMode(self, instance):
return True
def decryptionMode(self, instance):
return True
class EpicApp(App):
def build(self):
return StartPageKivy()
class StartPageTkinter():
pass
if __name__ == '__main__':
EpicApp().run()
runfile('C:/Users/danburnier/Desktop/PythonPrograms/AveCypher.py',> wdir ='C:/ Users / danburnier / Desktop / PythonPrograms') 追溯(最近一次通话):
文件“”,第1行,在 runfile('C:/Users/danburnier/Desktop/PythonPrograms/AveCypher.py',> wdir ='C:/ Users / danburnier / Desktop / PythonPrograms')
文件“ C:\ Users \ danburnier \ AppData \ Local \ Continuum \ anaconda3 \ lib \ site-> packages \ spyder_kernels \ customize \ spydercustomize.py”,行827,在运行文件中 execfile(文件名,命名空间)
exec文件中第110行的文件“ C:\ Users \ danburnier \ AppData \ Local \ Continuum \ anaconda3 \ lib \ site-> packages \ spyder_kernels \ customize \ spydercustomize.py” exec(compile(f.read(),文件名,'exec'),命名空间)
文件“ C:/Users/danburnier/Desktop/PythonPrograms/AveCypher.py”,第152行,在 EpicApp()。run()
文件“ C:\ Users \ danburnier \ AppData \ Local \ Continuum \ anaconda3 \ lib \ site-packages \ kivy \ app.py”,行829,正在运行 根= self.build()
在构建中,文件“ C:/Users/danburnier/Desktop/PythonPrograms/AveCypher.py”,第145行 返回StartPageKivy()
init 中的文件“ C:/Users/danburnier/Desktop/PythonPrograms/AveCypher.py”,第117行 self.add_widget(self.message)
add_widget中的第92行,文件“ C:\ Users \ danburnier \ AppData \ Local \ Continuum \ anaconda3 \ lib \ site-packages \ kivy \ uix \ layout.py” fbind = widget.fbind
AttributeError:'str'对象没有属性'fbind'
预先感谢您的帮助。
答案 0 :(得分:2)
我没有运行您的代码的环境,但是查看回溯,我的猜测是问题出在这里:
self.message = TextInput(multiline=False).text
self.add_widget(self.message)
问题可能是第一行末尾的“ .text”。我的猜测是文本(存储在此处self.message中)不是有效的小部件。尝试删除“ .text”或将第二行更改为:
self.add_widget(Label(text=self.message))