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.
Related
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
I'am new in Kivy and have follow problem (Environment is Python 3.7 with Kivy-1.11.1.):
I need a navigation area and a view area (=ViewScreen). With the navigation area i change the view area (change of kv-files - look later at 'def next_screen'). My problem is, that i can't interact with widgets (e.g. 'lblTest') in the view area.
I use follow files:
testGUI.py (= GUI Application)
testGUIRoot.kv (= RootWidget as kv-file)
testGUIBtn1.kv (= view area 1 as kv-file)
testGUIBtn2.kv (= view area 2 as kv-file)
The GUI Application is simple and starts the GUI and change the view area.
testGUI.py:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
class RootWidget(BoxLayout):
# runs select application
def startApplication (self, instance):
print(self.lblTest)
class mainGUI(App):
def build(self):
# loading the content of root.kv
self.root = Builder.load_file('testGUIRoot.kv')
def next_screen(self, screen):
#Clear container and load the given screen object from file in kv folder.
filename = screen + '.kv'
# unload the content of the .kv file
# reason: it could have data from previous calls
Builder.unload_file(filename)
# clear the container
self.root.container.clear_widgets()
# load the content of the .kv file
screen = Builder.load_file(filename)
# add the content of the .kv file to the container
self.root.container.add_widget(screen)
if __name__ == '__main__':
'''Start the application'''
mainGUI().run()
I use follow kv-files:
testGUIRoot.kv:
#:kivy 1.11.1
RootWidget:
container: container
orientation: 'horizontal'
# Navigation
BoxLayout:
orientation: 'vertical'
size_hint: (0.35, 1)
Button:
text: 'testButton1'
on_release: root.startApplication(self,)
on_press: app.next_screen('testGUIBtn1')
Button:
text: 'testButton2'
on_release: root.startApplication(self,)
on_press: app.next_screen('testGUIBtn2')
# ViewScreen
BoxLayout:
size_hint: (0.65, 1)
id: container
orientation: 'vertical'
padding: 0
spacing: 3
Label:
text: 'no data'
color: (0.667,0.667,0.667,1)
font_size: 14
bold: True
testGUIBtn1.kv:
#:kivy 1.11.1
Label:
id: lblTest
text: 'Button 1'
color: (0.667,0.667,0.667,1)
font_size: 14
bold: True
testGUIBtn2.kv:
#:kivy 1.11.1
Label:
id: lblTest
text: 'Button 2'
color: (0.667,0.667,0.667,1)
font_size: 14
bold: True
Follow error appears:
AttributeError: 'RootWidget' object has no attribute 'lblTest'
Have you a solution to interact with the Object 'lblTest'? For example like self.lblTest.text = 'Test-Text'.
Thank you in advance
I have been working with Kivy just this week, also as a new user.
The thing I have learned is to define properties on your RootWidget, just as the labels you already defined in the .kv files. This 'links' the layout and the Python code to eachother.
First of all you need to import the Kivy ObjectProperty by adding:
from kivy.properties import ObjectProperty
Next up is to declare the property on your RootWidget class.
You can add lblTest = ObjectProperty(None) right after the class-declaration.
The top of your file testGUI.py should look like this then:
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
class RootWidget(BoxLayout):
# Link the layout objects
lblTest = ObjectProperty(None)
# runs select application
def startApplication (self, instance):
print(self.lblTest)
The page that really helped me with this is https://techwithtim.net/tutorials/kivy-tutorial/object-properties/
A little sidenote is it would be best to keep your id attributes fully unique.
I found the solution
Content of 'def next_screen' of mainGUI(App) to 'def startApplication'. Now i can change widgets in object oScreen or using the object in other python libs.
def startApplication (self, instance, sScreen):
filename = sScreen + '.kv'
# unload the content of the .kv file
# reason: it could have data from previous calls
Builder.unload_file(filename)
# clear the container
self.container.clear_widgets()
# load the content of the .kv file
oScreen = Builder.load_file(filename)
# add the content of the .kv file to the container
self.container.add_widget(oScreen)
print(oScreen.ids.lblTest.text)
The follow should be added in kv-files testGUIBtn1.kv, testGUIBtn2.kv:
RootWidget:
In kv-file testGUIRoot.kv you have change on_release to
on_release: root.startApplication(self,'testGUIBtn1')
I am expecting the following kivy app to switch screens when I press and then release the button, but nothing happens and there's no error on the terminal. When I run the app, the GirisEkrani screen show up, and then when I press and release the button in GirisEkrani, the next screen (GirisEkrani2) should show up. Do you how to make that work?
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
Builder.load_string("""
<ekranApp>
GirisEkrani:
id: ge
Button:
text: "İleri"
on_release: ge.manager.current = ge.manager.next()
GirisEkrani2:
Button:
id: ge2
text: "Ileri 2"
on_release: ge2.manager.current = ge2.manager.next()
KontrolEkrani:
id: ke
Button:
text: "Geri"
on_release: ke.manager.current = ke.manager.previous()
""")
class GirisEkrani(Screen):
pass
class GirisEkrani2(Screen):
pass
class KontrolEkrani(Screen):
pass
class ekranApp(App, ScreenManager):
def build(self):
#root = ScreenManager()
#root.add_widget(GirisEkrani(name = "giris_ekrani"))
#root.add_widget(GirisEkrani2(name = "giris_ekrani2"))
#root.add_widget(KontrolEkrani(name = "kontrol_ekrani"))
return self
if __name__ == "__main__":
ekranApp().run()
While people seem to advocate the use of .kv files as opposed to using pure Python, I find it very frustrating to have no errors showing up when something doesn't work.
The build() method of an App is expected to return a Widget which will be the root of the Widget tree for your App, but yours returns the App itself. I suspect that will cause problems. However, your code is not working because you are not setting the names of the Screens. Here is a corrected version of your kv:
Builder.load_string("""
<ekranApp>:
GirisEkrani:
id: ge
name: "giris_ekrani"
Button:
text: "İleri"
on_release: ge.manager.current = ge.manager.next()
GirisEkrani2:
id: ge2
name: "giris_ekrani2"
Button:
text: "Ileri 2"
on_release: ge2.manager.current = ge2.manager.next()
KontrolEkrani:
id: ke
name: "kontrol_ekrani"
Button:
text: "Geri"
on_release: ke.manager.current = ke.manager.previous()
""")
Also, your id for GirisEkrani2 is actually set on the Button.
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()
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)