如何从另一个类/屏幕 kivy 访问值

Posted

技术标签:

【中文标题】如何从另一个类/屏幕 kivy 访问值【英文标题】:How to access the value from another class/screen kivy 【发布时间】:2018-03-14 14:20:42 【问题描述】:

我想将弹出窗口中的标签设置为我已经在另一个屏幕/类中声明的值。我该怎么做?

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

【问题讨论】:

【参考方案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()

【讨论】:

【参考方案2】:

用你的方法做作业更容易,Winning。详情请参考下面的sn-p和示例。

片段

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 - 井字游戏

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

【讨论】:

感谢您的帮助,但是这种方法将弹出窗口置于屏幕2的背景中,并且在传递结果时仍然给我一个错误。我发现在弹出窗口中,如果我这样做,x = ScreenTwo()然后结果 = x.result,理论上它应该通过它,但是整个事情崩溃了,也没有任何好的错误,整个程序崩溃:进程完成退出代码-1073741819(0xC0000005)。我的完整代码是:pastebin.com/mFqa5TkP,kv 文件是:pastebin.com/1xBx4ksY 我已经更新了你的 Python 脚本和 Kivy 文件。我还将弹出窗口大小设置为更小。详情请参考“示例2”。 太棒了,谢谢。这对弹出窗口很有用,我可以看到我对 wins() 的引用太多了,这把事情搞砸了。我可以看到您如何将结果传递给弹出窗口,这很棒。我的重置按钮现在不起作用,我需要从 screentwo 类中引用重置方法。但我会努力的。谢谢你的帮助

以上是关于如何从另一个类/屏幕 kivy 访问值的主要内容,如果未能解决你的问题,请参考以下文章

您如何使用 Kivy GUI 访问其他类方法和函数?

Kivy:从另一个类的小部件中检索文本?

Kivy - 屏幕管理器 - 访问其他类中的属性

Kivy - 在其他屏幕中创建的访问实例

Kivy:无法从另一个类更新文本输入值

在 Kivy 中,我如何从另一个以 kv 语言显示的另一个屏幕获取变量