python 3.x - How to get/pass current ScreenManager to button callback function? (no kv file in use) -


The question is simple I have SAP variables in the form of a screen manager, how do I get it, and the screen information in the button callback?

Below is the issue part of my code, I have commented on the section that I have to solve:

  def build (self): def callback_home (dt): # I will work as to how current sreenmanager received such # things sm.switch_to down here (Screenl, direction = "left") app = FloatLayout () # Ui top anchor_top = AnchorLayout (anchor_x = 'center', anchor_y = ' top ') app.add_widget (anchor_top) box_top = BoxLayout (orientation =' horizontal ', size_hint = (1.0, 0.1)) anchor_top.add_widget (box_top) btn_home = button (text =' Home ') btn_home. Tie (on_press = callback_home) #Callback line here btn_add = button (text = add '') btn_upload = button (text = "upload") box_top.add_widget (btn_home) box_top.add_widget (btn_add) box_top.add_widget (btn_upload) # Ui down anchor_bottom = AnchorLayout (anchor_x = 'center', anchor_y = 'bottom') app.add_widget (anchor_bottom) SM = ScreenManager (size_hint = (1.0, 0.9)) anchor_bottom.add_widget (SM) Screenl = screen (name = "Home") screen Lkadd_widget (L Abel (text = "Home")) Smkadd_widget (Screenl) Screen2 = screen (name = add '') Screen2kadd_widget (Add labels (text = '')) sm. Add_widget (screen2) SCREEN3 = screen (name = 'upload') screen3.add_widget (label (text = 'upload')) sm .add_widget (screen3) return app    

screenmanager After defining your callback function. But if you use KV language then it is 10 times easier.

In addition - ScreenManager. Switch_to () works only with the new screen s to switch to screen By using this method, which has already been added to a ScreenManager , it will throw the exception .

Finally, if you FloatLayout and AnchorLayout s instead of let BoxLayout Use your layout is easier Could.

  BoxLayout: Orientation: 'Vertical' BoxLayout: size_hint_y: 0.1 Button: Text: 'Home' on_press: sm.current = 'Home' button: Text: 'Add' on_press: sm.current = 'Add' ScreenManager: ID: SM Screen: Name: 'Home' label: Text: 'Home' screen: Name: 'Add' label: Text: 'Add'    

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -