How to get input box content in kivy application - python

I'm writing a kivy app that lets the user enter and store content, but I've searched the web and can't find a solution
This is the python program
from kivy.app import App
from kivy.graphics import Line, Color
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.config import Config
Config.set('graphics', 'resizable', False)
from kivy.core.window import Window
Window.size = (450, 800)
class DrawCanvasWidget(Widget):
def __init__(self,**kwargs):
super().__init__(**kwargs)
class PaintApp(App):
def build(self):
self.draw_canvas_widget = DrawCanvasWidget()
return self.draw_canvas_widget # 返回root控件
if __name__ == "__main__":
PaintApp().run()
This is the kv program
<DrawCanvasWidget>
orientation: "vertical"
canvas.before:
Color:
rgba: [1,1,1,1]
Rectangle:
pos: self.pos
size: self.size
TextInput:
multiline:False
font_size:20
pos:75,450
size:300,40
allow_copy:False
cursor_color:[0,1,0,1]
Button:
text:'ok'
bold:10
size_hint:None,None
size:100,50
pos:180,380
I want to let the user save the content of the input box after pressing the button

This is the basic idea. Create a function in the draw canvas widget and pass the text of the text input by using the id tag.
<DrawCanvasWidget>
orientation: "vertical"
canvas.before:
Color:
rgba: [1,1,1,1]
Rectangle:
pos: self.pos
size: self.size
TextInput:
id: text_input
multiline:False
font_size:20
pos:75,450
size:300,40
allow_copy:False
cursor_color:[0,1,0,1]
Button:
text:'ok'
bold:10
size_hint:None,None
size:100,50
pos:180,380
on_release: root.submit(text_input.text)
class DrawCanvasWidget(Widget):
def __init__(self,**kwargs):
super().__init__(**kwargs)
def submit(self, text_input):
print(text_input)
# Should print whatever is in the textinput to the terminal
# Here you can save it to your database or do anything with the input.

Related

Access to variables from Popup class in .kv file

