2015-05-06 70 views
0

我是基維新手。當在主屏幕(登錄屏幕)上單擊'註冊'按鈕時,我想切換到另一個屏幕(註冊屏幕)。這裏是Python代碼:屏幕開關不會發生在按鈕點擊kivy

import kivy 
kivy.require('1.9.0') 
import ConfigParser 
from kivy.app import App 
from kivy.core.window import Window 
from kivy.uix.screenmanager import Screen, ScreenManager 

Window.size = (700, 550) 

class LoginPage(Screen): 
    def switch_screen(*args): 
     global sm 
     print "i am in login and will switch to signup" 
     sm.current = 'signup' 

class SignUp(Screen): 
    print "signup screen" 
    pass 

sm = ScreenManager() 
sm.add_widget(LoginPage(name="login")) 
sm.add_widget(SignUp(name="signup")) 

class Test(App): 
    icon = "C:\Users\shru\Desktop\download.jpg" 
    title = "Member Login" 
    def build(self): 
     return sm 

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

這裏是.kv代碼

<LoginPage>: 
     canvas: 
     Rectangle: 
      source: "C:\Users\shru\Desktop\m.jpg" 
      size: self.size 

     Widget: 
     Button: 
      size: 90,35 
      pos: 380,200 
      font_size:15 
      background_color: (1,1,1,0.3) 
      text: "SignUp" 
      on_press: root.switch_screen() 

<SignUp>: 
    canvas: 
     Rectangle: 
      source: "C:\Users\shru\Desktop\m.jpg" 
      size: self.size 
    Widget: 
     Button: 
      size: 90,35 
      pos: 380,200 
      font_size:15 
      background_color: (1,1,1,0.3) 
      text: "hello" 

這對執行返回空白屏幕。難道我做錯了什麼?

謝謝

回答

1

的應用程序運行前,這也是KV文件被加載之前,讓你的KV規則不適用於創建小部件的情況下,你會看到什麼。

sm =等移入構建方法來解決此問題,在運行此方法之前加載kv文件。

1

這些變化中的代碼使工作:

sm = ScreenManager() 

class Test(App): 
    def build(self): 
     sm.add_widget(LoginPage(name="login")) 
     sm.add_widget(SignUp(name="signup")) 
     return sm 

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