【问题标题】:Changing screens from a Spinner on_text problem从 Spinner on_text 问题更改屏幕
【发布时间】:2019-08-30 18:20:33
【问题描述】:

我想使用 ScreenManager 创建一种工作流程,该工作流程会随着选项的选择而进行。我在 ScreenManager 中定义了几个屏幕,在一个屏幕中我创建了一个带有多个选项的 Spinner。我想从 on_text 调用一个函数并传递选定的 Spinner 值,根据传递的值执行任务,然后移动到工作流中的下一个屏幕。

我在一个类(屏幕)中创建了一个函数,其中只有一个 print() 用于测试目的。当我从 Spinner 中选择一个选项时,没有任何反应。

我只提供了我认为相关的代码...

kv 文件:

<HomeScreen>:
    name: 'homeScreen'
    BoxLayout:
        orientation: 'horizontal'

        Label:
            id: 'home'
            text: 'Home Screen'


<NewSession>:
    name: 'newSession'
    BoxLayout:
        orientation: 'vertical'

        Label:
            id: new
            text: 'Create New Session'

        Spinner:
            size_hint: None, None
            size: 100, 25
            id: category
            font_size: 12
            text: "Food Category" #default value showed
            values: ["Beef","Pork","Poultry", "Fish"] #list of values to show
            on_text: root.SelectCut(category.text)

        Widget:


<CurrentSession>:
    id: cs
    name: 'currentSession'
    BoxLayout:
        orientation: 'vertical'

        Label:
            id: cSession
            text: root.cSession

<History>:
    name: 'history'
    BoxLayout:
        orientation: 'vertical'

        Label:
            id: 'history'
            text: 'History Data Screen'

<ScreenManagement>:
    HomeScreen:
    NewSession:
    CurrentSession:
    History:

<AllScreen>:

    orientation: 'vertical'
    ScreenManagement:
        id: sm
    BoxLayout:

        size_hint_y: None
        height: 60
        spacing: 5
        padding: 5, 5, 0, 5

        Button:
            text: 'Home'
            on_press: root.ids.sm.current = 'homeScreen'
        Button:
            text: 'New Session'
            on_press: root.ids.sm.current = 'newSession'
        Button:
            text: 'Current Session'
            on_press: 
                root.ids.sm.current = 'currentSession'
        Button:
            text: 'History'
            on_press: root.ids.sm.current = 'history'

py 文件:

Builder.load_file("screenLayout.kv")

class ScreenManagement(ScreenManager):
    pass

class HomeScreen(Screen):
    pass

class NewSession(Screen):

    def SelectCut(self, text):
        print("In Select Cut")
        print("Food Category: " + text)
        self.sm.current = 'homeScreen'

class CurrentSession(Screen):
    pass

class History(Screen):
    pass

class AllScreen(BoxLayout):
    pass

class WorkingTestApp(App):

    def build(self):
        self.root = AllScreen()
        return self.root

if __name__ == '__main__':
    WorkingTestApp().run()

我无法弄清楚为什么从 Spinner 进行选择时什么都没有发生,并且我不确定从函数中移动到下一个屏幕的语法是什么。


更新: 当前代码(减去导入)

kv:

<HomeScreen>:
    name: 'homeScreen'
    BoxLayout:
        orientation: 'horizontal'

        Label:
            id: 'home'
            text: 'Home Screen'

<NewSession>:
    name: 'newSession'
    BoxLayout:
        orientation: 'vertical'

        Label:
            id: new
            text: 'Create New Session'

        Spinner:
            size_hint: None, None
            size: 100, 25
            id: category
            font_size: 12
            text: "Food Category" #default value showed
            values: ["Beef","Pork","Poultry", "Fish"] #list of values to show
            on_text: root.SelectCategory()

        Widget:

<SelectCut>:
    name: 'selectCut'
    BoxLayout:
        orientation: 'vertical'

        Label:
            id: 'cut'
            text: 'Select Cut Screen'

        Widget:

