0

我想将弹出窗口中的标签设置为我已经在另一个屏幕/类中声明的值。我怎样才能做到这一点?

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 中存储的值?

4

2 回答 2

1

这不是正确的 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()
于 2017-10-03T13:15:59.773 回答
0

用你的方法做作业更容易,Winning。有关详细信息,请参阅下面的代码段和示例。

片段

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

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

例子

主文件

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

测试.kv

#:kivy 1.10.0

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

<MyScreenManager>:
    ScreenTwo:
        name: "screen2"


<ScreenTwo>:

输出

在此处输入图像描述

示例 2 - 井字游戏

nanx.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()

nanx.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

在此处输入图像描述

于 2017-10-03T21:50:32.683 回答