2014-10-30 2 views
8

के साथ किवी भाषा में किसी लेबल के टेक्स्ट को कैसे बदलें, मैं सोच रहा था कि मैं पाइथन का उपयोग करके किवी भाषा के अंदर किए गए लेबल के टेक्स्ट को कैसे बदल सकता हूं। जैसा कि मेरे पास पाइथन से उपयोगकर्ता इनपुट कोवी में लेबल के टेक्स्ट के रूप में बनाया जाएगा। (जिस तरह से मेरे पास वास्तविक प्रोग्राम में स्वरूपण सही है, लेकिन मैंने इसे स्टैक ओवरफ्लो पर चिपकाया है) कहें कि क्या मैं कोड से लेबल का टेक्स्ट बनाना चाहता हूं, पाइथन में उत्पन्न एक यादृच्छिक संख्या हो ऐसा करने के बारे में? एक लेबल केपाइथन

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 संपत्ति है यह स्वचालित रूप से हर जगह अपडेट होगा। यहाँ अपने .py

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

प्रतिक्रिया के लिए धन्यवाद, हालांकि यह किवी भाषा के बाहर फ़ंक्शन को कॉल करना संभव है? मान लें कि मैं चाहता हूं कि लेबल का टेक्स्ट किसी सूची से स्ट्रिंग में बदला जाए, क्या मैं फ़ंक्शन को कॉल करने के लिए बटन दबाए बिना ऐसा कर सकता हूं? धन्यवाद – user2896775

+1

हां, आप सामान्य रूप से 'YourClass' पंक्ति 'self.change_text()' में कहीं भी हो सकते हैं। आपको किसी भी फ़ंक्शन को कॉल करने की आवश्यकता नहीं है, केवल एक चीज जो आपको करने की ज़रूरत है वह है (इस मामले में) 'random_number') को एक नई स्ट्रिंग असाइन करना। – Tatarkow