我正在将所有初始化 python 代码移动到我拥有的 main.py 文件中,在此过程中,我的登录页面从 this 变成了 {{3} }.顶部栏是一个无法正常工作的框布局。我想检查是.kv文件还是我写的python,所以我改变了BoxLayout的位置,把它放在底部,它起作用了。见this。我想知道代码出了什么问题以及如何修复它,因为我以前从未发生过这种情况。
这里是main.py文件
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.screenmanager import Screen, ScreenManager
from LogInWindow import LogInWindow # importing the class from Login.py
class WindowManager(ScreenManager):
pass
sm = WindowManager()
screens = [LogInWindow(name="Login_window")]
for screen in screens:
sm.add_widget(screen)
sm.current = "Login_window"
class FitnessApp(App):
def build(self):
App.title = "Fitness App"
Window.size = (1080, 720)
return sm
if __name__ == '__main__': # The value of __name__ attribute is set to “__main__” when module is run as main program
FitnessApp = FitnessApp()
FitnessApp.run()
这里是 kivy 文件(顶部的框布局不起作用)
<LogInWindow>:
id: Login_window ## setting the id to Login_window
orientation: "vertical" ## setting orientation to vertical
space_x: self.size[0]/4.5 ## setting space to a 1/4.5 of the screen either side
BoxLayout: ## setting up a box layout for the top bar
size_hint_y: None ## setting size hint to none so it can be set to anything
height: 100 ## setting height to 100
canvas.before:
Color:
rgba: (0, 0, 0, 1) ## changing the colour of the top bar
Rectangle:
size: self.size ## setting position of rect to the position of the box
pos: self.pos ## setting size of rect to the size of the box
Label: ## creating the "title"
text: "Login" ## setting the text
font_size: 48 ## changing the font size
bold: True ## changing it to bold
size_hint_x: .9 ## setting hint to 90%
BoxLayout: ## setting up a box layout for the rest of the page
orientation: 'vertical' ## setting orientation to vertical
padding: Login_window.space_x, 10 ## setting the padding between box and children
spacing: 20 ## adding a spacing between the child widgets
canvas.before:
Color:
rgba: (212/255,212/255,212/255, 1) ## setting colour to light grey
Rectangle:
size: self.size ## setting size to the size of the box layout
pos: self.pos ## setting position to the position of the box layout
BoxLayout:
orientation: "vertical" ## setting orientation to vertical
spacing: 30 ## adding a spacing between the child widgets
size_hint_y: None ## setting size hint to none so it can be set to anything
height: 170 ## setting height to 170
canvas.before:
Color:
rgba: (212/255,212/255,212/255, 1) ## setting colour to light grey
Rectangle:
size: self.size ## setting size to the size of the box layout
pos: self.pos ## setting position to the position of the box layout
Label:
id: info ## setting id to info
text: '' ## setting the text to empty
markup: True ## allows styling of text
size_hint_y: None ## setting size hint to none so it can be set to anything
height: 20 ## setting height to 20
TextInput: ## creating a text input for username
id: username_field ## setting the id to username_field
size_hint_x: 1 ## setting the size hint to 1 so it spans the whole box
hint_text: "Username" ## setting the translucent text to username
font_size: 24 ## setting font size to 24
multiline: False ## not allowing multiline inputs
write_tab: False ## making tab go to the password field, not create 4 spaces
focus: True ## lets the tab key work
on_text_validate: password_field.focus = True ## will check if password is filled if enter key pressed
TextInput: ## creating a text input for password
id: password_field ## setting the id to password_field
hint_text: "Password" ## setting the translucent text to Password
font_size: 24 ## setting font size to 24
multiline: False ## not allowing multiline inputs
write_tab: False ## does not allow the tab to create 4 spaces
password: True ## sets characters to *
on_text_validate: root.validate_user() ## runs the function to validate
Label: ## creating a label for spacing
id: spacing ## set the id to spacing
size_hint_y: None ## setting size hint to none so it can be set to anything
height: 40 ## setting height to 40
Button:
text: "Don't have an account? Sign Up Here!" ## setting the text to the sign up
font_size:20 ## changing font size
size_hint_y: None ## setting size hint to none so it can be set to anything
height: 60 ## setting height to 60
background_color: (0, 0, 0, 1) ## setting background colour
background_normal: '' ## changing the background_normal to nothing
on_release: root.validate_user() ##will change to put it to sign up screen
Button:
text: "Sign In" ## setting the text to the sign in
bold: True ## changing it to bold
font_size:48 ## changing font size
size_hint_y: None ## setting size hint to none so it can be set to anything
height: 100 ## setting height to 100
background_color: (0, 0, 0, 1) ## setting background color to white
background_normal: '' ## background normal to nothing
on_release: root.validate_user() ## setting the button to validate the user
Label: ## creating a label for spacing
id: spacing2 ## setting id to spacing2
仅供参考,这里是 main.py 启动时打开的类的前几行。
class LogInWindow(Screen, BoxLayout): # creating LogInWindow class
kv = Builder.load_file("LogInWindow.kv") # loading the kivy file which has all the
任何帮助将不胜感激。
答案 0 :(得分:0)
您的 LoginWindow
使用了 Screen
和 BoxLayout
的多重继承。 Screen
类扩展了 RelativeLayout
,因此有一个 do_layout()
方法,该方法遵循自己的规则来布置其子项。 BoxLayout
还有一个 do_layout()
,它遵循 BoxLayout
规则来布置其子项。那么,您的 LoginWindow
的孩子是如何安排的?
Python 有一个方法解析顺序,用于确定在这种情况下调用哪个 do_layout()
方法。在您的情况下,将调用 do_layout()
类的 Screen
。因此,结果是 LoginWindow
的子级不是使用 BoxLayout
规则布置的,而是使用 RelativeLayout
规则布置的。这意味着最后一个孩子可能会被吸引到之前的孩子身上(如您所见)。
每当您使用多重继承时,您都必须仔细考虑这一点以避免这个常见问题。我建议将 LoginWindow
更改为仅从 Screen
继承,并且只在其 BoxLayout
规则中使用顶级 kv
。