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 labelbut it gives me an error "invalid data after declarationhere 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>
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/268CDA07-7476-4A09-A380-C3DC073E2E01%40cox.net.
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()
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAJuAB8dWtaXGYGhE14bDEBP9prTY2QRJ_Kuss9ZsFpe6otnVow%40mail.gmail.com.