Sant 的kivy視頻筆記 3 - 图1
© Karobben

由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

Introduction of kivy:
Youtube Vedio
Text Tutorial

As you can see, I skipped lesson 2 which is introducing how to add and join a button.
Besides, he showed us how to get the input texts from the TextInput

In this video, Sant introduced the module of screenmanager from kivy.uix.

Main skeleton of the codes:

  1. import kivy
  2. from kivy.app import App
  3. from kivy.uix.label import Label
  4. from kivy.uix.gridlayout import GridLayout
  5. ## to use buttons:
  6. from kivy.uix.button import Button
  7. ## to screen
  8. from kivy.uix.screenmanager import ScreenManager, Screen
  9. kivy.require("1.10.1")
  10. class EpicApp(App):
  11. class ConnectPage(GridLayout):
  12. class InfoPage(GridLayout):
  13. if __name__ == "__main__":
  14. chat_app = EpicApp()
  15. chat_app.run()

EpicApp()

As you can see, there are three classes. In the first class, EpicApp, is the class which manager different screens.

  1. class EpicApp(App):
  2. def build(self):
  3. # We are going to use screen manager, so we can add multiple screens
  4. # and switch between them
  5. self.screen_manager = ScreenManager()
  6. # Initial, connection screen (we use passed in name to activate screen)
  7. # First create a page, then a new screen, add page to screen and screen to screen manager
  8. self.connect_page = ConnectPage()
  9. screen = Screen(name='Connect')
  10. screen.add_widget(self.connect_page)
  11. self.screen_manager.add_widget(screen)
  12. # Info page
  13. # Info page was below
  14. self.info_page = InfoPage()
  15. screen = Screen(name='Info')
  16. screen.add_widget(self.info_page)
  17. self.screen_manager.add_widget(screen)
  18. return self.screen_manager

After assigned the screen_manger, we need to assign the “name” and “widget” on each screen; and then, add them into screen_manager

As we can see, it loaded the ConnectPage first, and named it as ‘Connect’ which was used to switch the screen later.

Who loaded first would be the Home Page.

ConnectPage

  1. class ConnectPage(GridLayout):
  2. # runs on initialization
  3. # Our Main Screen
  4. def __init__(self, **kwargs):
  5. super().__init__(**kwargs)
  6. self.cols = 1 # used for our grid
  7. self.add_widget(Label(text='Hello World')) # widget #1, top left
  8. #self.add_widget(self.ip) # widget #2, top right
  9. # add our button.
  10. self.join = Button(text="Try me")
  11. self.join.bind(on_press=self.join_button)
  12. self.add_widget(Label()) # just take up the spot.
  13. self.add_widget(self.join)
  14. def join_button(self, instance):
  15. #print(f"Joining {ip}:{port} as {username}")
  16. # Create info string, update InfoPage with a message and show it
  17. info = f"You found me"
  18. chat_app.info_page.update_info(info)
  19. chat_app.screen_manager.current = 'Info'

The layout of this page was clearly explained by Sant. And for practicing, I simplified it and remained a line of text and button only. The button is the key to join another screen. The trick of it is create a brand new screen.

InfoPage

  1. class InfoPage(GridLayout):
  2. def __init__(self, **kwargs):
  3. super().__init__(**kwargs)
  4. # Just one column
  5. self.cols = 1
  6. # And one label with bigger font and centered text
  7. self.message = Label(halign="center", valign="middle", font_size=30)
  8. # By default every widget returns it's side as [100, 100], it gets finally resized,
  9. # but we have to listen for size change to get a new one
  10. # more: https://github.com/kivy/kivy/issues/1044
  11. self.message.bind(width=self.update_text_width)
  12. # Add text widget to the layout
  13. self.add_widget(self.message)
  14. # add our button.
  15. # Add an return button
  16. self.join = Button(text="return")
  17. self.join.bind(on_press=self.join_button)
  18. self.add_widget(Label()) # just take up the spot.
  19. self.add_widget(self.join)
  20. def join_button(self, instance):
  21. #print(f"Joining {ip}:{port} as {username}")
  22. chat_app.screen_manager.current = 'Connect'
  23. # Called with a message, to update message text in widget
  24. def update_info(self, message):
  25. self.message.text = message
  26. # Called on label width update, so we can set text width properly - to 90% of label width
  27. def update_text_width(self, *_):
  28. self.message.text_size = (self.message.width * 0.9, None)

