0

I want to set the label in a popup to a value which i have already declared in another screen/class. How can i do this?

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

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


class WinningPopup(Popup):
    pass

that is a part of the main file which shows the two classes, one a screen one a popup.

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

this is from the kv file for the popup trying to indicate the value which is held in screentwo for the label text, i´ve tried root.self.ScreenTwo, tried root.checkwin.result, tried all combinations of these but it just gives an error that it cannot find result. How can i link the text in the label to the value stored in screentwo?

Rayne
  • 119
  • 1
  • 2
  • 7

2 Answers2

1

This is not correct python syntax also not correct python naming convention. First of all self.result = StringProperty(None) doesn't make sense. Simply result = StringProperty(None). Also function name must be lowercase.

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()
Amin Etesamian
  • 3,363
  • 5
  • 27
  • 50
0

It is easier to do the assignment in your method, Winning. Please refer to the snippet and example below for details.

Snippet

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

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

Example

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>:

Output

enter image description here

Example 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()

Output - Example 2

enter image description here

ikolim
  • 15,721
  • 2
  • 19
  • 29
  • Thank you for the help, but this method puts the popup window in the background of screen 2 and still gives me an error when it comes time to pass result. i found that in the popup if i do, x = ScreenTwo() and then result = x.result, it should in theory pass it, but the whole thing crashes, not with any nice error either, total program crash: Process finished with exit code -1073741819 (0xC0000005). The full code i have is: https://pastebin.com/mFqa5TkP and the kv file is: https://pastebin.com/1xBx4ksY – Rayne Oct 04 '17 at 13:51
  • I have updated your Python script and Kivy file. I have also set the Popup window size to be smaller. Please refer to "Example 2" for details. – ikolim Oct 04 '17 at 17:41
  • fantastic, thank you. that works great for the popup, i can see i put way too many references to winning() which messed things up. i can see how you then passed the result to the popup, that's great. my reset button doesn't work now, i need to reference the reset method from the screentwo class. but i'll work on that. thank you for your help – Rayne Oct 05 '17 at 16:34