kivy app gives error "invalid data afet declaration"

131 views
Skip to first unread message

Mohit Varshney 139

unread,
Oct 28, 2023, 12:02:52 PM10/28/23
to Kivy users support
im new to kivy world
i wrote a simple application that showes a button and a label
but it gives me an error "invalid data after declaration
here i am attaching screenshort of my code please anyone can help me in this


Screenshot 2023-10-28 213222.png
Screenshot 2023-10-28 213132.png

Elliot Garbus

unread,
Oct 28, 2023, 1:54:22 PM10/28/23
to kivy-...@googlegroups.com
Please share code, not pictures. To retain formatting right-click and select paste as plain-text. 

There are changes I’d suggest. 
If you’re going to define a __init__ in kivy, you must pass the keyword arguments. 

def __init__(self, **kwargs):
    super().__init__(**kwargs)

In your case you can just delete the code, you are not executing any of you own code. 

Also in the kv code remove the redundant parentheses around orientation: “vertical“

The Labels and Buttons must be indented under the Layout. 


Sent from my iPhone

On Oct 28, 2023, at 9:03 AM, Mohit Varshney 139 <hello....@gmail.com> wrote:

im new to kivy world

i wrote a simple application that showes a button and a label
but it gives me an error "invalid data after declaration
here i am attaching screenshort of my code please anyone can help me in this


--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/ddd339aa-c6cc-47ad-a235-6197542153f1n%40googlegroups.com.
<Screenshot 2023-10-28 213222.png>
<Screenshot 2023-10-28 213132.png>

Mohit Varshney 139

unread,
Oct 28, 2023, 1:56:51 PM10/28/23
to kivy-...@googlegroups.com
kivy code
<main_root>:
    random_pass : random_pass
    BoxLayout:
    # orientation:'vertical'
   
    Label:
        text:"passsword generator"
        font_size: 60
        color:0.92,0.45,0
    Label:
        id: random_pass
        text:"-"    
        font_size:60
    Button:
        text:"generate"
        font_size:30
        size:75,40  
        on_press : root.generate_pass()

Mohit Varshney 139

unread,
Oct 28, 2023, 1:58:27 PM10/28/23
to kivy-...@googlegroups.com
python code
import kivy
from kivy.app import App
from kivy.uix.layout import Layout
from kivy.uix.boxlayout import BoxLayout
from tkinter import *
import random, string
import pyperclip as pc


# from kivy.uix.main_root import main_root
import random

class main_root(BoxLayout):
   def __init__(self , **kwargs):
      super(main_root,self).__init__()

def generate_pass(self):
             self.random_pass.text = str(random.randint(0,100))









   


class password_generator(App):
    def build(self):
        return main_root()

password_generator = password_generator()    
password_generator.run()  
   

elli...@cox.net

unread,
Oct 28, 2023, 6:01:09 PM10/28/23
to kivy-...@googlegroups.com

Here are the fixes.  Let me know if this works for you, happy to answer any questions.

In the python code,

Class names changed to CapCase

generate_pass() is indented so that it is a method of MainRoot.

__Init__() method is removed.  If you are going to use the __init__ method you want to pass the keywords args to the parent class

 

    def __init__(self, **kwargs):

        super().__init__(**kwargs)

 

In the kv code:

File name is changed to passwordgenerator to match new App class name.

Uncommented orientation

Indented the widgets so they are under the BoxLayout.

Sized the Button.  Note to size a widget you must turn off the hint by setting it to None.

 

 

Python Code:

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout

import random


class MainRoot(BoxLayout):  # python convention classes are in CapCase
    # def __init__(self, **kwargs):
    #     super(main_root, self).__init__() # change to super().__init__(**kwargs)

   
def generate_pass(self):
       
self.random_pass.text = str(random.randint(0, 100))


class PasswordGenerator(App):  # python convention classes are in CapCase
   
def build(self):
       
return MainRoot()


password_generator = PasswordGenerator()
password_generator.run()

passwordgenerator.kv # note the file name has changed.

<MainRoot>:
    random_pass : random_pass
   
BoxLayout:
       
orientation:'vertical'
       
Label:
           
text:"passsword generator"
           
font_size: 60
           
color:0.92,0.45,0
       
Label:
           
id: random_pass
           
text:"-"
           
font_size:60
       
Button:
           
text:"generate"
           
size_hint_y: None
           
height: dp(48)
           
font_size:30
#            size:75,40  Must set size_hint to None to set size
           
on_press : root.generate_pass()

Reply all
Reply to author
Forward
0 new messages