【问题标题】:Kivy: Updating TextInput when pressing ButtonKivy:按下按钮时更新TextInput
【发布时间】:2020-11-14 07:52:16
【问题描述】:

我正在尝试使用 kivy 创建一个 GUI。为此,我想在按下screen1 中的按钮时更新 inputText。为了组织我的页面,我使用了屏幕管理器。我要做的基本上是在按下此页面的按钮后立即在 textInput 中输入 ID 为 textbox 的值。为此,我开始使用方法read_in 准备一个类readInValues

一旦按下按钮,InputText 应该从 Will be filled out automatically 更改为从文件中提取的一些浮点数。

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import StringProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.base import Builder

        
kv_string = Builder.load_string("""
<ScreenManagement>:
    MenuScreen:
        id: name
        name: 'menu'
    
    Screen1:
        id: screen1
        name: 'screen1'

<MenuScreen>:
    BoxLayout:
        BoxLayout:
            canvas:
                Color:
                    rgba: 184/255, 200/255, 202/255, 1
                Rectangle:
                    pos: self.pos
                    size: self.size
      
            GridLayout:
                cols: 4
                rows: 4
                
                BoxLayout:
                    orientation: 'horizontal'
                    spacing: 10
                    padding: 10
                                  
                    Button:
                        size_hint_y: None
                        pos: (10,150)
                        size_hint: None, None
                        size: 250,50
                        text: 'button1'
                        on_press: self.background_color =  0,168/255,137/255,1
                        on_release:
                            root.manager.current = 'screen1'
                            
<Screen1>:
    BoxLayout:
        orientation: 'vertical'
        Button:
            text: 'Return'
            on_press: self.background_color =  0,168/255,137/255,1
            on_release: root.manager.current = 'menu'
            pos: (0,0)
            size_hint: (1,.07)
            
        FloatLayout:
            BoxLayout:
                canvas:
                    Color:
                        rgba: 184/255, 200/255, 202/255, 1

                GridLayout:
                    cols: 2
                    Label:
                        text: 'field1:'
                    TextInput:
                        id: textbox
                        text: 'Will be filled out automatically'
                        background_color: 0,0,0,0.4
                    Label:
                        text: 'field2:'
                    TextInput:
                        id: x2
                        hint_text: 'Will be filled out automatically'
                        color: 1, 0,757, .145,1
                        background_color: 0,0,0,0.4
                    Label:
                        text: 'field3:'
                    TextInput:
                        id: x2
                        hint_text: 'Will be filled out automatically'
                        background_color: 0,0,0,0.4
                    Button:
                        text: 'fill out'
                        on_press:
                            root.callback(self.text)

""")

class readInValues(BoxLayout):
    def read_in(self, file = 'file1.npy'):
        value = load(file)
        return value
    
    
    def input_values(self):
        Input = self.ids.textbox
        Eingabe= self.read_in_weight()
        
            
        
class MenuScreen(Screen,readInValues):
    pass

class Screen1(Screen,readInValues):
    pass


class ScreenManagement(ScreenManager):
    pass

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

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

【问题讨论】:

    标签: python kivy


    【解决方案1】:

    在您的 Screen1 类中,添加一个方法:

    def callback(self, text)
        self.ids.textbox.text = text
    

    我没有测试过这段代码,但我相信它应该可以工作。当然,您可以向该方法添加更多内容。

    【讨论】:

    • 是的,这对我有帮助。它最初似乎有效,但现在我不断收到错误消息:AttributeError:'Button' object has no attribute 'root'。你有什么建议吗?
    • 我想通了 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-14
    • 1970-01-01
    • 2015-09-01
    相关资源
    最近更新 更多