Как получить доступ к значению из другого класса / экрана кивы

Я хочу установить метку во всплывающем окне на значение, которое я уже объявил в другом экране / классе. Как я могу это сделать?

class ScreenTwo(Screen):
    self.result = StringProperty(None)

    def Winning(self):
        wp = WinningPopup()
        wp.open()


class WinningPopup(Popup):
    pass

это часть основного файла, которая показывает два класса, один экран, всплывающее окно.

<WinningPopup>:
    id: winning_popup
        Label:
            id: winning_label
            text: root.parent.ScreenTwo.checkwin.result

это из файла kv для всплывающего окна, пытающегося указать значение, которое содержится в screentwo для текста метки, я пробовал root.self.ScreenTwo, пробовал root.checkwin.result, пробовал все их комбинации, но он просто дает ошибка, что он не может найти результат. Как я могу связать текст в метке со значением, хранящимся в screentwo?

2 ответа

Это неверный синтаксис Python, а также неправильное соглашение об именах Python. Прежде всего self.result = StringProperty(None) не имеет смысла. Просто result = StringProperty(None), Также имя функции должно быть в нижнем регистре.

from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.properties import StringProperty
from kivy.uix.popup import Popup
from kivy.lang import Builder


layout = """
<WinningPopup>:
    id: winning_popup
    Label:
        id: winning_label
        text: root.result
"""


class ScreenTwo(Screen):
    result = StringProperty('')

    def __init__(self):
        super(ScreenTwo, self).__init__()
        self.add_widget(WinningPopup(self.result))


class WinningPopup(Popup):
    result = StringProperty('')

    def __init__(self, result):
        super(WinningPopup, self).__init__()
        self.result = result


class MyApp(App):
    def build(self):
        Builder.load_string(layout)
        s_m = ScreenManager()
        s_m.add_widget(ScreenTwo())
        return s_m

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

Проще сделать задание в вашем методе Winning. Пожалуйста, обратитесь к фрагменту и примеру ниже для деталей.

отрывок

class ScreenTwo(Screen):
    result = StringProperty("Testing-1-2-3")

    def Winning(self):
        wp = WinningPopup()
        wp.ids.winning_label.text = self.result
        wp.open()

пример

main.py

from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.popup import Popup
from kivy.properties import StringProperty


class WinningPopup(Popup):
    pass


class MyScreenManager(ScreenManager):

    def __init__(self, **kwargs):
        super(MyScreenManager, self).__init__(**kwargs)
        self.current = "screen2"    # display ScreenTwo


class ScreenTwo(Screen):
    result = StringProperty("Testing-1-2-3")

    def __init__(self, **kwargs):
        super(ScreenTwo, self).__init__(**kwargs)
        self.Winning()  # display popup

    def Winning(self):
        wp = WinningPopup()
        wp.ids.winning_label.text = self.result
        wp.open()


class TestApp(App):

    def build(self):
        return MyScreenManager()


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

test.kv

#:kivy 1.10.0

<WinningPopup>:
    id: winning_popup
    Label:
        id: winning_label

<MyScreenManager>:
    ScreenTwo:
        name: "screen2"


<ScreenTwo>:

Выход

Пример 2 - TicTacToe

nandx.py

from kivy.app import App
from kivy.uix.popup import Popup
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.properties import StringProperty
from kivy.config import Config
import random
import sys

Config.set("graphics", "width", "420")
Config.set("graphics", "height", "600")
Config.set("graphics", "borderless", "0")
Config.set("graphics", "resizable", "0")
Config.set("kivy", "window_icon", "nyc.ico")


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


class ScreenOne(Screen):
    pass


