【问题标题】:Switching between screens from python code not KV从 python 代码而不是 KV 在屏幕之间切换
【发布时间】:2021-11-24 21:45:03
【问题描述】:

我是 Kivy 的新手 我想在 Python 而不是 KV 中管理屏幕之间的更改 我在 Python 中编写了一个函数,当在弹出窗口中按下按钮时,屏幕将变为第二个屏幕 正如您在终端中看到的那样,这些值似乎是正确的[与以前的版本不同,我得到一个错误,没有第二个屏幕这样的名称] 我做的不对吗?为什么该功能不切换到第二个屏幕?

一年

import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen  
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty ,NumericProperty
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.popup import Popup

class MainWindow(Screen):

def set(self):

   x=9

   y=8

   print(x,y)

   show_popupw()

def change_screen(self):
    print("c_S")
    self.sm = ScreenManager()
    self.sm.add_widget(SecondWindow(name='second'))
    self.sm.current = 'second'
    print(self.sm)
    print(self.sm.current)
  



def show_popupw():

show = Pow()



popupWindow = Popup(title="Popup Window", content=show, size_hint=(None,None),size=(400,400))



popupWindow.open()

    



class SecondWindow(Screen):

a = NumericProperty()

def sec(self):

    self.a = self.a + 1

    print(self.a)

   






class WindowManager(ScreenManager):
   pass





 kv = Builder.load_file('mymainsctaq.kv')





class Pow(FloatLayout):

  

    mainw=MainWindow()     


class mymainscreenApp(App):

    def build(self):
     

      return kv





if __name__ == "__main__":

   mymainscreenApp().run()

千伏

  WindowManager:

  MainWindow:

  SecondWindow:

 <MainWindow>:
    name: "main"
    GridLayout:

    cols:2
    GridLayout:
        cols: 4
        Label:

            text: "name: "



        TextInput:

            id: name

            multiline: False



    Button:

        text: "Submit"

        on_release:
            app.root.current = "second" if name.text == "go" else "main"
            root.manager.transition.direction = "left"

    Button:

        text: "set"

        on_release:

            root.set()





<SecondWindow>:
   name: 'second'
   GridLayout:

    cols:2



    GridLayout:

        cols: 4

       

          

        Label:

            text: "num: "  + str(root.a)

        Button:

            text: "Go Back"

            on_press:

                

                app.root.current = "main"

                root.manager.transition.direction = "up"

                root.sec()



<Pow>:

   Label:

    text: "You set?"

    size_hint: 0.6, 0.2

    pos_hint: {"x":0.2, "top":1}



Button:

    text: "ok,set"

    size_hint: 0.8, 0.2

    pos_hint: {"x":0.1, "y":0.1}                   

    on_release:
        root.mainw.change_screen()
      

【问题讨论】:

    标签: python kivy screen


    【解决方案1】:

    在 python 中调用同一类的函数时,您需要使用self 指定您是从同一类调用该函数。

    所以在您的代码中的 .py 文件中。当您在MainWindow 类中调用函数show_popupw() 时,您需要使用self

    class MainWindow(Screen):
        def set(self):
           x=9
           y=8
    
           print(x,y)
           self.show_popupw()
    

    此外,您还需要将 show_popupw 函数设置为 self parameter。因为您将通过它的self 传递给reference

    def show_popupw(self):
        show = Pow()
        popupWindow = Popup(title="Popup Window",
                            content=show,
                            size_hint=(None,None),
                            size=(400,400))
        popupWindow.open()
    

    您的 .py 文件应如下所示:

    import kivy
    from kivy.app import App
    from kivy.lang import Builder
    from kivy.uix.screenmanager import ScreenManager, Screen  
    from kivy.uix.boxlayout import BoxLayout
    from kivy.properties import ObjectProperty ,NumericProperty
    from kivy.uix.button import Button
    from kivy.uix.label import Label
    from kivy.uix.floatlayout import FloatLayout
    from kivy.uix.popup import Popup
    
    class MainWindow(Screen):
        def set(self):
           x=9
           y=8
    
           print(x,y)
           self.show_popupw()
    
        def change_screen(self):
            print("c_S")
            self.sm = ScreenManager()
            self.sm.add_widget(SecondWindow(name='second'))
            self.sm.current = 'second'
            print(self.sm)
            print(self.sm.current)
    
        def show_popupw(self):
            show = Pow()
            popupWindow = Popup(title="Popup Window",
                                content=show,
                                size_hint=(None,None),
                                size=(400,400))
            popupWindow.open()
    
    class SecondWindow(Screen):
        a = NumericProperty()
    
        def sec(self):
            self.a = self.a + 1
            print(self.a)
    
    class WindowChanger(ScreenManager):
       pass
    
    class Pow(FloatLayout):
        mainw=MainWindow()  
    
    kv = Builder.load_file('mymainsctaq.kv')
    
    class mymainscreenApp(App):
        def build(self):
            return kv
    
    if __name__ == "__main__":
       mymainscreenApp().run()
    

    【讨论】:

      猜你喜欢
      • 2018-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多