用弹出文本输入kivy更改按钮文本

时间:2017-09-25 01:27:42

标签: python-3.x kivy kivy-language

所以,我是kivy的新手,无法使用此代码。我试图用弹出文本输入更新按钮文本。弹出窗口显示按下按钮的时间,当它被解除时,它应该用按钮中的任何文本更新按钮上的文本。

我已尝试过此代码的许多变体,但没有人工作过。没有任何反应或者我收到此错误:

属性错误:'超级'对象没有属性' __ getattr __'

这是:

main.py

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.popup import Popup

class MainWidget(GridLayout):
    pass

class PopText(Popup):

    def textChange(self):
        MyButton().change()

    def getText(self):
        text = self.display
        return text

class MyButton(AnchorLayout):
    def change(self):
        self.ids.equip_bt.text = PopText().getText()
        print(self.ids.my_bt.text)

class MyApp(App):
    def build(self):
        return MainWidget()

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

my.kv

#:kivy 1.10.0
#:import F kivy.factory.Factory

<PopText>:
    size_hint: .7, .3
    title: "Input Text"
    on_dismiss: self.textChange()
    display: pop_text.text

    TextInput:
        id: pop_text
        focus: True
        multiline: False
        on_text_validate: root.dismiss()

<MyButton>:
    anchor_y: "top"
    anchor_x: "right"

    Button:
        id: my_bt
        text: "Input Text"
        on_release: F.PopText().open()

<MainWidget>:
    cols: 1
    rows: 2

    MyButton:
    MyButton:

关于如何解决这个问题的任何想法?

2 个答案:

答案 0 :(得分:0)

以下是您尝试实现的最低范例。困难的部分是连接从弹出按钮打开它的按钮。我正在通过app类来实现这一目标。我不得不承认这不是一个很好的解决方案。

from kivy.app import App
from kivy.uix.popup import Popup
from kivy.uix.button import Button

class PopupBttn(Button):
    def openPopup(self):
        Pop = TextPopup().open(self.text)

class TextPopup(Popup):
    def open(self, text, **kwargs):
        super(TextPopup, self).open(**kwargs)
        self.ids.txtipt.text = text 

class MyApp(App):
    pass

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

kv文件:

BoxLayout:
    PopupBttn:
        id: bttn
        text: 'open Popup'
        on_press: self.openPopup()

<TextPopup>:
    BoxLayout:
        orientation: "vertical"
        TextInput:
            id: txtipt
        Button:
            text: "OK"
            on_release: app.root.ids.bttn.text=root.ids.txtipt.text
            on_release: root.dismiss()

这是使用多个按钮的更新版本。不幸的是,您需要将id和name设置为每个Button的id字符串。

python文件

from kivy.app import App
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.properties import StringProperty

class PopupBttn(Button):
    def openPopup(self):
        print(self.name)
        Pop = TextPopup().open(self.text, self.name)


class TextPopup(Popup):
    bttnid = StringProperty()
    text = StringProperty()
    def open(self, text, id, **kwargs):
        super(TextPopup, self).open(**kwargs)
        self.ids.txtipt.text = text
        self.bttnid = id

    def setText(self):
        App.get_running_app().root.ids[self.bttnid].text = self.text

class MyApp(App):
    pass

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

kv文件

BoxLayout:
    orientation: 'vertical'
    PopupBttn:
        name: 'one'
        id: one
        text: 'I am the first Button'
    PopupBttn:
        name: 'two'
        id: two
    PopupBttn:
        name: 'three'
        id: three
    PopupBttn:
        name: 'four'
        id: four
        text: 'I am the fourth button'



<PopupBttn>:
    text: 'open Popup'
    on_press: self.openPopup()

<TextPopup>:
    text: txtipt.text
    BoxLayout:
        orientation: "vertical"
        TextInput:
            id: txtipt
        Button:
            text: "OK"
            on_release: root.setText()
            on_release: root.dismiss()

答案 1 :(得分:0)

要更新两个按钮,您需要为每个按钮分配唯一ID。有关详细信息,请参阅以下示例。

实施例

main.py

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.popup import Popup
from kivy.properties import ObjectProperty


class MainWidget(GridLayout):
    btn_top = ObjectProperty()
    btn_bottom = ObjectProperty()


class PopText(Popup):
    pass


class MyButton(AnchorLayout):
    my_btn = ObjectProperty()


class TestApp(App):
    title = "Changing button text with popup text input Kivy"

    def build(self):
        return MainWidget()


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

test.kv

#:kivy 1.10.0
#:import F kivy.factory.Factory

<PopText>:
    size_hint: .7, .3
    title: "Input Text"

    TextInput:
        focus: True
        multiline: False
        on_text_validate:
            app.root.btn_top.my_btn.text = self.text
            app.root.btn_bottom.my_btn.text = self.text
            root.dismiss()

<MyButton>:
    my_btn: my_bt
    anchor_y: "top"
    anchor_x: "right"

    Button:
        id: my_bt
        text: "Input Text"
        on_release: F.PopText().open()

<MainWidget>:
    btn_top: btn_top
    btn_bottom: btn_bottom
    cols: 1
    rows: 2

    MyButton:
        id: btn_top
    MyButton:
        id: btn_bottom

输出

enter image description here