In this page, Sant add a function which could update the texts in the screen. So we can print the texts by running chat_app.info_page.update_info(info) before switch to the screen ‘Info’

And personally, I add a return button to back to the home page.
Sant 的kivy視頻筆記 3 - 图2

Click to see the full version of the codekivy.require(“1.10.1”)
class EpicApp(App):
def build(self):

We are going to use screen manager, so we can add multiple screens

and switch between them

self.screen_manager = ScreenManager()

Initial, connection screen (we use passed in name to activate screen)

First create a page, then a new screen, add page to screen and screen to screen manager

self.connect_page = ConnectPage()
screen = Screen(name=’Connect’)
screen.add_widget(self.connect_page)
self.screen_manager.add_widget(screen)

Info page

Info page was below

self.info_page = InfoPage()
screen = Screen(name=’Info’)
screen.add_widget(self.info_page)
self.screen_manager.add_widget(screen)
return self.screen_manager
class ConnectPage(GridLayout):

runs on initialization

Our Main Screen

def init(self, kwargs):
super().
init(kwargs)

  1. self.cols = 1 # used for our grid
  2. self.add_widget(Label(text='Hello World')) # widget #1, top left
  3. #self.add_widget(self.ip) # widget #2, top right
  4. # add our button.
  5. self.join = Button(text="Try me")
  6. self.join.bind(on_press=self.join_button)
  7. self.add_widget(Label()) # just take up the spot.
  8. self.add_widget(self.join)
  9. def join_button(self, instance):
  10. #print(f"Joining {ip}:{port} as {username}")
  11. # Create info string, update InfoPage with a message and show it
  12. info = f"You found me"
  13. chat_app.info_page.update_info(info)
  14. chat_app.screen_manager.current = 'Info'

Simple information/error page

class InfoPage(GridLayout):
def init(self, kwargs):
super().
init(kwargs)

  1. # Just one column
  2. self.cols = 1
  3. # And one label with bigger font and centered text
  4. self.message = Label(halign="center", valign="middle", font_size=30)
  5. # By default every widget returns it's side as [100, 100], it gets finally resized,
  6. # but we have to listen for size change to get a new one
  7. # more: https://github.com/kivy/kivy/issues/1044
  8. self.message.bind(width=self.update_text_width)
  9. # Add text widget to the layout
  10. self.add_widget(self.message)
  11. # add our button.
  12. # Add an return button
  13. self.join = Button(text="return")
  14. self.join.bind(on_press=self.join_button)
  15. self.add_widget(Label()) # just take up the spot.
  16. self.add_widget(self.join)
  17. def join_button(self, instance):
  18. #print(f"Joining {ip}:{port} as {username}")
  19. chat_app.screen_manager.current = 'Connect'
  20. # Called with a message, to update message text in widget
  21. def update_info(self, message):
  22. self.message.text = message
  23. # Called on label width update, so we can set text width properly - to 90% of label width
  24. def update_text_width(self, *_):
  25. self.message.text_size = (self.message.width * 0.9, None)

if name == “main“:
chat_app = EpicApp()
chat_app.run()

  1. <details>
  2. ---
  3. **Enjoy~**
  4. 本文由<span style='color:salmon'>Python腳本</span>[GitHub](https://karobben.github.io/2021/03/02/Python/yuqueAPI)/[語雀](https://www.yuque.com/liuwenkan/python/yuque_api)自動更新
  5. <span style='color:salmon'>由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~</span>
  6. [本文GitPage地址](https://karobben.github.io/2020/11/12/Python/Kivy_sent3)
  7. GitHub: [Karobben](https://github.com/Karobben)
  8. Blog:[Karobben](https://karobben.github.io/)
  9. BiliBili:[史上最不正經的生物狗](https://space.bilibili.com/393056819)