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
Related
The kivy app that I have coded up won't work on my samsung galaxy s10e but will work on my computer. Maybe I need to add somthing to my .spec file but I dont know
Here is the code for the app:
import kivy
kivy.require('2.1.0')
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
import random
class MyRoot(BoxLayout):
def __init__(self):
super(MyRoot, self).__init__()
def generate_number(self):
self.random_label.text = str(random.randint(0, 1000))
class randomnumber(App):
def build(self):
return MyRoot()
randomint = randomnumber()
randomint.run()
I am trying to change to another screen by swiping the screen. I've tried carousel but it seems that it only works with images, so I've tried detecting a swipe motion and changing the screen after it has been detected.
main.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.uix.button import ButtonBehavior
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.carousel import Carousel
from kivy.uix.widget import Widget
from kivy.uix.popup import Popup
class HomeScreen(Screen):
def on_touch_move(self, touch):
if touch.x < touch.ox: # this line checks if a left swipe has been detected
MainApp().change_screen(screen_name="swipedhikr_screen") # calls the method in the main app that changes the screen
class ImageButton(ButtonBehavior, Image):
pass
class LabelButton(ButtonBehavior, Label):
pass
class SettingsScreen(Screen):
pass
class SwipeDhikrScreen(Screen):
pass
#def quit_verification():
# pop = Popup(title="verification", content=Label(text= "Are you sure?"))
GUI = Builder.load_file("main.kv")
class MainApp(App):
def build(self):
return GUI
def change_screen(self, screen_name):
# get the screen manager from the kv file
screen_manager = self.root.ids["screen_manager"]
screen_manager.transition.direction = "up"
screen_manager.current = screen_name
def quit_app(self):
MainApp().stop()
MainApp().run()
I got an attribute error: "None type object has no attribute 'ids'"
MainApp().change_screen(screen_name="swipedhikr_screen")
This line creates a new instance of MainApp, which doesn't have any widgets and therefore naturally fails when you try to access them.
Use the existing instance of MainApp, i.e. the one that you're actually running, via MainApp.get_running_app().
Also you are not correct that Carousel works only with images.
I'm trying to use kivy for my project but I can't deal with it well..
I made a button, but I want that when I press him it will create another (new) button. Thanks a lot!
from kivy.app import App
from kivy.lang import builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivy.uix.button import Button
def createButton():
b = Button(pos=(0, 90), size_hint=(.2, .2), on_press=lambda a:ad())
return b
def ad():
"crate new button here!"
class NoobApp(App):
def build(self):
return createButton()
if __name__ == '__main__':
NoobApp().run()
In your ad() method add a line to create a Button and add it to the root of the app:
def ad():
print("crate new button here!")
App.get_running_app().root.add_widget(Button(text='hi'))
Note that this is adding a Button to a Button, (the root of the app is the original Button). Not a recommended approach. You should probably return some kind of Layout from your build() method instead.
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()
I am trying to get the textinput widget to pass text into the callback function that makes a label with the text when called by the printbutton, should be fairly simple when you think about it. But I have a habit of not seeing the wood for the trees. Anyhoo, if anyone can figure this out then code it up :P
import kivy
kivy.require('1.5.1')
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
class kivyentrywidget(GridLayout):
def __init__(self, **kwargs):
super(kivyentrywidget, self).__init__(**kwargs)
self.cols = 2
self.add_widget(Label(text='What do you want to print?'))
self.text_input = TextInput(multiline=False)
self.add_widget(self.text_input)
self.printbutton = Button(text='Print')
self.printbutton.bind(on_press=callback)
self.add_widget(self.printbutton)
def callback(self):
return Label(text=self.text_input.text)
class Firstapp(App):
def build(self):
return kivyentrywidget()
if __name__ == '__main__':
Firstapp().run()
def callback(self,evt=None): #not sure if kivy sends event info so added optional arg just in case
return self.add_widget(Label(text=self.text_input.text))
maybe ... not overly familiar with kivy but i think that would do it ..
also
self.printbutton.bind(on_press=self.callback)
should fix your other problem