【问题标题】:Kivy text input and outputKivy 文本输入和输出
【发布时间】:2018-02-12 22:04:28
【问题描述】:

我是Python(版本3.6.1)的新手,刚刚开始使用Kivy 作为我的GUI

我想做的是在Kivy 中创建一个命令行,让用户输入命令,处理命令,然后在同一个文本“框”中输出需要输出的内容。它应该像Windows 命令行一样工作,但它必须是Kivy 接口的一部分。

我最接近的方法是使用kivy.uixTextInput,但是,我不知道如何获取用户输入,然后如何将输出打印到该文本框。请帮忙,我很挣扎。

【问题讨论】:

    标签: python input kivy


    【解决方案1】:

    在您的py 文件中:

    from kivy.app import App
    from kivy.uix.boxlayout import BoxLayout
    
    class MainWindow(BoxLayout):
    
        # We create a dictionary of all our possible methods to call, along with keys
        def command_dict(self):
            return {
                'one': self.command_one,
                'two': self.command_two,
                'three': self.command_three
            }
    
        def process_command(self):
            # We grab the text from the user text input as a key
            command_key = self.ids.fetch_key_and_process_command.text
    
            # We then use that key in the command's built in 'get_method' because it is a dict
            # then we store it into a variable for later use
            called_command = self.command_dict().get(command_key, 'default')
    
            try:
                # The variable is a method, so by adding we can call it by simple adding your typical () to the end of it.
                called_command()
    
            except TypeError:
                # However we use an exception clause to catch in case people enter a key that doesn't exist
                self.ids.fetch_key_and_process_command.text = 'Sorry, there is no command key: ' + command_key
    
        # These are the three commands we call from our command dict.
        def command_one(self):
            self.ids.fetch_key_and_process_command.text = 'Command One has Been Processed'
    
        def command_two(self):
            self.ids.fetch_key_and_process_command.text = 'Command Two has Been Processed'
    
        def command_three(self):
            self.ids.fetch_key_and_process_command.text = 'Command Three has been Processed'
    
    class MainApp(App):
    
        def build(self):
            return MainWindow()
    
    if __name__ == '__main__':
        MainApp().run()
    

    在您的kv 文件中,(我的文件名为mainapp.kv):

    <MainWindow>:
        Label:
            text: 'This is a label'
        TextInput:
            # We give TextInput an id to reference it in the actual code
            id: fetch_key_and_process_command
    
            # We set multiline to False, so we can use the next property (on_text_validate)
            multiline: False
    
            # on_text_validate will called MainWindow's process_command() when a user presses enter
            on_text_validate: root.process_command()
    

    【讨论】:

    • 非常感谢!这非常出色。但是,有两个我无法克服的技术问题: 1. 如何在不替换的情况下打印代码下方的消息?我希望它像这样打印:一个命令一已处理 两个命令二已处理 2. 我必须单击命令行才能输入下一个命令,这是允许我继续键入命令而不使用的任何解决方法我的鼠标呢?感谢您的努力
    • self.ids.fetch_key_and_process_command.text = ''
    猜你喜欢
    • 1970-01-01
    • 2016-06-07
    • 2019-12-11
    • 1970-01-01
    • 2021-10-01
    • 2016-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多