Python & Kivy Attribute Error:
Python & Kivy Attribute Error:
我正在尝试在客户端和服务器基础上使用 kivy & python 制作文件选择器代码...代码的其余部分工作正常但给定部分给出属性错误为“'ClientScreen' 没有属性 'openfile_from_filechooser'"
我在哪一部分犯了错误...我搜索了如何解决属性错误但是 none 的工作...
class ClientScreen(Screen):
def __init__(self, **kwargs):
super(ClientScreen, self).__init__(**kwargs)
def openfile(self, path, filename):
f = open(os.path.join(path, filename[0]))
print f.read()
#self.clear_widgets()
def selected(self, filename):
print "selected: %s" % filename[0]
def openfile_from_filechooser(self, filechooser):
self.openfile(filechooser.path, filechooser.selection)
def selected_from_filechooser(self, filechooser):
self.selected(filechooser.selection)
def terminate(instance):
sys.exit()
def data(instance):
global IP
global conn
global host
global port
global address
print("Inside Data")
self.clear_widgets()
print("Inside File Operation")
button1 = Button(text = 'Open',size_hint = (None,None),pos = (0,0))
self.add_widget(button1)
button1.bind(on_release=partial(self.openfile_from_filechooser, filechooser))
button2 = Button(text = 'Send',size_hint = (None,None),pos = (100,0))
self.add_widget(button2)
#button2.bind(on_release=partial(self.selected_from_filechooser, filechooser))
def connection(instance):
global IP
global conn
global host
global port
global address
self.clear_widgets()
text = IP.text
print(text)
self.add_widget(Label (text = text))
print "Imported socket module"
print "Imported sys module used for termination condition"
conn = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
print('Socket created')
host = IP.text
print "Host Name:",host
port = 8000
print "Port value accessed",port
try:
self.clear_widgets()
address = (host,port)
print('Value of Address:',address)
print('Trying to connect...')
print('Initiating TCP server connection')
self.add_widget(Label(text = 'Connected....Press Next To Proceed...'))
button1 = Button(text = 'Next',size_hint = (None,None),pos = (0,0))
self.add_widget(button1)
button1.bind(on_press = data)
except socket.error as msg:
self.clear_widgets()
self.add_widget(Label(text = 'Sorry...Unable to create socket...press Finish to terminate...'))
button2 = Button(text = 'Finish',size_hint = (None,None),pos = (0,0))
self.add_widget(button2)
button2.bind(on_press = terminate)
print('Socket not created')
def ip_addr(instance):
global IP
self.clear_widgets()
self.add_widget(Label (text = "Enter the destination device's IP address"))
IP = TextInput(multiline=False, size_hint = (1,0.1), pos = (0,200))
print(IP.text)
self.add_widget(IP)
button1 = Button(text = 'Enter',size_hint = (None,None),pos = (0,0))
self.add_widget(button1)
button1.bind(on_press = connection)
global IP
self.add_widget(Label(text = 'Working as Client...Press Next to proceed'))
button1 = Button(text = 'Next',size_hint = (None,None),pos = (0,0))
self.add_widget(button1)
button1.bind(on_press = ip_addr)
如果有人能指出我的错误在哪里以及我该如何纠正错误,那将非常有帮助
你的缩进是错误的,所有函数都在你的 __init__
中声明,而不是作为方法。
您也不要调用 super(ClientScreen, self).__init__(**kwargs)
,这会导致其他问题,因为这意味着屏幕和小部件内部未初始化。
我正在尝试在客户端和服务器基础上使用 kivy & python 制作文件选择器代码...代码的其余部分工作正常但给定部分给出属性错误为“'ClientScreen' 没有属性 'openfile_from_filechooser'"
我在哪一部分犯了错误...我搜索了如何解决属性错误但是 none 的工作...
class ClientScreen(Screen):
def __init__(self, **kwargs):
super(ClientScreen, self).__init__(**kwargs)
def openfile(self, path, filename):
f = open(os.path.join(path, filename[0]))
print f.read()
#self.clear_widgets()
def selected(self, filename):
print "selected: %s" % filename[0]
def openfile_from_filechooser(self, filechooser):
self.openfile(filechooser.path, filechooser.selection)
def selected_from_filechooser(self, filechooser):
self.selected(filechooser.selection)
def terminate(instance):
sys.exit()
def data(instance):
global IP
global conn
global host
global port
global address
print("Inside Data")
self.clear_widgets()
print("Inside File Operation")
button1 = Button(text = 'Open',size_hint = (None,None),pos = (0,0))
self.add_widget(button1)
button1.bind(on_release=partial(self.openfile_from_filechooser, filechooser))
button2 = Button(text = 'Send',size_hint = (None,None),pos = (100,0))
self.add_widget(button2)
#button2.bind(on_release=partial(self.selected_from_filechooser, filechooser))
def connection(instance):
global IP
global conn
global host
global port
global address
self.clear_widgets()
text = IP.text
print(text)
self.add_widget(Label (text = text))
print "Imported socket module"
print "Imported sys module used for termination condition"
conn = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
print('Socket created')
host = IP.text
print "Host Name:",host
port = 8000
print "Port value accessed",port
try:
self.clear_widgets()
address = (host,port)
print('Value of Address:',address)
print('Trying to connect...')
print('Initiating TCP server connection')
self.add_widget(Label(text = 'Connected....Press Next To Proceed...'))
button1 = Button(text = 'Next',size_hint = (None,None),pos = (0,0))
self.add_widget(button1)
button1.bind(on_press = data)
except socket.error as msg:
self.clear_widgets()
self.add_widget(Label(text = 'Sorry...Unable to create socket...press Finish to terminate...'))
button2 = Button(text = 'Finish',size_hint = (None,None),pos = (0,0))
self.add_widget(button2)
button2.bind(on_press = terminate)
print('Socket not created')
def ip_addr(instance):
global IP
self.clear_widgets()
self.add_widget(Label (text = "Enter the destination device's IP address"))
IP = TextInput(multiline=False, size_hint = (1,0.1), pos = (0,200))
print(IP.text)
self.add_widget(IP)
button1 = Button(text = 'Enter',size_hint = (None,None),pos = (0,0))
self.add_widget(button1)
button1.bind(on_press = connection)
global IP
self.add_widget(Label(text = 'Working as Client...Press Next to proceed'))
button1 = Button(text = 'Next',size_hint = (None,None),pos = (0,0))
self.add_widget(button1)
button1.bind(on_press = ip_addr)
如果有人能指出我的错误在哪里以及我该如何纠正错误,那将非常有帮助
你的缩进是错误的,所有函数都在你的 __init__
中声明,而不是作为方法。
您也不要调用 super(ClientScreen, self).__init__(**kwargs)
,这会导致其他问题,因为这意味着屏幕和小部件内部未初始化。