cannot display progress bar in window - python

I'm new to kivy so I don't really know how to display a progress bar in my NaviWindow. I cant seem to put progress bar inside the kv file so anyone knows how to display the bar in the NaviWindow?
.pyfile
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.progressbar import ProgressBar
from kivy.uix.screenmanager import ScreenManager, Screen
KV = """
WindowManager:
NaviWindow:
<NaviWindow>:
"""
class WindowManager(ScreenManager):
pass
class NaviWindow(Screen):
def build(self):
Progress = ProgressBar(max=1000)
Progress.value = 100
return Progress
class MyMainApp(App):
def build(self):
return Builder.load_string(KV)
if __name__ == "__main__":
MyMainApp().run()

Only the App class (and obviously the classes that inherit from App) has the build method, but you seem to think that the Screen method also has it and is clearly incorrect. The solution is to add the ProgressBar using the add_widget() method:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.progressbar import ProgressBar
from kivy.uix.screenmanager import ScreenManager, Screen
KV = """
WindowManager:
NaviWindow:
<NaviWindow>:
"""
class WindowManager(ScreenManager):
pass
class NaviWindow(Screen):
def __init__(self, **kwargs):
super(NaviWindow, self).__init__(**kwargs)
self.progress = ProgressBar(max=1000)
self.progress.value = 100
self.add_widget(self.progress)
class MyMainApp(App):
def build(self):
return Builder.load_string(KV)
if __name__ == "__main__":
MyMainApp().run()

Related

How to fix [CRITICAL] App.root must be an _instance_ of Widget

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()

kivy doesn't show the window

after I write a code to show a window with text "name" and a dialog box, I got no error. However for me window doesn't show. and I got no error. does anyone any Idea about that?
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(multilines=False)
self.add_widget(self.name)
class MyApp(App):
def build(self):
return MyGrid()
if __name__ == "__name__":
MyApp().run()

Blank screen with kivy

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()

How to dynamically create layouts inside screen in kivy?

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()

Adding widgets to a screen subclass in .py file

I am using a screen manager and would like to add widgets to the screen subclass without using the .kv file.
class MainMenu(Screen):
def __init__(self, **kwargs):
gLayout = GridLayout()
gLayout.add_widget(Button(text = 'test'))
class Sis(App):
def build(self):
root = ScreenManager()
root.add_widget(MainMenu(name = 'mainMenu'))
root.current = 'mainMenu'
return root
Sis().run()
When I try to run the above code I get (pygame parachute) Segmentation Fault.
If I create the layout in the .kv file it works fine.
I've tried fiddling around with on_pre_enter and on_enter but I'm pretty sure I was using them wrong.
Any help is appreciated.
You forgot calling parent constructor of MainMenu class:
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
class MainMenu(Screen):
def __init__(self, **kwargs):
super(MainMenu, self).__init__(**kwargs)
self.add_widget(Button(text = 'test'))
class Sis(App):
def build(self):
root = ScreenManager()
root.add_widget(MainMenu(name = 'mainMenu'))
root.current = 'mainMenu'
return root
Sis().run()

Categories

Resources