I am new to Kivy and I recently started to create my own application. But there's one problem I have that I can't figure out why it's doing it.
Basically when I open my app and press the button to go to the other screen it does the sliding transition succesfully. But when I open a pop up and close it and hit the same button again, it doesn't do any transition and both screens overlap each other. Why exactly is this the case?
The thing that almost fixed it is using self.manager.current = "second" in the python file but the sliding animation is still not there and it just cuts to the next screen. Or pehaps is there a way to make the sliding animation inside the python file?
I have provided a simple example code to demonstrate the issue. I didn't want to put all of what I've done since it would be too much code.
Any help or suggestions would be appriciated!
Here is the example code
import kivy
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.button import Button,ButtonBehavior
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import Screen, ScreenManager, SlideTransition, CardTransition, FadeTransition
from kivy.uix.popup import Popup,PopupException
from kivy.lang import Builder
class FirstScreen(Screen):
def screen1btn(self):
popup = FloatLayout()
pop = Popup(title="Popup", content=popup, size_hint=(0.6, 0.6))
pop.open()
class SecondScreen(Screen):
pass
class WindowManager(ScreenManager):
pass
class ExampleApp(App):
def build(self):
return WindowManager()
if __name__ == "__main__":
ExampleApp().run()
.kv file
#:import SlideTransition kivy.uix.screenmanager.SlideTransition
<WindowManager>:
FirstScreen:
SecondScreen:
<FirstScreen>:
name: "first"
FloatLayout:
Label:
text: "Screen 1"
font_size: 20
pos_hint: {"x": 0.35,"y": 0.3}
Button:
text: "Popup"
size_hint: 0.3,0.1
pos_hint: {"x": 0.35,"y": 0.4}
on_release:
app.root.transition = SlideTransition(direction = "left")
app.root.current = root.screen1btn()
Button:
text: "Go to second screen"
size_hint: 0.5,0.1
pos_hint: {"x": 0.35,"y": 0.1}
on_release:
app.root.transition = SlideTransition(direction = "right")
app.root.current = "second"
<SecondScreen>:
name: "second"
FloatLayout:
Label:
text: "Screen 2"
font_size: 20
pos_hint: {"x": 0.35,"y": 0.3}
Button:
text: "Go back"
size_hint: 0.5, 0.1
pos_hint: {"x": 0.35,"y": 0.1}
on_release:
app.root.transition = SlideTransition(direction = "left")
app.root.current = "first"
The problem is your 'kv' code for the Popup Button:
Button:
text: "Popup"
size_hint: 0.3,0.1
pos_hint: {"x": 0.35,"y": 0.4}
on_release:
app.root.transition = SlideTransition(direction = "left")
app.root.current = root.screen1btn()
Not sure what that is tying to do, but I believe it should just be opening the Popup, like this:
Button:
text: "Popup"
size_hint: 0.3,0.1
pos_hint: {"x": 0.35,"y": 0.4}
on_release: root.screen1btn()
Note that the two lines referencing app.root have been removed. Now the Button just opens the Popup.
Related
I have this small issue where I'm trying to create a label in LabelScreen when a button in the MenuScreen is pressed. The closest I managed to do was making the Label appear in the same screen as the button.
The following code is just an simple example of what I would like to acheive. Any help is always appreciated!
.py
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.label import Label
from kivy.uix.button import Button,ButtonBehavior
from kivy.uix.image import Image
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.popup import Popup
from kivy.core.window import Window
from kivy.properties import ObjectProperty, NumericProperty
class MenuScreen(Screen):
def Item1(self,button):
App.get_running_app().cart += 5
label1 = Label(text="Item 1: $5", font_size=25)
self.add_widget(label1)
class LabelScreen(Screen):
pass
class WindowManager(ScreenManager):
pass
class ExampleApp(App):
cart = NumericProperty(0)
def build(self):
return WindowManager()
if __name__ == "__main__":
ExampleApp().run()
.kv
#:import SlideTransition kivy.uix.screenmanager.SlideTransition
<WindowManager>:
MenuScreen:
LabelScreen:
<MenuScreen>:
name: "menu"
FloatLayout:
Button:
text: "Add to cart"
size_hint: 0.5,0.05
pos_hint: {"x": 0.25, "y": 0.25}
on_release:
root.Item1(self)
Label:
text: "Cart: " + str(app.cart)
font_size: 25
pos_hint: {"x": 0.25, "y": 0.25}
Button:
text: "Label Screen"
size_hint: 0.5,0.05
pos_hint: {"x": 0.25, "y": 0.1}
on_release:
app.root.transition = SlideTransition(direction = "left")
app.root.current = "label"
<LabelScreen>:
name: "label"
FloatLayout:
Label:
text: "I want Item1 Label to appear here \n instead of in the MenuScreen"
font_size: 25
pos_hint: {"x": 0, "y": 0.25}
Button:
text: "Back"
size_hint: 0.5,0.05
pos_hint: {"x": 0.25, "y": 0.1}
on_release:
app.root.transition = SlideTransition(direction = "right")
app.root.current = "menu"
You have to reference the right screen window you want to add the wiget.
Since you are adding the widget whitin the python class first we have to get the app running, then we can navigate to the screen we want:
def Item1(self,button):
App.get_running_app().cart += 5
label1 = Label(text="Item 1: $5", font_size=25)
app_running = App.get_running_app()
root_screen = app_running.root
label_screen = root_scren.get_screen('label')
label_screen.add_widget(label1)
# Or simply:
# App.get_running_app().root.get_screen('label').add_widget(label1)
I am trying to incorporate ads into a Kivy app I made, and to do this, I want to have one class (with the ads) in a FloatLayout to be at the top of my screen, and the rest of the app to be below, in a separate class.
I was trying to get this to work with some test code (simple .py and .kv file that has multiple screens and classes and organizes accordingly). The code is supposed to have two float layouts: One has text, the other has a button that you press and it takes you to the next screen. However the issue I am having is that I can't position the button correctly, as it appears that the widget is shrunk in the bottom left corner. It is supposed to be next to the text box.
Here is my .kv file:
WindowManager:
Screen1:
Screen2:
<Screen1>:
name: "screen1"
FloatLayout:
Label:
pos_hint: {'top': 1, "center_x": 0.5}
size_hint: (0.2, 0.5)
font_size: 40
text: "TEXT AT TOP OF SCREEN"
FloatLayout:
TextInput:
pos_hint: {"x": 0.1, "y": 0.05}
size_hint: (0.3, 0.05)
multline:False
GoS:
FloatLayout:
Button:
text: "PRESS TO GO TO SCREEN 2"
pos_hint: {"right": 0.5, "center_y": 0.7}
on_press: widget.goscreen()
<Screen2>:
name: "screen2"
Label:
text: "YOU ARE ON SCREEN TWO"
and here is the .py file:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import ScreenManager, Screen
class Screen1(Screen):
pass
class GoS(Widget):
def goscreen(self):
self.parent.current = "screen2"
class Screen2(Screen):
pass
class WindowManager(ScreenManager):
pass
kv = Builder.load_file("rec_view.kv")
class TestApp(App):
def build(self):
return kv
if __name__ == "__main__":
TestApp().run()
Why is this happening?
Another small point also is that my button doesn't work because I can't seem to call the correct class. If I use "root.goscreen()", it doesn't work as my root widget doesn't have this function. What should be the correct syntax here?
I recommend you to use BoxLayout to divide your GUI.
Your button don't work because you don't link it to a valid Widget.
The GoS widget must be define in KV or be imported.
Here is a proposition for your rec_view.kv file :
WindowManager:
Screen1:
Screen2:
<GoS>:
Button:
text: "PRESS TO GO TO SCREEN 2"
on_press: root.goscreen()
<Screen1>:
name: "screen1"
BoxLayout:
orientation: "vertical"
BoxLayout:
orientation: "horizontal"
size_hint_y: 0.5 # Proportion of screen height that the widget occupe
Label:
font_size: 40
text: "TEXT AT TOP OF SCREEN"
BoxLayout:
orientation: "horizontal"
size_hint_y: 0.3
TextInput:
multline:False
BoxLayout:
orientation: "horizontal"
size_hint_y: 0.2
GoS:
<Screen2>:
name: "screen2"
Label:
text: "YOU ARE ON SCREEN TWO"
The definition of goscreen is not correct, GoS is not a Screen, so his parent don't have current , use instead App.get_running_app().root.current = "screen2".
I don't know what you want to do and why you define the GoS class, but you can avoid it. By moving the goscreen definition into Screen1 class, and replace the 3rd BoxLayout of Screen1 with this:
BoxLayout:
orientation: "horizontal"
size_hint_y: 0.2
Button:
text: "PRESS TO GO TO SCREEN 2"
on_press: root.goscreen()
I'm having trouble dynamically adding widgets in my kv file, everything I've seen so far has been around adding widgets in the py file. The reason I'd like to use the kv file for this is that I've got a modified Button widget I'd like to use that's written in the kv file and I don't know how to create it in the py file.
So I guess there are two possible questions:
1) How do I use add_widget correctly in the kv file. In the below code I get the NameError: SmoothButton is not defined.
Or
2) How do I create my SmoothButton in the py file?
kv file:
WindowManager:
ChatPage:
<ChatPage>:
name: "chat_page"
NavigationLayout:
id: nav_layout
MDNavigationDrawer:
NavigationDrawerIconButton:
text: "Test"
on_release: app.root.current = "login"
FloatLayout:
MDToolbar:
pos_hint: {'top': 1}
md_bg_color: 0.2, 0.6, 1, 1
left_action_items: [['menu', lambda x: root.ids.nav_layout.toggle_nav_drawer()]]
Button:
text: "Create button"
pos_hint: {"x": 0.65, "top": 0.15}
size_hint: 0.35, 0.15
on_release: root.add_widget(SmoothButton)
<SmoothButton#Button>:
background_color: 0,0,0,0
background_normal: ""
back_color: 1,0,1,1
border_radius: [6]
canvas.before:
Color:
rgba: self.back_color
RoundedRectangle:
size: self.size
pos: self.pos
radius: self.border_radius
py file:
import kivy
from kivy.app import App
from kivy.properties import ObjectProperty, StringProperty, NumericProperty, ListProperty
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.core.window import Window
from kivymd.theming import ThemeManager
import mysql.connector
from kivymd.uix.picker import MDDatePicker
class ChatPage(Screen):
pass
class WindowManager(ScreenManager):
pass
class MyApp(App):
theme_cls = ThemeManager()
def build(self):
kv = Builder.load_file("kivy.kv")
sm = WindowManager()
screens = [ChatPage(name="chat_page")]
for screen in screens:
sm.add_widget(screen)
sm.current = "chat_page"
return sm
if __name__ == '__main__':
MyApp().run()
Thanks
If you want to add modified widgets in the .kv it doesn't mean you have to add it there. The solution is to expose the modified widget in the .kv to the .py, and create a method in "ChatPage" where you add the widget:
*.py
# ...
class SmoothButton(Button):
pass
class ChatPage(Screen):
def on_released(self):
self.add_widget(SmoothButton())
# ...
*.kv
# ...
<ChatPage>:
name: "chat_page"
NavigationLayout:
id: nav_layout
MDNavigationDrawer:
NavigationDrawerIconButton:
text: "Test"
on_release: app.root.current = "login"
FloatLayout:
MDToolbar:
pos_hint: {'top': 1}
md_bg_color: 0.2, 0.6, 1, 1
left_action_items: [['menu', lambda x: root.ids.nav_layout.toggle_nav_drawer()]]
Button:
text: "Create button"
pos_hint: {"x": 0.65, "top": 0.15}
size_hint: 0.35, 0.15
on_release: root.on_released() # <---
<SmoothButton>: # <---
background_color: 0,0,0,0
# ...
I have a app in kivy with a screen manager and a popup within it. The popup works until the point I put a button with the close function into the popup window. At this point i get the message:
PopupException: Popup can have only one widget as content
There is another post on this topic but it does not seem to work.
The python code
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.lang import Builder
from kivy.uix.popup import Popup
class CustomPopup(Popup):
pass
class MainScreen(Screen):
pass
class ContentScreen(Screen):
def open_popup(self):
the_popup = CustomPopup()
the_popup.open()
class ScreenManagement(ScreenManager):
pass
presentation = Builder.load_file("am.kv")
class AMApp(App):
def build(self):
return presentation
if __name__ == "__main__":
AMApp().run()
The kivy file is below. The issue seems to come in the button function when calling the custompop
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManagement:
transition: FadeTransition()
MainScreen:
ContentScreen:
<CustomPopup>:
size_hint: .5 , .5
auto_dismiss: False
title: "The Popup"
Button:
text: "Close"
on_press: root.dismiss()
<MainScreen>:
name: "Welcome"
Button:
text: "First Screen"
size_hint: 1, .5
font_size: 40
pos_hint: {'center_x': 0.5, 'center_y': 0.7}
on_release: app.root.current = "other"
Button:
text: 'Welcome Mr and Mrs Shaw'
size_hint: 1, .5
font_size: 25
pos_hint: {'center_x': 0.5, 'center_y': 0.3}
on_release: app.root.current = "other"
<ContentScreen>:
name: "other"
BoxLayout:
orientation: "vertical"
size_hint_x: .22
Button:
text: "open Popup"
on_press: root.open_popup()
The code posted above, runs fine on Linux Buster, Kivy 1.11.0-dev and 1.10.1, and Python 3.7.3rc1
Solution
Try adding a layout e.g. BoxLayout into CustomPopup to solve the PopupException.
Example
The following example illustrates a popup window with a message and a button.
Snippets
<CustomPopup>:
size_hint: .5 , .5
auto_dismiss: False
title: "The Popup"
BoxLayout:
orientation: 'vertical'
Label:
text: "Hello Kivy"
Button:
text: "Close"
on_press: root.dismiss()
I am trying create a GUI by implementing the template of the ComicCreator GUI sample as a template for my own project. The code is easy to follow, but I would like to be able to reconfigure the drawingspace.kv, each time a button is pushed, say for example something like this:
Q: How could I configure the drawingspace.kv to have a different layout with different widgets for each button that is pressed?
A neat way to do this is to use screen.
Since I allready have an example of this app from you earlier question, it was easy to implement the screens, and rewrite the classes a bit.
When a button is pressed, you set the screenmanager's current to whatever the name you named the screen you want.
Then you just edit the layouts as you want inside of each screen, in the kv file, or python file.
I choose to make most of the layout stuff in kv language here. Because I find it easier to develop a layout the way I want it this way.
Later I could rewrite it to python if I want that.
So my python file looks like this now:
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty
from kivy.clock import Clock
from kivy.uix.screenmanager import Screen,ScreenManager,NoTransition
from kivy.lang import Builder
import time
Builder.load_file("kv.kv")
class MyLayout(BoxLayout):
def __init__(self,**kwargs):
super(MyLayout,self).__init__(**kwargs)
self.orientation = "vertical"
self.padding = 10
class MainScreen(Screen):
pass
class RemoveScreen(Screen):
pass
class GroupScreen(Screen):
pass
class MyLogo(BoxLayout):
your_time = StringProperty()
def __init__(self,**kwargs):
super(MyLogo,self).__init__(**kwargs)
Clock.schedule_interval(self.set_time, 0.1)
def set_time(self,dt):
self.your_time = time.strftime("%m/%d/%Y %H:%M")
class MyApp(App):
def __init__(self,**kwargs):
super(MyApp,self).__init__(**kwargs)
self.sm = ScreenManager(transition=NoTransition())
self.sm.add_widget(MainScreen(name = "main"))
self.sm.add_widget(RemoveScreen(name = "remove"))
self.sm.add_widget(GroupScreen(name = "group"))
self.sm.current = "main"
def build(self):
return self.sm
if __name__ == "__main__":
MyApp().run()
And kv.kv file looks like this:
#:kivy 1.9.1
<MyButtons#BoxLayout>:
padding: 10,10,10,0
spacing: 10
size_hint: 1,0.3
orientation: "horizontal"
Button:
text: "Clear"
on_press: app.sm.current = "main"
Button:
text: "Remove"
on_press: app.sm.current = "remove"
Button:
text: "Group"
on_press: app.sm.current = "group"
Button:
text: "Color"
Button:
text: "Gestures"
<MyLogo>:
spacing: 10
padding: 10,10,10,0
orientation: "horizontal"
BoxLayout:
orientation: "vertical"
size_hint: 0.3,1
canvas:
Rectangle:
pos: self.pos
size: self.size
AsyncImage
source: 'http://lmsotfy.com/so.png'
Label:
size_hint: 1,0.3
text: root.your_time
color: [0,0,0,1]
Label:
size_hint: 1,0.3
text: "NYC, New York, USA"
color: [0,0,0,1]
<MainScreen>:
MyLayout:
MyLogo:
#Button:
# text: "main"
MyButtons:
#buttons
BoxLayout:
padding: 10,10,10,10
size_hint: 1,0.3
Button:
text: "Total figures: 1 Kivy Started"
<RemoveScreen>:
MyLayout:
MyLogo:
BoxLayout:
orientation: "horizontal"
Label:
font_size: "40sp"
text: "Remove"
Button:
font_size: "20sp"
text: "Remove this or something"
MyButtons:
#buttons
BoxLayout:
padding: 10,10,10,10
size_hint: 1,0.3
Button:
text: "Total figures: 1 Kivy Started"
<GroupScreen>:
MyLayout:
MyLogo:
BoxLayout:
orientation: "vertical"
Label:
font_size: "40sp"
text: "Group"
Button:
font_size: "20sp"
text: "Something groups stuff"
MyButtons:
#buttons
BoxLayout:
padding: 10,10,10,10
size_hint: 1,0.3
Button:
text: "Total figures: 1 Kivy Started"
The layout frame should be a screen manager, and each layout a screen. Screen transitions would be then triggered by pressing the buttons. You can also watch a tutorial here if you don't know how to do this, but the documentation should be enough.