Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python - Kivy에서 버튼 작업

<시간/>

Kivy는 Python의 플랫폼 독립적 GUI 도구입니다. Android, IOS, Linux 및 Windows 등에서 실행할 수 있으므로 Kivy는 코드를 한 번만 작성하고 다른 플랫폼에서 실행할 수 있는 기능을 제공합니다. 기본적으로 Android 애플리케이션을 개발하는 데 사용되지만 Desktop 애플리케이션에서 사용할 수 없다는 의미는 아닙니다.

버튼은 버튼을 눌렀을 때(또는 클릭/터치 후 놓을 때) 트리거되는 관련 작업이 있는 레이블입니다. 버튼 뒤에 기능을 추가하고 버튼의 스타일을 지정할 수 있습니다.

예시

# import kivy module
import kivy  
# this restrict the kivy version below this kivy version you cannot
# use the app or software
kivy.require("1.9.1")  
# base Class of your App inherits from the App class.
# app:always refers to the instance of your application
from kivy.app import App  
# creates the button in kivy if not imported shows the error
from kivy.uix.button import Button  
# class in which we are creating the button
class ButtonApp(App):      
   def build(self):
      # use a (r, g, b, a) tuple
      btn = Button(text ="Push Me !",
         font_size ="20sp",
         background_color =(1, 1, 1, 1),
         color =(1, 1, 1, 1),
         size =(32, 32),
         size_hint =(.2, .2),
         pos =(300, 250))
      # bind() use to bind the button to function callback
      btn.bind(on_press = self.callback)
      return btn
   # callback function tells when button pressed
   def callback(self, event):
      print("button pressed")
      print('Kivy!')  
# creating the object root for ButtonApp() class
root = ButtonApp()  
#run function runs the whole program. run() method which calls the #target function passed to the constructor.
root.run()