2014-10-30 3 views
8

파이썬을 사용하여 Kivy 언어로 만든 레이블의 텍스트를 어떻게 바꿀 수 있었는지 궁금합니다. 어떻게하면 파이썬에서 사용자 입력을 kivy의 레이블 텍스트로 만들 수 있을까요? (그런데 나는 실제 프로그램에서 올바른 형식을 가지고 있지만 stackoverflow에 붙여 넣기를 망쳤다.) 코드에서 레이블의 텍스트를 파이썬에서 생성 된 임의의 숫자로 만들고 싶다면 어떻게 할 것인가? 그 일에 대해서? 라벨의파이썬을 사용하여 kivy 언어의 레이블 텍스트를 변경하는 방법

from kivy.app import App 
from kivy.lang import Builder 
from kivy.uix.screenmanager import ScreenManager, Screen 
from kivy.graphics import Color, Rectangle 
from kivy.properties import ObjectProperty 

# Create both screens. Please note the root.manager.current: this is how 
# you can control the ScreenManager from kv. Each screen has by default a 
# property manager that gives you the instance of the ScreenManager used. 


Builder.load_string(""" 
<[email protected]+AsyncImage> 
<[email protected]+AsyncImage> 
<MenuScreen>: 
GridLayout: 
    cols: 4 
    row_force_default: True 
    col_default_width: 175 
    row_default_height: 150 
    padding: 15 
    spacing: 15 
    canvas.before: 
     BorderImage: 
      # BorderImage behaves like the CSS BorderImage 
      border: 10, 10, 10, 10 
      source: '/Users/clayhigh/Desktop/kivy/aot.png' 
      pos: self.pos 
      size: self.size 
    Button: 
     text: 'Goto settings' 
     background_color: 1,0,0,0.5 
     on_press: root.manager.current = 'settings' 
    ButImage: 
     on_press: root.manager.current = 'UBW' 
     id: but 
     size_hint: .5, .5 
     opacity: 1 if self.state == 'normal' else .5 
     allow_stretch: True 
     keep_ratio: False 
     source: 'http://s3.amazonaws.com/rapgenius/1361742626_beautiful-ocean-beautiful-pictures-27115524-1440-900.jpg' 
     Label: 
      center: but.center 
      text: "UBW" 
      color: 0.78,0.145,0.016,2 
    ButImage: 
     id: lh 
     size_hint: .5, .5 
     opacity: 1 if self.state == 'normal' else .5 
     allow_stretch: True 
     keep_ratio: False 
     source: 'http://s3.amazonaws.com/rapgenius/1361742626_beautiful-ocean-beautiful-pictures-27115524-1440-900.jpg' 
     Label: 
      center: lh.center 
      text: "LH 2" 
      color: 0,0,0,1 
    ButImage: 
     id: ttl 
     size_hint: .5, .5 
     opacity: 1 if self.state == 'normal' else .5 
     allow_stretch: True 
     keep_ratio: False 
     source: 'http://s3.amazonaws.com/rapgenius/1361742626_beautiful-ocean-beautiful-pictures-27115524-1440-900.jpg' 
     Label: 
      center: ttl.center 
      text: "TwTl" 
      color: 0,0,0,1 
    ButImage: 
     id: gris 
     size_hint: .5, .5 
     opacity: 1 if self.state == 'normal' else .5 
     allow_stretch: True 
     keep_ratio: False 
     source: 'http://s3.amazonaws.com/rapgenius/1361742626_beautiful-ocean-beautiful-pictures-27115524-1440-900.jpg' 
     Label: 
      center: gris.center 
      text: "Gris" 
      color: 0,0,0,1 
    ButImage: 
     id: shig 
     size_hint: .5, .5 
     opacity: 1 if self.state == 'normal' else .5 
     allow_stretch: True 
     keep_ratio: False 
     source: 'http://s3.amazonaws.com/rapgenius/1361742626_beautiful-ocean-beautiful-pictures-27115524-1440-900.jpg' 
     Label: 
      center: shig.center 
      text: "Shig" 
      color: 0,0,0,1 
    Button: 
     text: 'Test3' 
     background_color: 1,0,0,0.5 
    Button: 
     text: 'Test4' 
     background_color: 1,0,0,0.5 
    Button: 
     text: 'Quit' 
     background_color: 1,0,0,0.5 
     on_press: App.on_stop 

<SettingsScreen>: 
GridLayout: 
    row_force_default: True 
    row_default_height: 100 
    cols: 2 
    canvas.before: 
     BorderImage: 
      # BorderImage behaves like the CSS BorderImage 
      border: 10, 10, 10, 10 
      source: '/Users/clayhigh/Desktop/kivy/ato.jpeg' 
      pos: self.pos 
      size: self.size 
    Button: 
     text: 'Button' 
     color: 0,0,.5 
     background_color: 1,0,0,1 
    Button: 
     text: 'Back to menu' 
     background_color: 1,0,0,1 
     on_press: root.manager.current = 'menu' 
<UBW>: 
GridLayout: 
    row_force_default: True 
    row_default_height: 100 
    cols: 2 
    canvas.before: 
     Color: 
      rgb: .5, .5, .5 
     Rectangle: 
      pos: self.pos 
      size: self.size 
     Color: 
      rgb: 1, 1, 1 
     BorderImage: 
      # BorderImage behaves like the CSS BorderImage 
      border: 10, 10, 10, 10 
      source: '/Users/clayhigh/Desktop/kivy/fsn.jpg' 
      pos: self.pos 
      size: self.size 
    Button: 
     text: 'Back to menu' 
     color: 0,0,.5 
     on_press: root.manager.current = 'menu' 
     background_color: 1,0,0,1 
    Label: 
     id: AName 
     text: "F S/N: UBW" 
     font_size: '24sp' 
""") 

