Reputation: 47
I am new to Python
(version 3.6.1
) and have just started using Kivy
as my GUI
.
What I want to do is to create a command line in Kivy
which lets the user type in a command, processes the command and then outputs what needs to be output in the same text "box". It should work exactly like a Windows
commmand line, but it has to be a part of the Kivy
interface.
The closest I ever got to that was using the kivy.uixTextInput
, however, I have no idea how to fetch the user input and then how to print to that text box the output. Please help, I am struggling.
Upvotes: 3
Views: 6756
Reputation: 630
In your py
file:
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()
In your kv
file, (mine is called 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()
Upvotes: 4