class ScreenTwo(Screen):
    playericon = ""
    compicon = ""
    array_of_buttons = [11, 12, 13, 21, 22, 23, 31, 32, 33]
    array_of_lines = [[11, 12, 13], [11, 21, 31], [12, 22, 32], [13, 23, 33], [21, 22, 23], [31, 32, 33], [11, 22, 33],
                      [13, 22, 31]]
    array_of_moves_player = []
    array_of_moves_comp = []
    result = StringProperty("")

    def choosexoro(self, pressedchoice):
        self.ids.choose_o.disabled = True
        self.ids.choose_x.disabled = True
        if pressedchoice == "x":
            self.playericon = "x.png"
            self.compicon = "circle.png"
            x = 0
            while x < 9:
                butt = self.array_of_buttons[x]
                butt = str(butt)
                self.ids[butt].disabled = False
                x += 1
        else:
            self.playericon = "circle.png"
            self.compicon = "x.png"
            x = 0
            while x < 9:
                butt = self.array_of_buttons[x]
                butt = str(butt)
                self.ids[butt].disabled = False
                x += 1
        return self.playericon, self.compicon

    def movemade(self, pressed, pressed_for_array):
        self.array_of_buttons.remove(pressed_for_array)
        self.array_of_moves_player.append(pressed_for_array)
        pressed.disabled = True
        pressed.background_disabled_normal = (self.playericon)
        if self.array_of_buttons == []:
            self.checkwin()
        else:
            self.checkwin()
            self.computersturn()

    def computersturn(self):
        lengthof = int(len(self.array_of_buttons))
        compgo = random.randint(0, (lengthof - 1))
        compchoice = (self.array_of_buttons.__getitem__(compgo))
        self.array_of_moves_comp.append(compchoice)
        compchoice_str = str(compchoice)
        del self.array_of_buttons[compgo]
        self.ids[compchoice_str].disabled = True
        self.ids[compchoice_str].background_disabled_normal = (self.compicon)
        self.checkwin()

    def checkwin(self):
        result = StringProperty("")
        resultlist = 0
        x = 0
        while x < 8:
            if set(self.array_of_lines[x]) <= set(self.array_of_moves_player):
                resultlist = 1
                # self.Winning()
                break
            else:
                x += 1
        x = 0
        while x < 8:
            if set(self.array_of_lines[x]) <= set(self.array_of_moves_comp):
                resultlist = 2
                # self.Winning()
                break
            else:
                x += 1
        if resultlist == 0 and self.array_of_buttons == []:
            resultlist = 3
            # self.Winning()
        if resultlist == 1:
            result = "You Win!"
            self.Winning(result)
        elif resultlist == 2:
            result = "The Computer Wins!"
            self.Winning(result)
        elif resultlist == 3:
            result = "It´s a Draw!"
            self.Winning(result)

        return self.result

    def Winning(self, result):
        wp = WinningPopup(size_hint=(None, None), size=(400, 400))
        wp.ids.winning_label.text = result
        wp.open()

    def resetgame(self):
        self.array_of_moves_player = []
        self.array_of_moves_comp = []
        self.array_of_buttons = [11, 12, 13, 21, 22, 23, 31, 32, 33]
        x = 0
        while x <= 8:
            butt = self.array_of_buttons[x]
            butt = str(butt)
            self.ids[butt].background_disabled_normal = ("blank.png")
            butt = int(butt)
            x += 1

        self.ids.choose_x.disabled = False
        self.ids.choose_o.disabled = False
        self.ids.top_player.text = "Player 1"
        self.ids.bottom_player.text = "Player 2"


class WinningPopup(Popup):

    def quitgame(self):
        sys.exit(0)


class nandxApp(App, ScreenTwo):
    def build(self):
        self.title = "Noughts and Crosses"
        return Manager()


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

nandx.kv

#:kivy 1.0
#:import hex kivy.utils.rgba

<Manager>:

    ScreenOne:
        id: screen_one

    ScreenTwo:
        id: screen_two


<ScreenOne>:

    id: screen_one
    name: "screen1"
    GridLayout:
        size_hint: (.5, .5)
        pos_hint: {"center_x":0.5,"center_y":0.6}
        rows: 3
        padding: 20

        Label:
            size_hint: (.2, .2)
            text: "Please enter\nyour name:"
            font_size: 30
            halign: 'center'
            valign: 'middle'

        TextInput:
            size_hint: (.2, .06)
            cursor_blink: True
            font_size: 20
            multiline: 0
            id: player_name

        Button:
            size_hint: (.2, .08)
            text: "Continue"
            on_release:
                root.manager.current = "screen2"
                root.manager.ids.screen_two.ids.final_playername.text=player_name.text