# Declare both screens 


class MenuScreen(Screen): 
pass 


class SettingsScreen(Screen): 
pass 


class UBW(Screen): 
pass 

# Create the screen manager 
sm = ScreenManager() 
sm.add_widget(MenuScreen(name='menu')) 
sm.add_widget(SettingsScreen(name='settings')) 
sm.add_widget(UBW(name='UBW')) 


class TestApp(App): 

def build(self): 
    return sm 

if __name__ == '__main__': 
TestApp().run() 

답변

6

텍스트는 나중에 변경할 수있는 kivy 재산이 될 수 있으며이 kivy 특성 때문에 자동으로 모든 곳에서 업데이트됩니다. 여기에 귀하의 평

from kivy.app import App 
from kivy.uix.widget import Widget 
from kivy.properties import StringProperty 
import random 

class YourWidget(Widget): 
    random_number = StringProperty() 

    def __init__(self, **kwargs): 
     super(YourWidget, self).__init__(**kwargs) 
     self.random_number = str(random.randint(1, 100)) 

    def change_text(self): 
     self.random_number = str(random.randint(1, 100)) 

class YourApp(App): 
    def build(self): 
     return YourWidget() 

if __name__ == '__main__': 
    YourApp().run() 

을하고 .kv의 예 버튼을 클릭하면

<YourWidget>: 
    BoxLayout: 
     size: root.size 
     Button: 
      id: button1 
      text: "Change text" 
      on_release: root.change_text() 
     Label: 
      id: label1 
      text: root.random_number 

, 그것은 무작위 임의의 정수에 레이블의 텍스트를 변경합니다 change_text() 함수를 호출 할 것이다 1 ~ 100 사이입니다.

+1

응답을 보내 주셔서 감사합니다. kivy 언어 외부에서 함수를 호출 할 수 있습니까? 레이블의 텍스트를 목록의 문자열로 변경하려면 버튼을 눌러 함수를 호출하지 않아도됩니까? 감사합니다. – user2896775

+1

예, 평상시처럼'YourClass' 줄'self.change_text()'에있을 수 있습니다. 함수를 호출 할 필요는 없으며, 단지 (새로운) 문자열을 (이 경우)'random_number'에 할당하는 것만 할 수 있습니다. – Tatarkow

관련 문제