如何修复Kivy Python程序中的“Screen already managed by another ScreenManager”错误?

xmakbtuz  于 2023-06-04  发布在  Python
关注(0)|答案(1)|浏览(350)

我将我的python kivy游戏代码分离出来,使其更易于管理。但我的代码根本不起作用。请帮帮我~
main.py

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager

from login_screen import loginscreen
from game_screen import gamescreen
from quiz_screen import quizscreen

class LanguageChanApp(App):
    screen_manager = ScreenManager()
    def build(self):
        self.screen_manager.add_widget(loginscreen)
        self.screen_manager.add_widget(gamescreen)
        self.screen_manager.add_widget(quizscreen)
    
        return self.screen_manager
    def goto_loginscreen(self):
        self.screen_manager.current = "Login Screen"
    def goto_gamescreen(self):
        self.screen_manager.current = "Game Screen"
    def goto_quizscreen(self):
        self.screen_manager.current = "Quiz Screen"
languagechanapp = LanguageChanApp()

if __name__ == "__main__":
    languagechanapp.run()

login_screen.py

from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.popup import Popup

from db_interface import userDBinter

Builder.load_file('login_screen_GUI.kv')

class LoginWin(GridLayout):
    def login(self):
        if self.ids.nickname.text == '' or self.ids.password.text == '':
            return
        usernum = userDBinter.selectUsernum(self.ids.nickname.text, self.ids.password.text)
        if usernum == None:
            Popup(title="Login Error", content=Label(text="Sorry, we can't find your account :("), size_hint=(1, 0.2), auto_dismiss=True).open()
        else:
            from main import languagechanapp
            languagechanapp.goto_gamescreen()
    def showRegwin(self):
        loginscreen.exchangeToRegwin()
loginwin = LoginWin()

class RegWin(GridLayout):
    def register(self):
        if self.ids.nickname.text == '' or self.ids.mailaddr.text == '' or self.ids.password.text == '':
            return
        if self.ids.password.text != self.ids.pwagain.text:
            Popup(title="Password Error", content=Label(text="Please write same pasword!!"), size_hint=(1, 0.2), auto_dismiss=True).open()
            return
        result = userDBinter.insertUserInfo(self.ids.nickname.text, self.ids.mailaddr.text, self.ids.password.text)
        if result == True:
            Popup(title="Congrats!", content=Label(text="Registration succeeded!"), size_hint=(1, 0.2), auto_dismiss=True).open()
        else:
            Popup(title="Registration Error", content=Label(text="Sorry, your registration is failed :("), size_hint=(1, 0.2), auto_dismiss=True).open()
    def showLoginwin(self):
        loginscreen.exchangeToLoginwin()
regwin = RegWin()

class LoginScreen(Screen):
    def __init__(self, **kwargs):
        super(LoginScreen, self).__init__(**kwargs)
    def showLoginwin(self):
        self.add_widget(loginwin)
    def exchangeToLoginwin(self):
        self.add_widget(loginwin)
        self.remove_widget(regwin)
    def exchangeToRegwin(self):
        self.add_widget(regwin)
        self.remove_widget(loginwin)
loginscreen = LoginScreen()

login_screen_GUI.kv

<LoginWin>
    cols: 2
    size_hint: 1, 0.2
    Label:
        text: "Nickname:"
        width: 100
        color: "black"
    TextInput:
        id: nickname
    Label:
        text: "Password:"
        width: 100
        color: "black"
    TextInput:
        id: password
    Button:
        id: loginbutton
        text: "Login"
        on_release: root.login()
    Button:
        id: regbutton
        text: "Register"
        on_release: root.showRegwin()

<RegWin>
    cols: 2
    size_hint: 1, 0.4
    Label:
        text: "Make nickname:"
        width: 100
        color: "black"
    TextInput:
        id: nickname
    Label:
        text: "Input mail address:"
        width: 100
        color: "black"
    TextInput:
        id: mailaddr
    Label:
        text: "Set password:"
        width: 100
        color: "black"
    TextInput:
        id: password
    Label:
        text: "Password again?"
        width: 100
        color: "black"
    TextInput:
        id: pwagain
    Button:
        id: regbutton
        text: "Register me!"
        on_release: root.register()
    Button:
        id: exitButton
        text: "Exit"
        on_release: root.showLoginwin()

<LoginScreen>
    name: "Login Screen"
    Button:
        pos_hint: {"anchor_x":"center", "anchor_y":"center"}
        size_hint: 1, 1
        background_normal: "graphics\\title_image.jpg"
        on_press: root.showLoginwin()

错误日志:

Traceback (most recent call last):
   File "c:\KivyApk\Language_Chan_App\main.py", line 26, in <module>
     languagechanapp.run()
   File "C:\Users\lje64\AppData\Local\Programs\Python\Python310\lib\site-packages\kivy\app.py", line 955, in run
     runTouchApp()
   File "C:\Users\lje64\AppData\Local\Programs\Python\Python310\lib\site-packages\kivy\base.py", line 574, in runTouchApp
     EventLoop.mainloop()
   File "C:\Users\lje64\AppData\Local\Programs\Python\Python310\lib\site-packages\kivy\base.py", line 339, in mainloop
     self.idle()
   File "C:\Users\lje64\AppData\Local\Programs\Python\Python310\lib\site-packages\kivy\base.py", line 383, in idle
     self.dispatch_input()
   File "C:\Users\lje64\AppData\Local\Programs\Python\Python310\lib\site-packages\kivy\base.py", line 334, in dispatch_input
     post_dispatch_input(*pop(0))
   File "C:\Users\lje64\AppData\Local\Programs\Python\Python310\lib\site-packages\kivy\base.py", line 302, in post_dispatch_input
     wid.dispatch('on_touch_up', me)
   File "kivy\_event.pyx", line 731, in kivy._event.EventDispatcher.dispatch
   File "C:\Users\lje64\AppData\Local\Programs\Python\Python310\lib\site-packages\kivy\uix\behaviors\button.py", line 179, in on_touch_up
     self.dispatch('on_release')
   File "kivy\_event.pyx", line 727, in kivy._event.EventDispatcher.dispatch
   File "kivy\_event.pyx", line 1307, in kivy._event.EventObservers.dispatch
   File "kivy\_event.pyx", line 1191, in kivy._event.EventObservers._dispatch
   File "C:\Users\lje64\AppData\Local\Programs\Python\Python310\lib\site-packages\kivy\lang\builder.py", line 55, in custom_callback
     exec(__kvlang__.co_value, idmap)
   File "C:\KivyApk\Language_Chan_App\login_screen_GUI.kv", line 19, in <module>
     on_release: root.login()
   File "c:\KivyApk\Language_Chan_App\login_screen.py", line 20, in login
     from main import languagechanapp
   File "c:\KivyApk\Language_Chan_App\main.py", line 23, in <module>
     languagechanapp = LanguageChanApp()
   File "c:\KivyApk\Language_Chan_App\main.py", line 13, in __init__
     self.screen_manager.add_widget(loginscreen)
   File "C:\Users\lje64\AppData\Local\Programs\Python\Python310\lib\site-packages\kivy\uix\screenmanager.py", line 995, in add_widget
     raise ScreenManagerException(
 kivy.uix.screenmanager.ScreenManagerException: Screen already managed by another ScreenManager.

是的,我知道循环进口是不好的。但我不喜欢把整个代码组合起来解决循环导入的问题。TT;;;;;

tf7tbtn2

tf7tbtn21#

你的代码看起来很乱。我建议创建一个“main.kv”文件,在其中可以添加屏幕管理器。

相关问题