我正在尝试在check_login()
文件中调用方法kv
,但无法识别,当我按下kv
文件中的第一个按钮时,会产生此错误:< / p>
AttributeError: 'LoginWindow' object has no attribute 'check_login'
这是我的代码:
loginpageGUI.py
# importing library
import kivy
# version
kivy.require('1.11.1')
# importing functionality
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ObjectProperty
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen
# defining screens
class LoginWindow(Screen):
pass
class PreferencesWindow(Screen):
pass
class HomeWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
# background
Window.clearcolor = (0.67, 0.83, 0.88, 1)
# creating layout class
class MyFloatLayout(FloatLayout):
username = ObjectProperty(None)
password = ObjectProperty(None)
# defining processing method of the login button
def check_login(self):
"""
Processing, will fix later
"""
print("Login succesful!")
# reset the textinputs to empty strings once pressed and processed
self.username.text = ''
self.password.text = ''
# navigate to home screen
app.root.current = "home"
root.manager.transition.direction = "right"
# linking .py with .kv
kv = Builder.load_file('gui.kv')
# creating application class that returns variable kv
class MyApp(App):
def build(self):
return (kv)
if __name__ == '__main__':
MyApp().run()
gui.kv
WindowManager:
LoginWindow:
HomeWindow:
PreferencesWindow:
<Button>:
font_size: 27
size_hint: 0.2, 0.1
background_color: 0.1, 0.5, 0.6, 1
color: 1, 1, 1, 1
<Label>:
font_size: 18
size_hint: 0.1, 0.05
color: 0.1, 0.5, 0.6, 1
<TextInput>:
font_size: 14
size_hint: 0.3, 0.05
<LoginWindow>:
name: "login"
MyFloatLayout:
username: username
password: password
Button:
pos_hint:{'center_y':0.43, 'center_x': 0.5}
id: to_home
text: "Login"
on_press: root.check_login()
color: 1, 1, 1, 1
Label:
pos_hint:{'center_y':0.57, 'center_x': 0.35}
text: "Username"
TextInput:
pos_hint:{'center_y':0.57, 'center_x': 0.6}
id: username
multiline: False
Label:
pos_hint:{'center_y':0.5, 'center_x': 0.35}
text: "Password"
TextInput:
pos_hint:{'center_y':0.5, 'center_x': 0.6}
id: password
multiline: False
<HomeWindow>:
name: "home"
MyFloatLayout:
Button:
text: "Log out"
on_release:
app.root.current = "login"
root.manager.transition.direction = "right"
pos_hint:{'center_x': 0.5, 'center_y': 0.57}
Button:
text: "Preferences"
id: to_preferences
on_release:
app.root.current = "preferences"
root.manager.transition.direction = "left"
pos_hint:{'center_x': 0.5, 'center_y': 0.43}
<PreferencesWindow>
name: "preferences"
Button:
text: "Home"
id: to_home
on_release:
app.root.current = "home"
root.manager.transition.direction = "right"
pos_hint:{'center_x': 0.5, 'center_y': 0.43}
Label:
text: "To be further processed"
以某种方式,执行时无法识别类login_check()
中的方法MyFloatLayout
,这会使程序失败。我看过SO,但到目前为止还没有发现类似的问题。一些帮助会很棒。
编辑:我刚刚意识到,到目前为止,root
是LoginWindow
,因为这是我们在此讨论的按钮的根。我实际上想称该根的子代MyFloatLayout
。我该怎么办?
答案 0 :(得分:0)
您可以通过在kv文件中为undefined
定义一个id并通过该id访问其功能来实现。更新后的gui.kv文件如下:
MyFloatLayout
但是loginpageGUI.py文件中也几乎没有问题。您需要在WindowManager:
LoginWindow:
HomeWindow:
PreferencesWindow:
<Button>:
font_size: 27
size_hint: 0.2, 0.1
background_color: 0.1, 0.5, 0.6, 1
color: 1, 1, 1, 1
<Label>:
font_size: 18
size_hint: 0.1, 0.05
color: 0.1, 0.5, 0.6, 1
<TextInput>:
font_size: 14
size_hint: 0.3, 0.05
<LoginWindow>:
name: "login"
MyFloatLayout:
id: myfloat
username: username
password: password
Button:
pos_hint:{'center_y':0.43, 'center_x': 0.5}
id: to_home
text: "Login"
on_press: myfloat.check_login()
color: 1, 1, 1, 1
Label:
pos_hint:{'center_y':0.57, 'center_x': 0.35}
text: "Username"
TextInput:
pos_hint:{'center_y':0.57, 'center_x': 0.6}
id: username
multiline: False
Label:
pos_hint:{'center_y':0.5, 'center_x': 0.35}
text: "Password"
TextInput:
pos_hint:{'center_y':0.5, 'center_x': 0.6}
id: password
multiline: False
<HomeWindow>:
name: "home"
MyFloatLayout:
Button:
text: "Log out"
on_release:
app.root.current = "login"
root.manager.transition.direction = "right"
pos_hint:{'center_x': 0.5, 'center_y': 0.57}
Button:
text: "Preferences"
id: to_preferences
on_release:
app.root.current = "preferences"
root.manager.transition.direction = "left"
pos_hint:{'center_x': 0.5, 'center_y': 0.43}
<PreferencesWindow>
name: "preferences"
Button:
text: "Home"
id: to_home
on_release:
app.root.current = "home"
root.manager.transition.direction = "right"
pos_hint:{'center_x': 0.5, 'center_y': 0.43}
Label:
text: "To be further processed"
块中定义app
,然后才能使用__name__
。同样在app
函数中,需要将check_login
更改为root.manager.transition.direction
。更新后的loginpageGUI.py如下:
app.root.transition.direction