I am trying to learn how to create application in Kivy and I have problem with sending argument to the function. I want to send text from input to the function and print it. Can somebody tell me how can I do it ?
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
class TutorialApp(App):
def gratulation(self, *args):
print args
def build(self):
boxLayout = BoxLayout(spacing=10,orientation='vertical')
g = TextInput(text='Enter gratulation',
multiline=False,
font_size=20,
height=100)
button = Button(text='Send')
button.bind(on_press=self.gratulation)
boxLayout.add_widget(g)
boxLayout.add_widget(button)
return boxLayout
if __name__ == "__main__":
TutorialApp().run()
Yo must get the text from "g" and then send it to the button callback, there is 2 ways of doing this, by a lambda function, or calling your class method aplying to it.
Lambda Version:
from __future__ import print_function ##Need to import this for calling print inside lambda
def build(self):
boxLayout = BoxLayout(spacing=10,orientation='vertical')
g = TextInput(text='Enter gratulation',
multiline=False,
font_size=20,
height=100)
button = Button(text='Send')
buttoncallback = lambda:print(g.text)
button.bind(on_press=buttoncallback)
...
The partial version:
from functools import partial ##import partial, wich allows to apply arguments to functions returning a funtion with that arguments by default.
def build(self):
boxLayout = BoxLayout(spacing=10,orientation='vertical')
g = TextInput(text='Enter gratulation',
multiline=False,
font_size=20,
height=100)
button = Button(text='Send')
buttoncallback = partial(self.gratulation, g.text)
button.bind(on_press=buttoncallback)
...
One way to do it:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
class TutorialApp(App):
def gratulation(self, instance):
print(self.g.text)
def build(self):
boxLayout = BoxLayout(spacing=10,orientation='vertical')
self.g = TextInput(text='Enter gratulation',
multiline=False,
font_size=20,
height=100)
button = Button(text='Send')
button.bind(on_press=self.gratulation)
boxLayout.add_widget(self.g)
boxLayout.add_widget(button)
return boxLayout
if __name__ == "__main__":
TutorialApp().run()
Hope it helps!
Related
What I want to do is take input from kivy.uix.textinput.TextInput() and show it on the screen.
I am new to gui Programming and I think it is an easy task.
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
class MyWindowApp(App):
def __init__(self):
super(MyWindowApp, self).__init__()
self.lbl = Label(text='Read Me!')
self.inp = TextInput(multiline=False,
size_hint =(1, 0.05),
pos_hint = {"x":0, "y":0.05})
def build(self):
self.inp.bind(on_text_validate=self.on_enter)
#self.bt1.bind(on_press=self.clk)
layout = FloatLayout()
layout.orientation = 'vertical'
layout.add_widget(self.lbl)
layout.add_widget(self.inp)
return layout
def on_enter(self,value):
print(value)
def clk(self, obj):
print ('input')
x = input()
self.lbl.text = x
window = MyWindowApp()
window.run()
when i run the code, I get the regular output output.
when I type say "hello world" in the textbox, this is the output:
<kivy.uix.textinput.TextInput object at 0x03F5AE30>
I do not get what I typed.
please suggest what should I do
Modify the following ...
def on_enter(self, value):
print(value.text)
I am newbies to programming, just picking up python. I have a very simple program which is showing a popup message, and it seems OK:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.popup import Popup
class t6App(App):
def build(self):
lb = Label(text="Welcome Here",size_hint=(1,0.8),pos_hint={'left':1,'top':1})
bt = Button(text="Press Me", size_hint=(1,0.1),pos_hint={'left':1,'x':0})
fl = FloatLayout()
fl.add_widget(lb)
fl.add_widget(bt)
cp = Popup(title='Guys', content=fl)
return cp
t6App().run()
Then I wanted to modify it, and add more things to it, the popup didn't show then: (2 programs always the same)
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.popup import Popup
class CustPop(Popup):
def build(self):
lb = Label(text="Welcome Here",size_hint=(1,0.8),pos_hint={'left':1,'top':1})
bt = Button(text="Press Me", size_hint=(1,0.1),pos_hint={'left':1,'x':0})
fl = FloatLayout()
fl.add_widget(lb)
fl.add_widget(bt)
cp = Popup(title='Guys', content=fl)
return cp
class t6App(App):
def build(self):
cp1 = CustPop()
return cp1
t6App().run()
The codes do run, but it seems like the layout /button /label in CustPop() can't be accessed (became a blank popup). My question is why and how to make it work?
Thx guys. I am appreciated.
You have defined a build() method in your CustPop, but unlike the App class, that build() method is not automatically called. You should put that code (with a couple minor changes) in the __init__() method.
class CustPop(Popup):
def __init__(self, **kwargs):
super(CustPop, self).__init__(**kwargs)
lb = Label(text="Welcome Here", size_hint=(1, 0.8), pos_hint={'left': 1, 'top': 1})
bt = Button(text="Press Me", size_hint=(1, 0.1), pos_hint={'left': 1, 'x': 0})
fl = FloatLayout()
fl.add_widget(lb)
fl.add_widget(bt)
# cp = Popup(title='Guys', content=fl)
self.title = 'Guys'
self.content = fl
How to get access to different widgets in python kivy?
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
class kxApp(App):
def build(self):
gl_main = GridLayout(rows=2,padding=5, spacing=5)
gl_left = GridLayout(padding=5, spacing=5)
gl_middle = GridLayout(padding=5, spacing=5)
gl_right = GridLayout(padding=5, spacing=5)
gl_main.add_widget(gl_left)
gl_main.add_widget(gl_middle)
gl_main.add_widget(gl_right)
gl_left.add_widget(TextInput(text='Input Here'))
gl_middle.add_widget(Label(text='Just Label'))
gl_right.add_widget(Button(text='Remove Input Field', on_press killFunc))
def killFunc(self,obj):
#how to get access to TextInput() and remove it?
kxApp().run()
What is the natural way to 'get' widget to manipulate it?
By saving a reference to the TextInput, you can simply use remove_widget:
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
class kxApp(App):
def build(self):
gl_main = GridLayout(rows=2,padding=5, spacing=5)
gl_left = GridLayout(cols=1, padding=5, spacing=5)
gl_middle = GridLayout(cols=1, padding=5, spacing=5)
gl_right = GridLayout(cols=1, padding=5, spacing=5)
gl_main.add_widget(gl_left)
gl_main.add_widget(gl_middle)
gl_main.add_widget(gl_right)
self.text_input = TextInput(text='Input Here')
gl_left.add_widget(self.text_input)
gl_middle.add_widget(Label(text='Just Label'))
gl_right.add_widget(Button(text='Remove Input Field', on_press=self.killFunc))
return gl_main
def killFunc(self,obj):
self.text_input.parent.remove_widget(self.text_input)
kxApp().run()
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.core.window import Window
from kivy.uix.scrollview import ScrollView
from kivy.effects.scroll import ScrollEffect
from kivy.uix.widget import Widget
from kivy.uix.button import Button
class BSGameMain:
def sas(self):
# blmain.remove_widget(scrlFBtns)
self.scrlFBtns.remove_widget(blbtns)
blmain = BoxLayout(orientation = 'vertical') # MainBoxLayout init
scrlFBtns = ScrollView(effect_cls = 'ScrollEffect')
blbtns = BoxLayout(
orientation = 'vertical',
size_hint_y = None
) # BoxLayout for buttons
blbtns.bind(minimum_height = blbtns.setter('height'))
scrlFBtns.add_widget(blbtns)
for i in range (2):
blbtns.add_widget(Button(
text='asd',
size_hint_y = None,
height = 40,
on_press = sas
))
lblmain = Label(text = 'asd')
blmain.add_widget(lblmain)
blmain.add_widget(scrlFBtns)
class BSApp(App):
def build(self):
game = BSGameMain()
return game.blmain
if __name__ == "__main__":
BSApp().run()
AttributeError 'Button' object has no attribute scrlFBtn. What is the problem? I'm trying to make it so that when you clicked, the screen was cleared (Widget was deleted). Рelp me please =)
Your code has several errors and bad programming practices:
if you declare variables that are inside a class and outside any method of the class will be class variables and not attributes of the class, so it is not a good practice to do so if you want to use later self, all that code must be within a method of a class.
on_someproperty wait as parameter a function that receives parameters, in your case sas() does not receive them so the solution is to use a lambda method.
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView
from kivy.effects.scroll import ScrollEffect
from kivy.uix.button import Button
class BSGameMain:
def __init__(self):
self.blmain = BoxLayout(orientation = 'vertical') # MainBoxLayout init
self.scrlFBtns = ScrollView(effect_cls = 'ScrollEffect')
self.blbtns = BoxLayout(
orientation = 'vertical',
size_hint_y = None )
self.blbtns.bind(minimum_height = self.blbtns.setter('height'))
self.scrlFBtns.add_widget(self.blbtns)
for i in range(2):
self.blbtns.add_widget(Button(
text='asd',
size_hint_y = None,
height = 40,
on_press = lambda *args: self.sas()))
lblmain = Label(text = 'asd')
self.blmain.add_widget(lblmain)
self.blmain.add_widget(self.scrlFBtns)
def sas(self):
self.scrlFBtns.remove_widget(self.blbtns)
class BSApp(App):
def build(self):
game = BSGameMain()
return game.blmain
if __name__ == "__main__":
BSApp().run()
Simple question, how can i divide two numbers from a single InputBox? I have no idea, example, i have only one inputbox and i write two numbers "40 10" how can i divide automatically this? Here the code:
import kivy
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.listview import ListView
class Widget(GridLayout):
def __init__(self, **kwargs):
super(Widget, self) .__init__(**kwargs)
self.cols = 2
self.add_widget(Label(text="RPM"))
self.rpm = TextInput(multiline=False)
self.add_widget(self.rpm)
btn1 = Button(text="Division:")
btn1.bind(on_press=self.buttonClicked)
self.add_widget(btn1)
Example of what i need:
def buttonClicked(self, btn):
self.rpm.text(first input / second input)
x = self.rpm.text
popup = Popup(title='Result', content=x, size_hint=(None,
None), size=(500, 90))
popup.open()
The procedure is the next:
Get the text from TextInput
Separate it by the space
Verify that only 2 terms exist
Convert it to float, so an error may appear, exceptions must be used.
And for the last one we establish the popup with a Label that contains the text.
import kivy
kivy.require("1.0.6")
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.popup import Popup
class Widget(GridLayout):
def __init__(self, **kwargs):
super(Widget, self) .__init__(**kwargs)
self.cols = 2
self.add_widget(Label(text="RPM"))
self.rpm = TextInput(multiline=False)
self.add_widget(self.rpm)
btn1 = Button(text="Division:")
btn1.bind(on_press=self.buttonClicked)
self.add_widget(btn1)
def buttonClicked(self, btn):
texts = self.rpm.text.split()
if len(texts) == 2:
try:
x, y = map(float, texts)
res = x/y
popup = Popup(title='Result', content=Label(text=str(res)), size_hint=(None, None), size=(500, 90))
popup.open()
except (ValueError, ZeroDivisionError):
print("error")
class TestApp(App):
def build(self):
return Widget()
if __name__ == '__main__':
TestApp().run()
This type of interfaces are few useful since the user could place anything, the appropriate thing is to validate the text while it is being written as for example to accept a certain set of characters.
Another improvement is that it is separated into 2 TextInputs.