self.add_widget(ViewGridLayout()) error?

50 views
Skip to first unread message

Erin Biskup

unread,
Apr 19, 2017, 12:09:25 AM4/19/17
to Kivy users support
So I'm doing some Kivy tutorials that my teacher set me, but I keep getting a syntax error on self.add_widget(ViewGridLayout()). The tutorials are all pretty much just copy and pasting the code from a word doc and figuring out by ourselves what code does what so it should be working. Here are the full .py and .kv codes:

.kv
#: import NoTransition kivy.uix.screenmanager.NoTransition

ScreenManagement:
    transition: NoTransition()
    MainScreen:
    ViewScreen:


<MainScreen>:
name: 'main'
BoxLayout:
orientation: 'vertical'
Label:
text: 'My Application'
size_hint: 1, 0.1
GridLayout:
size_hint: 1, 0.5
cols: 2
Label:
text: 'Name'
TextInput:
BoxLayout:
size_hint: 1, 0.2
Button:
text: 'Add'
Button:
text: 'Delete'
Button:
text: 'View'
on_press: app.root.current = 'view'

Label:
size_hint: 1, 0.2
text: 'Debug'

<ViewScreen>:
name: 'view'


.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout

class MainScreen(Screen):
pass

class ViewScreen(Screen):
def btn_pressed(self, *args):
presentation.current='main'

def __init__(self, **kwargs):
super(ViewScreen, self).__init__(**kwargs)
self.add_widget(ViewBoxLayout())


class ViewBoxLayout(BoxLayout):
def btn_pressed(self, *args):
presentation.current='main'
def __init__(self, **kwargs):
super(ViewBoxLayout, self).__init__(**kwargs)
self.orientation = 'vertical'
back_btn = Button(text='Back')
back_btn.bind(on_press=self.btn_pressed)
self.add_widget(Label(text='My Application')    
self.add_widget(ViewGridLayout())              #This is where the error is occurring
self.add_widget(back_btn)


class ViewGridLayout(GridLayout):
def __init__(self, **kwargs):
super(ViewGridLayout, self).__init__(**kwargs)
self.cols = 2

class ScreenManagement(ScreenManager):
pass

class MainApp(App):
    
def build(self):
return presentation

presentation = Builder.load_file("main2.kv")
MainApp().run()


Any ideas what could be going wrong?
Thanks!

Fari Tigar

unread,
Apr 19, 2017, 3:26:14 AM4/19/17
to Kivy users support
A bracket is missing in this line:
self.add_widget(Label(text='My Application'))

As a beginner, do yourself a favor and work with an IDE, either Pycharm or Eclipse, you would have found the error in no time.
And if you really want to impress your teacher, user kivymd, which is a library of nicer Widgets.

Fari Tigar

unread,
Apr 19, 2017, 3:27:47 AM4/19/17
to Kivy users support
Sorry, the correct name is parentheses, you might ques that already.

OceanArrow

unread,
Apr 27, 2017, 9:30:16 PM4/27/17
to Kivy users support
Thanks! This helped me heaps! :)

Geekademy

unread,
Apr 28, 2017, 12:03:18 PM4/28/17
to kivy-...@googlegroups.com
Not sure this is always the best advice to be honest. Those huge environments
have correspondingly high learning curves, even if they do help once learned.

The situation described is one of the things that every programmer must
discover, however. If you have an error and can't figure out why it is
happening, look a line or two before it. Error messages are often wrong as
well, so reading between the lines is sometimes necessary. It's something that
is a bit more obvious when learning C due to its nature (multiple compiler
errors and use of braces), but can happen in any language.

Personally I use a lightweight/fast editor that does simple bracket/paren
matching, and as I'm familiar with the rule above, do not encounter such problems.

Cheers,
Reply all
Reply to author
Forward
0 new messages