Kivy - TextInput behavior similar to Excel - python

I'm editing a text of TextInput 'A' in Kivy application. I now need to copy text of TextInput 'B' to the A, by clicking on B, without A loosing it's focus.
Something like when I write an equation in Excel, I can click on another cell and the cell ID is copied to the equation, instead of selecting the another cell.
How would I do this, please?
Thanks.

Not really sure if that is what you are looking for. If you click into the second TextInput it will copy the content of the first TextInput. I am using a main.py
# main.py
from kivy.app import App
from kivy.properties import StringProperty
class AnswerApp(App):
text_of_text_input_1 = StringProperty()
def change_text_of_text_input_2(self):
self.text_of_text_input_1 = self.root.ids.text_input_1.text
if __name__ == "__main__":
AnswerApp().run()
and the kv file answer.kv.
# answer.kv
BoxLayout:
orientation: "vertical"
TextInput:
id: text_input_1
text: "text_input_1"
TextInput:
text: app.text_of_text_input_1
on_focus: app.change_text_of_text_input_2()

Related

Text overlapping in label whenever updating the label in kivy

Whenever I am trying to update my label in the app, it is always overlapping the previous text. I don't know what is the mistake I am making for it to happen. If anyone knows please guide me.
here is the .py file:
import json
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.widget import Widget
Builder.load_file('rough.kv')
class MyLayout(Widget):
f=open('questions.json')
global data
data=json.load(f)
def printing_questions(self):
a=0
question='Blue whale is a large animal'
self.ids.question.text=''
self.ids.question.text=question #i am changing text here, but it overlaps
class rough(App):
def build(self):
return MyLayout()
if __name__=='__main__':
rough().run()
Here is the .kv file:
#:kivy 2.1.0
<MyLayout>:
BoxLayout:
orientation:'vertical'
size:root.width,root.height
Label:
id:question #in this label i am updating the text
text:'Questions coming soon...444444444444442222222222222222ssssssssss\n555555555555211111assssssssss'
multiline:True
Button:
text:'Next'
on_release:
root.printing_questions()
Not sure what exactly you are expecting. But if you want the text to wrap into its container (or, cease to overflow) you can modify its text_size property as,
Label:
id:question #in this label i am updating the text
text:'Questions coming soon...444444444444442222222222222222ssssssssss\\n555555555555211111assssssssss'
halign: "center" # To set in the middle.
text_size: self.width, None # Extends upto it's width.
The previous text is:'Hello my name is mrsomethng' and when i update the label with:'What was the original color of ferrari models' they are both overlapping.
i want to show only the updated label not the previous text with it

Make a variable from .py get displayed in a Kivy Label when pressing a button

I've been watching some tutorials on Kivy where a functional button prints a string in the console. However I haven't found a simple example where a functional button displays a string somewhere on the GUI (for instance a label) yet. So I tried to make one myself.
Looking for answers here I found some which are far too complex and cover several intertwined issues at the same time, which makes figuring out what would work in a simple example like this daunting, to say the least. Here's what I did so far:
simple.py
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
class SimpleWidget(BoxLayout):
result = ObjectProperty(None)
def button(self, result):
print(str(result.text))
class SupersimpleApp(App):
def build(self):
return SimpleWidget()
if __name__ == "__main__":
SupersimpleApp().run()
And here's supersimple.kv
<SimpleWidget>:
result: result
orientation: 'vertical'
BoxLayout:
id: box1
Label:
text: 'Type something'
TextInput:
id: result
height: 80
Button:
id: button
text: 'Press this'
on_press: root.button(result)
BoxLayout:
id: box2
Label:
id: your_input
What I want to achieve is to show ''result'' in your_input Label instead of printing it.
You can use the ids that you have defined in the kv, like this:
def button(self, result):
print(str(result.text))
self.ids.your_input.text = result.text

How to clear an MDTextField without the hint_text in the wrong position

I want to clear an MDTextField. The problem is when I set the text to "", the hint_text stays in the wrong position. (It happens when I try to clear while unfocused from the MDTextField)
I tried to focus and then unfocus (from the code) on the text field to fix this, but it's not a good fix because on android the keyboard would come up. I also found this on the KivyMD documentation:
When changing a TextInput property that requires re-drawing, e.g. modifying the text, the updates occur on the next clock cycle and not instantly. This might cause any changes to the TextInput that occur between the modification and the next cycle to be ignored, or to use previous values. For example, after a update to the text, changing the cursor in the same clock frame will move it using the previous text and will likely end up in an incorrect position. The solution is to schedule any updates to occur on the next clock cycle using schedule_once().
Here's the code:
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
kv = Builder.load_string("""
<MainScreen>:
orientation: "vertical"
FloatLayout:
MDTextField:
id: textfield
hint_text: "Text Fields"
pos_hint: {"center_y":.5}
Button:
text: "Clear"
on_press: root.clear_text_field()
""")
class MainScreen(BoxLayout):
def clear_text_field(self):
self.ids.textfield.text = ""
class MainApp(MDApp):
def build(self):
return MainScreen()
if __name__ == "__main__":
app = MainApp()
app.run()

