带有textvariable的Python Kivy标签

时间:2018-09-15 12:51:58

标签: python variables label kivy kivy-language

我是使用kivy进行编程的新手,我的代码可能需要一些帮助。

我想做的是运行函数draw_card(),并在按下screen2中的第二个按钮时,将Screen2中的标签文本设置为与draw_card中提到的变量some​​_var相等。

我有3个文件可以这样做。使用我的python代码的main.py,使用我的kivy代码的screens.kv和使用我的函数和draw_a_card()的字典的appvariables.py。

是否还有一种方法来使用常规变量代替appvariables.py中的card_drawn_dict来输出我提取的卡?

非常感谢你们。

main.py:

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, WipeTransition
from kivy.properties import ObjectProperty, StringProperty
from kivy.uix.floatlayout import FloatLayout
from AppVariables import *



class ScreenOne(Screen):
    pass

class ScreenTwo(Screen):
    pass

class ScreenThree(Screen):
    pass

class Manager(ScreenManager):

    screen_one = ObjectProperty(None)
    screen_two = ObjectProperty(None)
    screen_three = ObjectProperty(None)


class ScreensApp(App):

    def build(self):
        m = Manager(transition=WipeTransition())
        return m


    def draw_card(self, *args):
        draw_a_card(self, *args)
        some_var = card_drawn_dict[1]
        print(some_var)
        screen_one.mylabel.text = some_var


if __name__ == "__main__":
    ScreensApp().run()

screens.kv:

#:kivy 1.8.0

<Manager>:
    id: screen_manager

    screen_one: screen_one
    screen_two: screen_two
    screen_three: screen_three

    ScreenOne:
        id:screen_one
        name: "Screen1"
        manager: screen_manager


    ScreenTwo:
        id: screen_two
        name: "Screen2"
        manager: screen_manager


    ScreenThree:
        id: screen_three
        name: "Screen3"
        manager: screen_manager

<ScreenOne>:
    Button:     
        text: "Start"
        on_press: root.manager.current = 'Screen2'
        size_hint: 0.3, 0.2
        pos_hint: {"x":0.35, "y":0.2}

<ScreenTwo>:
    Button:
        text: "Screen 3"
        on_press: root.manager.current = 'Screen3'
        size_hint: 0.3, 0.2
        pos_hint: {"x":0.35, "y":0.1}

    Button:
        text: "Karte ziehen"
        on_press: app.draw_card()
        size_hint: 0.3, 0.2
        pos_hint: {"x":0.35, "y":0.3}

    Label:
        id: mylabel
        text: ""
        size_hint: 0.3, 0.2
        pos_hint: {"x":0.35, "y":0.6}

<ScreenThree>:
    Button:
        text: "Screen 1"
        on_press: root.manager.current = 'Screen1'

appvariables.py:

# Module: AppVariables.py
from random import randint

# Global variables
card_dict = {1:'Ace of Hearts', 2:'King of Hearts',
             3:'Ace of Diamonds', 4:'King of Diamonds',
             5:'Ace of Clubs', 6:'King of Clubs',
             7:'Ace of Spades', 8:'King of Spades'}
card_drawn_dict = {}

# Functions
def draw_a_card(self, *args):
        random_number = randint(1, 8)
        if random_number in card_dict:    
                card_drawn = card_dict[random_number]
                card_drawn_dict.update({1:card_drawn})
                #print(card_drawn)
                del card_dict[random_number]
        else:
                while not (random_number in card_dict):
                        if random_number == 1:
                                random_number = 8
                        else:
                                random_number -= 1 
                card_drawn = card_dict[random_number]
                card_drawn_dict.update({1:card_drawn})
                del card_dict[random_number]

1 个答案:

答案 0 :(得分:0)

填充Screen_Two的标签

使用<div class="rect"></div> <div class="line"></div>

使用变量

一副纸牌有52张扑克牌,4套西装(黑桃,心,钻石,球杆),每套西装有13个等级。

示例

AppVariables.py

self.root.screen_two.ids.mylabel.text = some_var

main.py

from random import randint

# Global variables
cardSuits = ['Spades', 'Hearts', 'Diamonds', 'Clubs']
courtCards = ['Jack', 'Queen', 'King']
cardRanks = ['Ace'] + ['{}'.format(x) for x in range(2, 11)] + courtCards
cardsDrawn = []


# Functions
def draw_a_card(self, *args):

    # a deck of card has 52 playing cards
    if len(cardsDrawn) >= 52:
        print("\tcards_drawn=", sorted(cardsDrawn))
        return 'Drawn 52 cards!'

    # Example set of 52 playing cards; 13 of each suit clubs, diamonds, hearts, and spades
    suit = randint(1, 4)
    rank = randint(1, 13)
    card = '{0}-{1:02d}'.format(suit, rank)

    if card not in cardsDrawn:
        cardsDrawn.append(card)
        return '{0} of {1}'.format(cardRanks[rank-1], cardSuits[suit-1])
    else:
        # recursive call
        return draw_a_card(self, args)

screens.kv

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from AppVariables import *


class ScreenOne(Screen):
    pass


class ScreenTwo(Screen):
    pass


class ScreenThree(Screen):
    pass


class Manager(ScreenManager):
    screen_one = ObjectProperty(None)
    screen_two = ObjectProperty(None)
    screen_three = ObjectProperty(None)


class ScreensApp(App):

    def build(self):
        return Manager()

    def draw_card(self, *args):
        some_var = draw_a_card(self, args)
        print(some_var)
        self.root.screen_two.ids.mylabel.text = some_var


if __name__ == "__main__":
    ScreensApp().run()

输出

Img01 Img02 - 52 cards drawn