I have unfortuanlly have encourtered an error in kivy and Python 3. I have not found a soultion via Google. I wanted to get text input at the very least but it does not show up. Just the text itself. Thank you for your time!
import kivy
kivy.require('1.10.1') # replace with your current kivy version !
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
class ColdKivyApp(App):
def build(self):
f = FloatLayout()
label = Label(text="Cold") #I acutally orginally called it Zone unitil I changed it into Cold cause it's really cold now
f.add_widget(label)
txt = TextInput(text='', focus=True, multiline=True, cursor_blink=True, background_color=(1,1,1,1))
f.add_widget(txt)
return f
if __name__ == '__main__':
ColdKivyApp().run()
It seems that there is a bug in TextInput when setting the focus in the constructor, a workaround is to set the focus an instant after the window is shown through Clock:
import kivy
kivy.require('1.10.1') # replace with your current kivy version !
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.clock import Clock
class ColdKivyApp(App):
def build(self):
f = FloatLayout()
label = Label(text="Cold")
f.add_widget(label)
txt = TextInput(multiline=True, cursor_blink=True, background_color=(1,1,1,1))
f.add_widget(txt)
Clock.schedule_once(lambda *args: setattr(txt, "focus", True))
return f
if __name__ == '__main__':
ColdKivyApp().run()
Related
I have tried to create a layout with multiple rows, where you can count up and down. The count is displayed in the label next to the buttons.
It is a part of a larger layout and application.
I get the layout that I want, but nothing happens when I press the buttons.
I have watched a lot of videos and searched the internet and encountered the .bind but I still haven't fixed it. I have also tried to make changes in the 'def add' and 'def sub'. I am not sure the *args should be there, but if i remove it I get a Traceback: TypeError: Countdown.add() takes 1 positional argument but 2 were given
I hope you can help on my way :)
The python file:
from kivy.app import App
from kivy.core.window import Window
from kivy.graphics.context_instructions import Color
from kivy.graphics.vertex_instructions import Ellipse, Line, Rectangle
from kivy.metrics import dp
from kivy.uix.scrollview import ScrollView
from kivy.properties import (BooleanProperty, NumericProperty, ObjectProperty,
StringProperty)
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.stacklayout import StackLayout
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget
Window.clearcolor =(1,1,1,1)
class Countdown(GridLayout):
my_text = StringProperty("0")
count = 0
def __init__(self, **kwargs):
super().__init__(**kwargs)
for i in range(0,10):
self.cols = 3
self.btn1=Button(text="+")
self.btn1.bind(on_press=self.add)
self.add_widget(self.btn1)
self.btn2=Button(text="-")
self.btn2.bind(on_press=self.sub)
self.add_widget(self.btn2)
self.lb=Label(text=self.my_text, color=[0,0,0,1])
self.add_widget(self.lb)
def add(self,*args):
self.count +=1
self.my_text = str(self.count)
def sub(self, *args):
self.count -=1
self.my_text = str(self.count)`
After running this code python idle gave me a blank window without any kivy widgets and python idle does not show any error.
what is the problem in this code?
screen shot of blank window
code:
import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
class me(App):
def __init__(self,b,g,l,t):
super(me, self).__init__()
self.b=Button(text='start')
self.g=GridLayout(cols=4)
self.l=Label(text='label')
self.t=TextInput()
self.g.add_widget(self.b)
self.g.add_widget(self.t)
self.g.add_widget(self.l)
m=me('b','g','l','t')
m.run()
Building the App should be done in a build() method of the App, and that method should return the root widget for the App. Like this:
class me(App):
def __init__(self,b,g,l,t):
super(me, self).__init__()
self.b=Button(text='start')
self.g=GridLayout(cols=4)
self.l=Label(text='label')
self.t=TextInput()
self.g.add_widget(self.b)
self.g.add_widget(self.t)
self.g.add_widget(self.l)
def build(self):
return self.g
I'll try to be as curt and straight to the point as possible, as I believe this is probably a simple solution.
How do I get a string that is defined in a python file to show up as a label within a kivy file? Below is a very basic idea on how I tried to achieve this.
kivy-tests.py
import kivy
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.properties import ObjectProperty
from kivy.properties import StringProperty
class Entry(BoxLayout):
x = ObjectProperty('Trees')
class kvfiletests(App):
def build(self):
return Entry()
if __name__ == '__main__':
kvfiletests().run()
kivyfiletests.kv
<Entry>
x: set_name
Label:
id: set_name
text: set_name.text
Any help is greatly appreciated.
Using this code:
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
class Main(App):
def build(self):
root = FloatLayout(size=(100, 100))
root.add_widget(TextInput(pos=(0, 0)))
root.add_widget(TextInput(pos=(50, 50)))
return root
if __name__ == '__main__':
Main().run()
I get two TextInputs, one on top of the other. When I click on the top TextInput (by clicking somewhere in the middle of the screen), the focus goes to the lower TextInput for some reason. In fact, the only way I can get focus on the top TextInput is by clicking entirely outside of the lower TextInput (by clicking right at the top of the screen). Why does this happen, and how can I circumvent this?
Your problem can be approached in two ways.Float layout honors the pos_hint and the size_hint properties of its children.So you need to set size_hint for textinput.
ie-
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
class Main(App):
def build(self):
root = FloatLayout(size=(100, 100))
root.add_widget(TextInput(pos=(0, 0),size_hint=(0.5,0.5)))
root.add_widget(TextInput(pos=(100, 100),size_hint=(0.5,0.5)))
return root
if __name__ == '__main__':
Main().run()
or use boxlayout instead of floatlayout
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
class Main(App):
def build(self):
root = BoxLayout(size=(100, 100))
root.add_widget(TextInput(pos=(0, 0)))
root.add_widget(TextInput(pos=(50, 50)))
return root
if __name__ == '__main__':
Main().run()
I've tried to create an Icon that can be clicked, which means an Image with a ButtonBehavior.I followed the documentation (http://kivy.org/docs/api-kivy.uix.behaviors.html), and I've got a FactoryException with the following code:
# coding: utf-8
from kivy.uix.behaviors import ButtonBehavior
from kivy.core.image import Image
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
kv_string = """
BoxLayout:
IconButton
"""
class IconButton(ButtonBehavior, Image):
def on_press(self):
print("on_press")
class DashboardApp(App):
pass
Builder.load_string(kv_string)
if __name__ == "__main__":
DashboardApp().run()
When I change the parent class of IconButton from (ButtonBehavior, Image) to (ButtonBehavior, Widget), the problem disappears.
You want kivy.uix.image, not kivy.core.image.