Kivy - Create style buttons for reStructuredText

I would like to create buttons for the reStructuredText widget in Kivy. The buttons would do basic things like bold, underline, or make a heading so the user doesn't have to manually type in the markup. For example, the user could select some text then click the 'bold' button and the text would then be surrounded by [b]...[/b].
I would love to show code of what I've tried but I honestly don't even know where to begin. (Or please let me know if there is a better way to implement basic text editing in Kivy.) I'm currently using the Kivy language to display the rst widget by simply adding
RstDocument:
show_errors: True
to the kv file (along with the save, etc... buttons).
In your question, I heard about the RstDocument widget for the first time. You got me interested and I came up with a minimal sample app which could be a good starting point for you to add more.
This is my python file
from kivy.app import App
from kivy.base import Builder
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
Builder.load_string("""
<root_wgt>:
orientation: 'vertical'
BoxLayout:
size_hint_y:0.2
Button:
text: 'Emphasize'
on_press: root.emphasize()
Button:
text: 'Section Header'
on_press: root.add_section_header()
Button:
text: 'Subection Header'
on_press: root.add_sub_section_header()
BoxLayout:
orientation: 'vertical'
TextInput:
id: textinput
RstDocument:
id: rstdocument
text: textinput.text
""")
class root_wgt(BoxLayout):
def emphasize(self):
text = self.ids.textinput.text
selection = self.ids.textinput.selection_text
begin = self.ids.textinput.selection_from
end = self.ids.textinput.selection_to
new_text = text[:begin] + ' **' + selection + '** ' + text[end:]
self.ids.textinput.text = new_text
self.ids.rstdocument.render()
def add_section_header(self):
self.ids.textinput.insert_text("""\n==============""")
def add_sub_section_header(self):
self.ids.textinput.insert_text("""\n-----------------""")
class MyApp(App):
def build(self):
return root_wgt()
if __name__ == '__main__':
MyApp().run()
Alternatively, you could just go with a label which also has some styling options https://kivy.org/docs/api-kivy.uix.label.html#markup-text The implementation would look quite similar.

New instance of Kivy widget does not have attributes of kv lang file

I created a class for a Popup and set the title property in the kv file.
When the popup shows, it does not have the title as in the kv file, but instead shows No Title as if it was never set.
It is exactly the same problem as here, but I do not understand from this link what the problem is or how to make it work:
https://github.com/kivy/kivy/issues/751
I understand how to do this using IDs in kv lang, but that only works if the Popup is put as a child widget of the root widget (ex. MainUI). Then I can link an instance of a python class to a widget in the kv file.
But then the popup displays as part of the root widget.
What I want to do, is instantiate a new instance of the popNewDB class when the New button is clicked and have this instance use the values such as "title" in the KV file.
Can you please explain how to do this?
Here is my code:
py file:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.popup import Popup
from kivy.properties import ObjectProperty
class popNewDB(Popup):
pass
class MainUI(Widget):
pop = ObjectProperty(popNewDB())
def showpopup(self):
self.pop.open()
class VerseReminder(App):
def build(self):
return MainUI()
if __name__ == '__main__':
VerseReminder().run()
kv file:
#:kivy 1.9.1
<popNewDB>
title: 'Hallo'
<MainUI>
Label:
pos: root.center_x - self.width/2,root.center_y + 200
text: "Quote Reminder"
BoxLayout:
size_hint: None,None
width: 400
height: 200
pos: root.center_x-200,root.center_y-50
orientation: 'vertical'
spacing: 20
Button:
size_hint: 1,1
text: "New..."
on_press: root.showpopup()
Button:
size_hint: 1,1
text: "Open..."
Button:
size_hint: 1,1
text: "Quit"
At the time pop = ObjectProperty(popNewDB()) is evaluated, the rules haven't been loaded, so only a barebones Popup will be created. Instead, you could do this:
class MainUI(Widget):
pop = ObjectProperty()
def showpopup(self):
if self.pop is None:
self.pop = PopNewDB()
self.pop.open()
Here, the first time the button is pressed, a new instance of PopNewDB will be created and stored in self.pop.
(NB: I renamed the Popup subclass to start with a Capital Letter, to be consistent with language standards and kivy expectations)

Categories