首页 > 解决方案 > 从 python 代码而不是 KV 在屏幕之间切换

问题描述

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

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)

   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()
      

标签: pythonkivyscreen

解决方案


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

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

class MainWindow(Screen):
    def set(self):
       x=9
       y=8

       print(x,y)
       self.show_popupw()

而且,您还需要show_popupw使用 a 设置您的功能self parameter。因为您将通过它的selffor 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()

推荐阅读