I need light/dark theme. I can make it work but I want another solution (I know about KivyMD too but in this case I don't want to use it).
In short: I have multiple screens and on all screens there is a Popup button.
On the Popup, there are two buttons: light and dark. So when I press light or dark it should changes the background color of the screens.
My problem is that I don't know how to access to Popup class from the .kv file. Could you give me a solution or a hint how to achive this?
Minimal code:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.popup import Popup
from kivy.properties import ListProperty
Builder.load_string("""
<FirstScreen>:
canvas.before:
Color:
#--------------> how to access to popup class from here?
#rgba: ???.my_background_color
rgba: 240/255, 240/255, 240/255, 1
Rectangle:
pos: self.pos
size: self.size
Button:
text: 'Popup'
size_hint: 0.5, 0.5
on_release:
root.the_popup()
<MyPopup>
size_hint: 0.75 , 1
title: 'T H E M E'
BoxLayout:
Button:
text: 'D A R K'
on_release:
root.dark()
Button:
text: 'L I G H T'
on_release:
root.light()
"""
)
class FirstScreen(Screen):
def the_popup(self):
the_popup = MyPopup()
the_popup.open()
class MyPopup(Popup):
my_background_color = ListProperty([240/255, 240/255, 240/255, 1])
def light(self):
self.my_background_color = [240/255, 240/255, 240/255, 1]
def dark(self):
self.my_background_color = [48/255, 48/255, 48/255, 1]
class myApp(App):
def build(self):
sm = ScreenManager()
sm.add_widget(FirstScreen(name='first'))
return sm
if __name__ == '__main__':
myApp().run()

Kivy: How to check the location of a drag behavior item

Not much code yet, but I am trying to make the buttons stay if they are on the right side, but if they are on the left, to disappear. I am not sure if this is possible, but if you are able to help, I would love it!main.py:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.behaviors import DragBehavior
from kivy.uix.button import Button
Builder.load_file('main.kv')
class DragButton(DragBehavior, Button):
pass
class MainScreen(Widget):
pass
class WorkingApp(App):
def build(self):
return MainScreen()
if __name__ == '__main__':
WorkingApp().run()
​
main.kv
<DragButton>
drag_rectangle: self.x, self.y, self.width, self.height
drag_timeout: 10000
drag_distance: 0
text: 'Hi'
size_hint: (1, .5)
<MainScreen>
BoxLayout:
size: root.width, root.height
orientation: 'horizontal'
BoxLayout:
orientation: 'vertical'
Label:
text: 'Widgets'
size_hint: (1, .25)
DragButton:
DragButton:
DragButton:
Splitter:
sizable_from: 'left'
Label:
text: 'Check If they are here'
One way to do this is to bind a method to the center_x property of the DragButtons. That method can then check the position of the DragButton, and remove it, if it is to the right of the Splitter:
from kivy.app import App
from kivy.properties import ObjectProperty
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.behaviors import DragBehavior
from kivy.uix.button import Button
Builder.load_file('main.kv')
class DragButton(DragBehavior, Button):
pass
class MainScreen(Widget):
splitter = ObjectProperty(None)
boxlayout = ObjectProperty(None)
def dragged(self, dragButton):
if dragButton.center_x > self.splitter.x:
self.boxlayout.remove_widget(dragButton)
class WorkingApp(App):
def build(self):
return MainScreen()
if __name__ == '__main__':
WorkingApp().run()
with some small modifications to the 'kv':
<DragButton>
drag_rectangle: self.x, self.y, self.width, self.height
drag_timeout: 10000
drag_distance: 0
text: 'Hi'
size_hint: (1, .5)
<MainScreen>
splitter: split
boxlayout: box
BoxLayout:
size: root.width, root.height
orientation: 'horizontal'
BoxLayout:
id: box
orientation: 'vertical'
Label:
text: 'Widgets'
size_hint: (1, .25)
DragButton:
on_center_x: root.dragged(self)
DragButton:
on_center_x: root.dragged(self)
DragButton:
on_center_x: root.dragged(self)
Splitter:
id: split
sizable_from: 'left'
Label:
text: 'Check If they are here'
Key modifications are the addition of ids in the kv for the Splitter and the BoxLayout and methods bound to the center_x property of each DragButton. In the python, ObjectProperties for those new ids and the dragged() method are added to the MainScreen class. The dragged() method just checks if the center of the DragButton is roght of the Splitter and removes that DragButton if it is.

How can i make custom class having image as button and text below in kivy?

How can I make custom class having an image/png icon as a button and text below in kivy and use that custom made class avoiding the recurring lines in code?
I am new to kivy and python.The basic idea is to create a class, which consists of a round vector icon as portfolio and name of the portfolio below it. So I can copy it multiple times wherever needed.
Please help. Thanks in Advance.
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.config import Config
from kivy.uix.label import Label
from kivy.uix.button import Button
class custombutton(Button, Label, BoxLayout):
pass
class pfselectwindow(BoxLayout):
Config.set('graphics', 'borderless', 'True')
Config.set('graphics', 'width', '900')
Config.set('graphics', 'height', '600')
Config.write()
def __init__(self, **kwargs):
super().__init__(**kwargs)
class pfselectApp(App):
def build(self, **kwargs):
return pfselectwindow()
if __name__ == "__main__":
app =pfselectApp()
app.run()
.kv file
<custombutton#BoxLayout+Button+Label>:
orientation:"vertical"
Button:
text:""
size_hint:None, None
size:80,80
Image:
source:"F:/untitled/accounticon.png"
Label:
size_hint_y:None
height:5
text:""
<pfselectwindow>:
id:select_portfolio
orientation:"vertical"
padding:10
#left with 580
BoxLayout:
size_hint_y:None
size_hint_y:100
#left with 530
canvas.before:
Color:
rgba:(0.5,0.5,0.5,0.5)
Rectangle:
size:self.size
pos:self.pos
Label:
text:"SELECT PORTFOLIO"
BoxLayout:
padding:10
orientation:"vertical"
spacing:5
size_hint_y:None
size_hint_y:430
#left with 230
canvas.before:
Color:
rgba:(0.6,0.6,0.6,0.6)
Rectangle:
size:self.size
pos:self.pos
StackLayout:
orientaton:"lr-tb"
spacing:5
Button:
size_hint:None,None
size:80,80
text:"P1"
Button:
size_hint:None,None
size:80,80
text:"P2"
Button:
size_hint:None,None
size:80,80
text:"+"
BoxLayout:
size_hint_y:None
size_hint_y:50
#left with 50
canvas.before:
Color:
rgba:(0.5,0.5,0.5,0.5)
Rectangle:
size:self.size
pos:self.pos
Label:
text:"status"
enter image description here

How to use image as splash screen in kivy python desktop application with labels?

This is my code which just accepts numbers and add them and displays results in label, numbers are accepted through spinners. But I need splash screen before the main app is loaded. I want to know how we can use image as an splash screen and display for a while until the main app starts.
from kivy.lang import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.properties import StringProperty
from kivy.uix.spinner import Spinner
from kivy.app import App
Builder.load_string('''
<MainScreen>:
GridLayout:
orientation: 'vertical'
cols: 1
canvas.before:
Color:
rgba: 1, 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
GridLayout:
orientation: 'vertical'
cols: 2
Spinner:
id: first
text: ' First Number'
values: ['1','2','3','4','5','6','7','8','9']
Spinner:
id: second
text: ' Second Number'
values: ['1','2','3','4','5','6','7','8','9']
Label:
id: result
text: ' Result'
color: 0,0,0,1
Button:
id: res
on_press: root.onPow(first.text,second.text)
text: 'Progress'
''')
class MainScreen(FloatLayout):
changet = StringProperty()
def __init__(self, **kwargs):
super(MainScreen, self).__init__(**kwargs)
def onPow(self,fir,sec):
a = 0
b = 0
for i in range((10000)):
print(b)
self.ids.result.text=str(b*(int(fir)*int(sec)+i))
b+=1
class TestApp(App):
def build(self):
return MainScreen()
if __name__ == "__main__":
TestApp().run()
Image for splash screen:
Even can we use labels with this image. Thank you.

Read the text from Textinput dynamically when text changed in text box?

I'm new to kivy , I'm trying to write an application using kivy in python, I got struck at one point where i have to read text from textinput whenever it is changed and based on that i want to implement my button's functionality - I have gone through all the documentation but i couldn't figure out how to do it - Can any body tell me how can I resolve this or am I missing something?
from __future__ import print_function
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.core.image import Image
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics import *
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.popup import Popup
from kivy.properties import ObjectProperty
import walascan
from kivy.clock import Clock
import os
kv = """
<KartScan>:
IntroScreen:
<IntroScreen#Screen>:
orientation: 'horizontal'
name: 'introscreen'
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: 'index.png'
AnchorLayout:
anchor_x: 'center'
anchor_y: 'center'
BoxLayout:
orientation:'horizontal'
size_hint: .5, .1
canvas:
Color:
rgb: 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
spacing: 20
pos_hint: {'center_x':.8, 'center_y': .8}
AnchorLayout:
anchor_x: 'left'
size_hint_x: .5
TextInput:
id: waybill
width: 20
text: "Enter Waybill No."
multiline: False
height: self.minimum_height
size_hint_y: None
font_size: 30
focus: True
on_text_validate: app.on_waybill()
AnchorLayout:
anchor_x: 'right'
size_hint_x: None
Button:
size_hint: None, None
height: 50
width: self.texture_size[0]
padding: 10, 10
text: "Add"
on_press:app.buttonClicked()
on_release: root.current = 'mainpage'
AnchorLayout:
anchor_x: 'right'
size_hint_x: None
Button:
size_hint: None, None
height: 50
width: self.texture_size[0]
padding: 10, 10
text: "Compare"
on_press:app.buttonClicked()
on_release: root.current = 'mainpage'
"""
Builder.load_string(kv)
waybill = TextInput(text="Enter Waybill No.", multiline=False)
class KartScan(FloatLayout):
def __init__(self, **kwargs):
super(KartScan, self).__init__(**kwargs)
self.register_event_type('on_text_validate')
def on_text(self, *args):
print('new value is ', waybill.text)
def on_text_validate(self):
pass
def on_focus(self, obj, focused):
if not focused:
self.dispatch('on_text_validate')
class KartScanApp(App):
def build(self):
return KartScan()
def buttonClicked(self):
popup = Popup(title='Result',
content=Label(text=self.on_waybill()),
size_hint=(None, None), size=(100, 100))
popup.bind()
popup.open()
def getwlbtstate(self):
return walascan.mainAntennas()
def on_waybill(self):
waybill.bind(text=KartScan.on_text_validate)
# popup = Popup(title='Result',
# content=Label(text=waybill.text),
# size_hint=(None, None), size=(100, 100))
# popup.bind()
# popup.open()
return waybill.text
if __name__ == '__main__':
KartScanApp().run()
kv file
TextInput:
on_text: print(self.text)
You could use on_text to call a function when the text changes. I think it sometimes even triggers when no change has occured.
TextInput has a text property, and in kivy, all properties have associated on_xxx events, that you can bind to to react to them, so when reading the doc, even if it's not explicitly mentionned for them, everytime you see something is defined as a Property (or any subclass of it, like StringPropertyNumericProperty,ObjectProperty,AliasProperty`).
TextInput:
on_text: do_something(self.text)
or in python
t = TextInput(text='', on_text=do_something)
or
t = TextInput(text='')
t.bind(on_text=do_something)

Categories

Resources