我已经写了一个注册页面为我的应用程序.一切都很顺利,直到我更新与角色选项选择代码.我已经尝试调试,但问题仍然存在.
main.py `
# main.py
from kivy.core.text import LabelBase
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.factory import Factory
from kivy.uix.button import Button
import requests
Window.size = (310, 580)
class NavBarScreen(Screen):
pass
class HomeScreen(Screen):
pass
class SignUpScreen(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.dropdown = DropDown()
self.role_button = Button(text='Select Role', size_hint=(None, None), height=44)
self.role_button.bind(on_release=self.dropdown.open)
self.dropdown.bind(on_select=lambda instance, x: setattr(self.role_button, 'text', x))
self.add_widget(self.role_button)
for role in ["Farmer", "Administrator"]:
btn = Button(text=role, size_hint_y=None, height=44)
btn.bind(on_release=lambda btn: self.dropdown.select(btn.text))
self.dropdown.add_widget(btn)
def signup_request(self, username, password, role):
signup_data = {"username": username, "password": password, "role": role}
api_url = "https://c272-2001-d08-d2-d387-2cd9-bbf2-5f29-cacf.ngrok-free.app/signup"
response = requests.post(api_url, json=signup_data)
if response.status_code == 200:
print("Successfully signed up! Token: ", response.json()["access_token"])
else:
print("Signup failed. Error: ", response.json()["detail"])
class Myapp(MDApp):
def build(self):
screen_manager = ScreenManager()
screen_manager.add_widget(Builder.load_file("start.kv"))
screen_manager.add_widget(Builder.load_file("signup.kv"))
screen_manager.add_widget(Builder.load_file("login.kv"))
home_screen = HomeScreen(name="home")
home_screen.add_widget(NavBarScreen())
screen_manager.add_widget(home_screen)
return screen_manager
def show_role_dropdown(self, text_field):
text_field.focus = False
self.screen.ids.role_dropdown.open()
def select_role(self, role):
self.screen.ids.role_input.text = role
self.screen.ids.role_dropdown.dismiss()
def change_color(self, instance):
if instance in self.root.ids.values():
current_id = list(self.root.ids.keys())[list(self.root.ids.values()).index(instance)]
for i in range(4):
if f"nav_icon{i}" == current_id:
self.root.ids[f"nav_icon{i + 1}"].text_color = 1, 0, 0, 1
else:
self.root.ids[f"nav_icon{i + 1}"].text_color = 0, 0, 0, 1
def goto_home_screen(self):
self.root.current = "home"
if __name__ == "__main__":
LabelBase.register(name="Poppins-Medium", fn_regular=r"C:\Users\User\Desktop\FONT\Poppins\Poppins-Medium.ttf")
LabelBase.register(name="Poppins-SemiBold", fn_regular=r"C:\Users\User\Desktop\FONT\Poppins\Poppins-SemiBold.ttf")
Factory.register("NavBarScreen", cls=NavBarScreen)
# Create an instance of Myapp and run it
app = Myapp()
app.run()
字符串
signup.kv
# signup.kv
<SignUpScreen>:
name: "signup"
MDScreen:
MDFloatLayout:
md_bg_color: rgba(246, 250, 247, 255)
Image:
source: r"C:\Users\User\Desktop\FONT\Pytho2farmers.zip"
pos_hint: {"center_x": 0.75, "center_y": .15}
MDIconButton:
icon: "arrow-left"
pos_hint: {"center_y": .95}
user_font_size: "30sp"
theme_text_color: "Custom"
text_color: rgba(26, 24, 58, 255)
on_release:
root.manager.transition.direction = "right"
root.manager.current = "main"
MDLabel:
text: "Hi!"
font_name: "Poppins-SemiBold"
font_size: "26sp"
pos_hint: {"center_x": .6,"center_y": .85}
color: rgba(48, 62, 39, 255)
MDLabel:
text: "Create an account"
font_name: "Poppins-SemiBold"
font_size: "18sp"
pos_hint: {"center_x": .6, "center_y": .79}
color: rgba(180, 187, 114, 255)
MDFloatLayout:
size_hint: .7, .07
pos_hint: {"center_x":.5, "center_y":.71}
TextInput:
hint_text: "Username"
font_name: "Poppins-Medium"
size_hint_y: .75
pos_hint: {"center_x":.5, "center_y":.5}
background_color: 1, 1, 1, 0
foreground_color: rgba(0, 0, 59, 255)
cursor_color: rgba(0, 0, 59, 255)
font_size: "14sp"
cursor_width: "2sp"
multiline: False
MDFloatLayout:
pos_hint: {"center_x": .5, "center_y": 0}
size_hint_y: .03
md_bg_color: rgba(178, 178, 178, 255)
MDFloatLayout:
size_hint: .7, .07
pos_hint: {"center_x":.5, "center_y":.6}
TextInput:
hint_text: "Password"
font_name: "Poppins-Medium"
size_hint_y: .75
pos_hint: {"center_x":.5, "center_y":.5}
background_color: 1, 1, 1, 0
foreground_color: rgba(0, 0, 59, 255)
cursor_color: rgba(0, 0, 59, 255)
font_size: "14sp"
cursor_width: "2sp"
multiline: False
MDFloatLayout:
pos_hint: {"center_x": .5, "center_y": 0}
size_hint_y: .03
md_bg_color: rgba(178, 178, 178, 255)
MDFloatLayout:
size_hint: .7, .07
pos_hint: {"center_x":.5, "center_y":.6}
MDTextField:
id: role_input # Add this ID
hint_text: "Role"
font_name: "Poppins-Medium"
size_hint_y: .75
pos_hint: {"center_x": .5, "center_y": .5}
background_color: 1, 1, 1, 0
foreground_color: rgba(0, 0, 59, 255)
cursor_color: rgba(0, 0, 59, 255)
font_size: "14sp"
cursor_width: "2sp"
multiline: False
readonly: True
on_focus: if self.focus: app.show_role_dropdown(self)
MDRaisedButton:
id: role_dropdown_btn
pos_hint: {'center_x': .5, 'center_y': .6}
text: "Choose Role"
on_release: app.show_role_dropdown(role_input)
MDRaisedButton:
text: "SIGNUP"
size_hint: .66, .065
pos_hint: {"center_x": .5, "center_y":.33}
background_color: 0, 0, 0, 0
font_name: "Poppins-SemiBold"
on_release:
root.signup_request(username_input.text, password_input.text, role_input.text)
型
我希望整个事情可以运行,但它不能。我尝试了调试和它所显示的是
'ScreenManager accepts only Screen widget.')
型
并且还
(<class 'kivy.uix.screenmanager.ScreenManagerException'>, ScreenManagerException('ScreenManager accepts only Screen widget.'), <traceback object at 0x000001548362E048>)
型
和
File "C:\Users\User\PycharmProjects\Assignment\main.py", line 47, in build
screen_manager.add_widget(Builder.load_file("signup.kv"))
File "C:\Users\User\PycharmProjects\Assignment\.venv\lib\site-packages\kivy\uix\screenmanager.py", line 988, in add_widget
'ScreenManager accepts only Screen widget.')
kivy.uix.screenmanager.ScreenManagerException: ScreenManager accepts only Screen widget.
型
我已经不知道了。我已经检查了类似的问题的解决方案,但问题仍然存在。你是我解决这个问题的最后希望。
1条答案
按热度按时间mctunoxg1#
问题是代码:
字符串
返回
None
,这会在您尝试将其添加到screen_manager
时导致错误。上述代码返回None
的原因是kv
文件包含一个规则<SignUpScreen>:
,该规则定义了SignUpScreen
应该如何构建。它实际上并没有构建SignUpScreen
的示例。您可以通过更改来修复此问题:型
收件人:
型