I don't know why when I run, the window won't show the buttons that I've added
Here is my code
from kivy.app import App
from kivy.metrics import dp
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.stacklayout import StackLayout
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.widget import Widget
class StackLayoutExample(StackLayout):
def __int__(self, **kwargs):
super().__init__(**kwargs)
b = Button(text="A", size_hint=(0.2, 0.2))
self.add_widget(b)
class TheLabApp(App):
pass
TheLabApp().run()
and the .kv file is simply this:
StackLayoutExample:
You wrote the typo def __int__ but you need def __init__.
Related
why isn't kivy show a text box for this code? please help me know how to let kivy add a text box by using this style.
thank you
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
class textbox(GridLayout):
def __initial__(self, **kwargs):
super(MyGrid, self).__initial__(**kwards)
self.cols = 2
self.add_widger(Label(text = "Point for good"))
self.name = TextInput(multiline = False)
self.add_widget(self.name)
if __name__ == '__main__':
textbox().run()
For any kivy program, You need to inherit from App class and override the build method and place your UI elements here, and call run() method from the instance of the class inheriting the the App class
secondly, its add_widget() not add_widger and perhaps you mean __init__() instead of __initial__
Here is the corrected code:
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
class TextBox(App):
def build(self):
grid = GridLayout()
grid.cols = 2
grid.add_widget(Label(text = "Point for good"))
name = TextInput(multiline = False)
grid.add_widget(name)
return grid
if __name__ == '__main__':
TextBox().run()
I was working on kivy to learn to build apps. I am encountering this error while running the attached code.
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
class MyGrid(GridLayout):
def __init__(self,**kwargs):
super(MyGrid,self).__init__(**kwargs)
self.cols=2
self.add_widget(Label(text="Name:"))
self.name=TextInput(multiline=False)
self.add_widget(self.name)
class MyApp(App):
def build(self):
return MyGrid
if __name__== "__main__":
MyApp().run()
You need to replace return MyGrid ----> return MyGrid()
I am new to kivy and using some tutorials but i only get black screen while on the videos developpers get their widget on the screen. i've tried multiple solutions but i dont really understand what the problem is. I am trying to build a login screen here is the code i wrote :
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
class LoginScreen(BoxLayout):
def __int__(self, **kwargs):
super(LoginScreen,self).__int__(**kwargs)
sm=ScreenManager
Screen=Screen(name="screen")
layout=BoxLayout(orientation='vertical')
self.Username = TextInput(multiline=False)
self.Password = TextInput(multiline=False, password=True)
layout.add_widget(Label(text="username"))
layout.add_widget(self.Username)
layout.add_widget(Label(text="password"))
layout.add_widget(self.Password)
Screen.add_widget(layout)
sm.add_widget(Screen)
return sm
class simplekivy(App):
def build(self):
return LoginScreen()
if __name__ == "__main__":
simplekivy().run()
Thank you for your help !
Your code has the following errors:
The constructor method is called __init__, it is not called __int__.
The constructor method should not return anything
The variables should not be called the same as the classes or functions, I mean the following code: Screen=Screen(name="screen").
It is best to segment is to separate your code, with the name of the class LoginScreen I think you want to make a Screen, so create another class that is the ScreenManager.
Another error is that you have not imported Label.
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
class LoginScreen(Screen):
def __init__(self, *args, **kwargs):
super(LoginScreen,self).__init__(*args, **kwargs)
layout=BoxLayout(orientation='vertical')
self.Username = TextInput(multiline=False)
self.Password = TextInput(multiline=False, password=True)
layout.add_widget(Label(text="username"))
layout.add_widget(self.Username)
layout.add_widget(Label(text="password"))
layout.add_widget(self.Password)
self.add_widget(layout)
class Manager(ScreenManager):
def __init__(self, *args, **kwargs):
super(ScreenManager,self).__init__(*args, **kwargs)
self.add_widget(LoginScreen())
class simplekivy(App):
def build(self):
return Manager()
if __name__ == "__main__":
simplekivy().run()
I wrote this python code to dyanamically create a BoxLayout inside a screen.
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
class ListScreen(Screen):
def __init__(self,**kwargs):
super(ListScreen, self).__init__(**kwargs)
layout = BoxLayout(orientation ='vertical')
top_buttons=BoxLayout()
layout.add_widget(top_buttons)
top_buttons.add_widget(Button(text='Save'))
class ExampleApp(App):
def build(self):
root=ScreenManager()
root.add_widget(ListScreen(name='list'))
return root
ExampleApp().run()
It ran without any compilation error but the output is just a blank sreen.
The problem is that you have not added layout to the ListScreen instance:
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
class ListScreen(Screen):
def __init__(self,**kwargs):
super(ListScreen, self).__init__(**kwargs)
layout = BoxLayout(orientation ='vertical')
self.add_widget(layout) #<<<<<<<<<<<<<<<<<
top_buttons=BoxLayout()
layout.add_widget(top_buttons)
layout.add_widget(Button(text='Save'))
class ExampleApp(App):
def build(self):
root=ListScreen()
root.add_widget(ListScreen(name='list'))
return root
ExampleApp().run()
I am trying to add a widget to a class but it is not working.
Example:
from kivy.uix.button import Button
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
Builder.load_string('''
<test>:
Label:
text: "Hi!"
''')
class test(GridLayout):
def build(self):
pass
testbutton=Button(text="This I want to show on test class!")
test().add_widget(testbutton)
class apprun(App):
def build(self):
return test()
apprun().run()
When run, this does not work.
It is generally good practice to explain how things don't work. For instance, you might have a useful python traceback including information about an exception that was raised, or you might simply mean you don't get the result you expect in which case you should explain how.
In this case, the obvious problem is that you try to inherit from app, when you mean App. Since app doesn't exist this would throw an exception.
You also try to add a widget to a class definition (test) rather than an instance of a class (test()). This will also fail, and if you don't see why you should read about the difference between class definitions and instances.
Also, widgets don't need a build method, this will do nothing.
Is this what you were going for?
from kivy.uix.button import Button
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
Builder.load_string('''
<test>:
Label:
text: "Hi!"
''')
class test(GridLayout):
pass
class apprun(App):
def build(self):
t = test()
b = Button(text="This I want to show on test class!")
t.add_widget(b)
return t
apprun().run()
Or maybe this... so your widgets don't overlap each other.
from kivy.uix.button import Button
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.gridlayout import GridLayout
from kivy.core.window import Window
from kivy.lang import Builder
Builder.load_string('''
<test>:
Label:
text: "Hi!"
''')
class test(GridLayout):
pass
class apprun(App):
def build(self):
t = test(cols=1, size=Window.size)
b=Button(text="This I want to show on test class!")
t.add_widget(b)
return t
apprun().run()