<CurrentSession>:
    name: 'currentSession'
    BoxLayout:
        orientation: 'vertical'

        Label:
            id: cSession
            text: 'Current Session Screen'

<History>:
    name: 'history'
    BoxLayout:
        orientation: 'vertical'

        Label:
            id: 'history'
            text: 'History Data Screen'

<ScreenManagement>:
    HomeScreen:
    NewSession:
    SelectCut:
    CurrentSession:
    History:

<AllScreen>:
    orientation: 'vertical'
    ScreenManagement:
        id: sm
    BoxLayout:

        size_hint_y: None
        height: 60
        spacing: 5
        padding: 5, 5, 0, 5

        Button:
            text: 'Home'
            on_press: root.ids.sm.current = 'homeScreen'
        Button:
            text: 'New Session'
            on_press: root.ids.sm.current = 'newSession'
        Button:
            text: 'Current Session'
            on_press: 
                root.ids.sm.current = 'currentSession'
        Button:
            text: 'History'
            on_press: root.ids.sm.current = 'history'

py:

Builder.load_file("screenLayout.kv")

class ScreenManagement(ScreenManager):
    pass

class HomeScreen(Screen):
    pass

class NewSession(Screen):
    # This Function isn't executing consistently.
    def SelectCategory(self):
        print("Food Category: " + self.ids.category.text)
        self.manager.current = 'selectCut'

class SelectCut(Screen):
    pass

class CurrentSession(Screen):
    pass

class History(Screen):
    pass

class AllScreen(BoxLayout):
    pass

class WorkingTestApp(App):

    def build(self):
        self.root = AllScreen()
        return self.root

if __name__ == '__main__':
    WorkingTestApp().run()

【问题讨论】:

  • 在您的SelectCut() 方法中,只需添加self.manager.current = 'someScreenName'。其中someScreenName 是您的Screens 之一的名称。
  • 您的SelectCut() 方法应在您的Spinner 文本更改时执行。如果它没有被调用,那么你的代码中肯定还有一些你没有发布的东西。
  • 我已经更新了我的原始帖子以包含完整的 py 文件(减去导入)和 kv 文件。

标签: python-3.x kivy spinner


【解决方案1】:

将您的微调器的on_text: root.SelectCut(self, text) 更改为on_text: root.SelectCut() 在.py 中将def SelectCut(self, text): 更改为def SelectCut(self): 然后更改您可以使用self.ids.category.text 读取或更改微调器的文本并将root.ids.sm.current = 'your screen' 添加到也有这个功能

【讨论】:

  • 感谢您的指点,它让我更接近了。我看到的一个奇怪行为是我可以从 Spinner 中选择一个选项,然后现在转到一个新屏幕,但是,如果我返回选择不同的选项,则 Spinner 上最初选择的默认选项,如果我选择了不同的选项最初什么都没有发生。如果我最终继续尝试不同的选择,它似乎会认识到变化。
  • 如果您将更新后的代码发布在您问题中已有内容下方的“更新:”部分中,我会看看我能为这个问题做些什么
  • 同样在您的 .kv 中,&lt;ScreenManagement&gt;: HomeScreen: NewSession: CurrentSession: History: 也不需要您应该将类​​似 screen_manager = ScreenManager() screen_manager.add_widget(ScreenOne(name="screenOne")) class TestApp(App): def build(self): return screen_manager 的内容添加到您的 .py 文件中,以便 screenmanager 直接从应用程序的类中处理所有内容
  • Bob - 感谢您的意见和耐心。当我实施您关于消除 : 并使用 .add_widget 的建议时,我丢失了包含所有屏幕的底部菜单栏。我将使用我正在尝试使用的当前代码更新我的原始帖子...我恢复到原来的状态。
  • 你到底想用你的按钮/屏幕实现什么,所以我知道更多他们应该做什么?就像当你打开应用程序时它应该如何工作的快速演练一样,直到它完成你想要的一切,因为我不确定 SelectCut 屏幕的用途或 on_text 事件应该发生什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多