Python Kivy移至build()内加载KV文件后屏幕切换功能失效的解决方案求助
Hey there! Let's fix that screen switching issue you're having. I went through your code and spotted a few key problems that are causing the button clicks not to switch screens properly. Here's what you need to adjust:
1. Fix the Screen Switching Logic in ViewSpecificFindInfo
Your marked line needs to use the correct reference to your WindowManager instance. Replace the commented section with this:
def ViewSpecificFindInfo(self, FindName, FindDate, FindTime, ObjectInfo): print(FindName, FindDate, FindTime) # Update app-wide properties app = App.get_running_app() app.FindInfoName = FindName app.FindInfoDate = FindDate app.FindInfoTime = FindTime # Switch to the specific find screen app.root.current = "Specific Find Info Window"
2. Fix the BackToMainMenu Function
Your current code for returning to the main menu is incorrect. Update it to use the same screen switching pattern:
def BackToMainMenu(self, event): App.get_running_app().root.current = "Base Window"
3. Load Your KV File Properly
You commented out the KV loader, and your build() method just returns pass—this means Kivy isn't loading your layout file. Fix the MetalDetectorsFriendApp class's build method:
class MetalDetectorsFriendApp(App): FindInfoName = StringProperty("Name of Find") FindInfoDate = StringProperty("Date of Find") FindInfoTime = StringProperty("Time of Find") def build(self): # Load the KV file here so your layouts are initialized return Builder.load_file('MetalDetectorsFriend.kv') # ... existing build_config and build_settings methods ...
4. Fix Scope Issues in AllFindsGridLayout
Your grid layout is trying to access self.AllFinds which doesn't exist (it's a global variable). Also, move your ViewSpecificFindInfo and BackToMainMenu functions inside the class to avoid scope errors:
class AllFindsGridLayout(GridLayout): FindButton={} # Create dictionary, used for holding the ids!! FindInfoName = StringProperty("Name of Find") FindInfoDate = StringProperty("Date of Find") FindInfoTime = StringProperty("Time of Find") def __init__(self, **kwargs): super().__init__(**kwargs) global AllFinds # Access the global variable BackButton = Button(text="Back",color=(1,1,0,1)) BackButton.bind(on_release=self.BackToMainMenu) self.add_widget(BackButton) for Find in AllFinds: self.FindButton[Find.Name] = Button(text = Find.Name, size_hint_y=None,height=dp(100)) self.add_widget(self.FindButton[Find.Name]) self.FindButton[Find.Name].bind(on_release=partial(self.ViewSpecificFindInfo, Find.Name, Find.Date, Find.Time )) def ViewSpecificFindInfo(self, FindName, FindDate, FindTime, ObjectInfo): print(FindName, FindDate, FindTime) app = App.get_running_app() app.FindInfoName = FindName app.FindInfoDate = FindDate app.FindInfoTime = FindTime app.root.current = "Specific Find Info Window" def BackToMainMenu(self, event): App.get_running_app().root.current = "Base Window"
Why These Fixes Work
- Screen switching:
App.get_running_app().rootrefers directly to yourWindowManagerinstance, which controls screen transitions via itscurrentproperty. - KV loading: Your
build()method must return the loaded layout so Kivy knows what to render. - Class methods: Keeping functions inside their respective classes ensures proper access to
selfand avoids messy global scope issues. - Global variable access: Using
global AllFindslets your grid layout correctly iterate over the loaded find objects.
Once you apply these changes, your dynamically created buttons should switch to the SpecificFindInfoWindow and update the displayed find details as expected.
内容的提问来源于stack exchange,提问作者Bohdan