<ScreenTwo>:
    id: screen_two
    name: "screen2"
    GridLayout:
        cols: 3
        size: root.size
        spacing: 10
        padding: 10

        Button:
            id: 11
            disabled: True
            background_normal: ("blank.png")
            background_disabled_normal: ("blank.png")
            on_release:
                root.movemade(self, 11)

        Button:
            id: 12
            disabled: True
            background_normal: ("blank.png")
            background_disabled_normal: ("blank.png")
            on_release:
                root.movemade(self, 12)

        Button:
            id: 13
            disabled: True
            background_normal: ("blank.png")
            background_disabled_normal: ("blank.png")
            on_release:
                root.movemade(self, 13)

        Button:
            id: 21
            disabled: True
            background_normal: ("blank.png")
            background_disabled_normal: ("blank.png")
            on_release:
                root.movemade(self, 21)

        Button:
            id: 22
            disabled: True
            background_normal: ("blank.png")
            background_disabled_normal: ("blank.png")
            on_release:
                root.movemade(self, 22)

        Button:
            id: 23
            disabled: True
            background_normal: ("blank.png")
            background_disabled_normal: ("blank.png")
            on_release:
                root.movemade(self, 23)

        Button:
            id: 31
            disabled: True
            background_normal: ("blank.png")
            background_disabled_normal: ("blank.png")
            on_release:
                root.movemade(self, 31)

        Button:
            id: 32
            disabled: True
            background_normal: ("blank.png")
            background_disabled_normal: ("blank.png")
            on_release:
                root.movemade(self, 32)

        Button:
            id: 33
            disabled: True
            background_normal: ("blank.png")
            background_disabled_normal: ("blank.png")
            on_release:
                root.movemade(self, 33)

        Button:
            id: choose_x
            background_normal: ("x.png")
            background_disabled_normal: ("x.png")
            on_release:
                root.choosexoro("x")
                root.ids.top_player.text=("<--"+(root.ids.final_playername.text))
                root.ids.bottom_player.text=("Computer -->")

        GridLayout:
            rows: 2

            Label:
                id: top_player
                font_size: 20
                bold: True
                color: (.67,.3,.95,1)
                text: "Player 1"

            Label:
                id: bottom_player
                font_size: 20
                bold: True
                color: (.67,.3,.95,1)
                text: "Player 2"

        Button:
            id: choose_o
            background_normal: ("circle.png")
            background_disabled_normal: ("circle.png")
            on_release:
                root.choosexoro("o")
                root.ids.bottom_player.text=("<--"+(root.ids.final_playername.text))
                root.ids.top_player.text=("Computer -->")

        GridLayout:
            rows: 3

            Label:
                id: final_playername
                font_size: 20
                bold: True
                color: (0,1,0,1)
                text: ""

            Label:
                font_size: 20
                bold: True
                color: (1,0,0,1)
                text: "Score:"

            Label:
                id: userscore
                font_size: 20
                bold: True
                color: (1,0,0,1)
                text: "0"

        Label:
            text: ""

        GridLayout:
            rows: 3

            Label:
                font_size: 20
                bold: True
                color: (0,1,0,1)
                text: "Computer"

            Label:
                font_size: 20
                bold: True
                color: (1,0,0,1)
                text: "Score:"

            Label:
                id: computerscore
                font_size: 20
                bold: True
                color: (1,0,0,1)
                text: "0"


<WinningPopup>:
    id: winning_popup
    name: "WinningPopup"
    title: "Winner!"

    GridLayout:
        size: root.size
        spacing: 30
        padding: 30
        cols: 1

        Label:
            id: winning_label
            font_size: 30
            bold: True
            color: (.5,.5,.5,1)
            text: "Unknown"     #root.result

        Button:
            text: "Play Again"
            on_release:
                root.resetgame()
                root.dismiss()

        Button:
            text: "Quit"
            on_release: root.quitgame()

Выход - Пример 2

Другие вопросы по тегам