I have simple code for kivy, on W10 runs without problem. It falls down during loading in kivy launcher. Problem is without message.
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
class View(BoxLayout):
def __init__(self):
super().__init__()
self.text = "No text"
but = Button(text = "Press",on_press = self.show)
self.add_widget(but)
self.lbl = Label()
self.add_widget(self.lbl)
def show(self,obj):
self.lbl.text = self.text
pass
class MyPaintApp(App):
def build(self):
return View()
if __name__ == '__main__':
MyPaintApp().run()
It does not run because you call super wrong.
As kivy launcher uses python 2, you need to pass your class (View) and the instance (self) to super.
You need to edit your class like this:
class View(BoxLayout):
def __init__(self,**kwargs):
super(View,self).__init__(**kwargs)
in every failure in kivy launcher, there is a '.kivy/log' directory inside the project directory that has a full log. you could find all the problem there.
Related
I am a beginner in Python and Kivy. I want to create a simple program that should have an exit button on a window that must exit the app when pressed. Hi there, please help me. It's a request, please keep the code simple for a beginner. ^_^
Okay, I don't know how your code is, but this code could be copy-pasted as is and it will work (just import the necessary modules from kivy though):
.py
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
class yourscreen(FloatLayout):
def __init__(self, **kwargs):
#NEEDED
super(yourscreen, self).__init__(**kwargs)
self.button = Button(
text='exit',
size_hint=(0.5, 0.2)
)
self.button.bind(on_release= lambda x:self.exit())
self.add_widget(self.button)
def exit(self):
return MyApp().stop()
class MyApp(App):
def build(self):
return yourscreen()
if __name__ == '__main__':
MyApp().run()
You could pull out what you need which is MyApp().stop()
getting the blank screen after running this code what is the problem
after adding .kv file everything is working fine and after removing .kv file and using the only python to show the layout it is not working
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
class BoxLayoutExample(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
b1 = Button(text="A")
b2 = Button(text="B")
b3 = Button(text="C")
self.clear_widgets()
self.add_widget(b1)
self.add_widget(b2)
self.add_widget(b3)
class MainWidget(Widget):
pass
class TheLabApp(App):
pass
TheLabApp().run()
This the output:)
Image
If you are not using a kv file named thelab.kv, then your App has no way of knowing what you want it to look like. You will at least need to add a build() method to your TheLabApp class that returns BoxLayoutExample().
I want to ask the user for a number, then display that amount of widgets.
This is how I do it:
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
class MainApp(App):
def build(self):
return List()
class List(GridLayout):
def __init__(self, **kwargs):
super(List, self).__init__(**kwargs)
self.cols = 1
user_input = 3 # Just an example
for i in range(user_input):
label = Label(text="Widget number {}:".format(i + 1))
self.add_widget(label)
if __name__ == "__main__":
app = MainApp()
app.run()
The problem is: the point of "kivy language" is to keep the logic in the .py file, and the design in the .kv file.
Is there any way I can keep the design only in .kv file, while doing this (having a for loop)?
(I'm new to kivy, sorry if I'm asking a simple question. :) )
Definitely, you can.
The KV file is used primarily to design the looks while the login part of a program is handled by the PY file.
.kv file
<classname>:
Button:
text:'click me'
on_release: root.function_to_call_from_py_file()
.py file
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
class List(GridLayout):
def function_to_call_from_py_file(self):
print('This function is called')
class MainApp(App):
def build(self):
return List()
if __name__ == "__main__":
app = MainApp()
app.run()
I implemented pywebview on kivy. After clicking the button it will create the window, but after closing the window and click the button again, the window did not created.
How can I solve this problem ?
Below is my code :
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
import threading
import webview
class LoginScreen(BoxLayout):
def __init__(self, **kwargs):
super(LoginScreen, self).__init__(**kwargs)
self.btn1 = self.add_widget(Button(text='Web',on_press=self.on_web))
def on_web(self,instance):
url='http://www.google.com'
print("Im open windows")
webview.create_window('My Web App', url=url,debug=True)
class MyApp(App):
def build(self):
return LoginScreen()
if __name__ == '__main__':
MyApp().run()
In the following lines, remove the debug=True from the webview.create_window. Then add webview.start(debug=True) per https://github.com/r0x0r/pywebview and https://pywebview.flowrl.com/examples/debug.html
webview.create_window('My Web App', url=url)
webview.start(debug=True)
Also, the following are the params for the create_window function, notice that there is no debug per https://pypi.org/project/pywebview/0.5/:
webview.create_window(title, url, width=800, height=600, resizable=True, fullscreen=False)
The above works for me (Python 3, Kivy 1.11, Windows10) after the edit and test of your code.
Let's say I define on the fly in Kivy a few widgets (buttons) and dynamically assign their id.
I'm not using kv language in this use case.
I can keep a reference of a widget id without keeping track of the widget itself : then I'd like to access the widget through its id.
Can I do something like "get widget by id" ?
(If I had defined the widget in a kv file, I could have used self.ids.the_widget_id to access the widget itself through its id)
Kivy widgets make tree structure. Children of any widget are avaiable through children atribute. If you want, you can keep reference only to root window and then iterate over it's widgets using walk method:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
class MyWidget(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
button = Button(text="...", id="1")
button.bind(on_release=self.print_label)
l1 = BoxLayout(id="2")
l2 = BoxLayout(id="3")
self.add_widget(l1)
l1.add_widget(l2)
l2.add_widget(button)
def print_label(self, *args):
for widget in self.walk():
print("{} -> {}".format(widget, widget.id))
class MyApp(App):
def build(self):
return MyWidget()
if __name__ == '__main__':
MyApp().run()
walk() and walk_reverse() method were added to kivy.uix.widget.Widget in 1.8.1 version of Kivy. For older versions you need to recursively parse tree yourself:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
class MyWidget(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
button = Button(text="...", id="1")
button.bind(on_release=self.print_label)
l1 = BoxLayout(id="2")
l2 = BoxLayout(id="3")
self.add_widget(l1)
l1.add_widget(l2)
l2.add_widget(button)
def print_label(self, *args):
children = self.children[:]
while children:
child = children.pop()
print("{} -> {}".format(child, child.id))
children.extend(child.children)
class MyApp(App):
def build(self):
return MyWidget()
if __name__ == '__main__':
MyApp().run()
You can retrieve the widget using directly the ids. For example in your code you can modify the Button text with the following snippet:
self.ids.2.ids.3.ids.1.text = '!!!!'
You can change properties of each widget using ids:
self.ids['order_number'].text='S1212'