Kivy Screenmanager Issues - - python

I am currently struggling with Kivy's screenmanager function. While it works in the menus it just won't show the character. I didn't find any helpful Information in the Documentation.
This works just fine so that class isn't the problem:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.image import Image
from kivy.core.window import Window
class Character(Widget):
pass
class Basic(Image):
def __init__(self, **kwargs):
super(Basic, self).__init__(**kwargs)
self._keyboard = Window.request_keyboard(None, self)
if not self._keyboard:
return
self._keyboard.bind(on_key_down=self.on_keyboard_down)
print('Basic initialised')
def on_keyboard_down(self, keyboard, keycode, text, modifiers):
if keycode[1] == 'left':
self.x -= 10
print('pressed left')
elif keycode[1] == 'right':
self.x += 10
print('pressed right')
elif keycode[1] == 'up':
self.y += 10
print('pressed up')
elif keycode[1] == 'down':
self.y -= 10
print('pressed down')
else:
return False
return True
class HemeApp(App):
def build(self):
wimg = Basic(source='sprite.png')
m = Character()
m.add_widget(wimg)
return m
if __name__ == '__main__':
HemeApp().run()
But when I want to display that on my GameScreen from ScreenManager it's just a Blackscreen.
This is the code:
from kivy.app import App
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.config import Config
from kivy.vector import Vector
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.uix.image import Image
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.core.window import Window
from kivy.animation import Animation
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.screenmanager import ScreenManager, Screen
Builder.load_file('heme.kv')
##############################################################################################################################
class HemeApp(App):
title = 'geme'
icon = 'custoicon.png'
def build(self):
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(SettingsScreen(name='settings'))
sm.add_widget(CharacterCreationScreen(name='creationscreen'))
sm.add_widget(GameScreen(name='gameplay'))
return sm
def save(self, plrname):
fob = open('stats.txt', 'w+')
fob.write(plrname + "\n")
fob.close()
class MenuScreen(Screen):
pass
class SettingsScreen(Screen):
pass
class CharacterCreationScreen(Screen):
pass
class Character(Widget):
pass
class GameScreen(Screen):
def playy(self):
print ('playy fired')
wimg = Basic(source='sprite.png')
m = Character()
m.add_widget(wimg)
return m
def on_enter(self):
print ("on_enter fired")
self.playy()
class MyButton(ButtonBehavior, Image):
def __init__(self, **kwargs):
super(MyButton, self).__init__(**kwargs)
self.source = 'button.png'
def on_press(self):
self.source = 'button_pressed.png'
def on_release(self):
self.source = 'button.png'
class Basic(Image):
def __init__(self, **kwargs):
super(Basic, self).__init__(**kwargs)
self._keyboard = Window.request_keyboard(None, self)
if not self._keyboard:
return
self._keyboard.bind(on_key_down=self.on_keyboard_down)
print('Basic initialised')
def on_keyboard_down(self, keyboard, keycode, text, modifiers):
print ('on_keyboard_down active')
if keycode[1] == 'left':
self.x -= 10
elif keycode[1] == 'right':
self.x += 10
else:
return False
return True
if __name__ == '__main__':
HemeApp().run()
Plus the Kivy code:
#; kivy 1.10.0
#:import Factory kivy.factory.Factory
<MenuScreen>:
FloatLayout:
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: 'bg.png'
MyButton:
pos: (330, 400)
allow_stretch: True
background_color: (0,1,0,1)
size_hint: (.2,.2)
text: 'Play'
on_release:
root.manager.transition.direction = 'right'
root.manager.current = 'creationscreen'
Label:
pos: (330, 450)
text: 'Play'
color: (1,0,0)
MyButton:
pos: (330, 300)
size_hint: (.2,.2)
text: 'Goto settings'
on_release:
root.manager.transition.direction = 'right'
root.manager.current = 'settings'
MyButton:
pos: (330, 200)
background_color: (1,0,0,1)
size_hint: (.2,.2)
text: 'Quit'
on_release: exit()
<SettingsScreen>:
FloatLayout:
MyButton:
pos: (330, 400)
size_hint: (.2,.2)
text: 'My settings button'
MyButton:
pos: (330, 300)
size_hint: (.2,.2)
text: 'Back to menu'
on_release:
root.manager.transition.direction = 'left'
root.manager.current = 'menu'
<CharacterCreationScreen>:
name: name_input
FloatLayout:
MyButton:
pos: (330, 400)
name: name_input
size_hint: (.2,.2)
text: 'Start game'
on_release:
root.manager.transition.direction = 'right'
root.manager.current = 'gameplay'
MyButton:
pos: (430, 300)
text: 'Save'
on_release: app.save(name_input.text)
size_hint: (.2,.2)
TextInput:
pos: (230, 300)
id: name_input
font_size: 20
multiline: False
size_hint: (.2,.2)
MyButton:
pos: (330, 200)
size_hint: (.2,.2)
text: 'Back to menu'
on_release:
root.manager.transition.direction = 'left'
root.manager.current = 'menu'
<GameScreen>:
game: game
Character:
id: game
pos: self.pos

Problem
The character was not displayed because no location was given to add the character.
Solution
Replaced return m with self.add_widget(m)
Snippet
class GameScreen(Screen):
def playy(self):
print('playy fired')
wimg = Basic(source='sprite.png')
m = Character()
m.add_widget(wimg)
self.add_widget(m)
Output

Related

How to share input between popup and screen in Kivy?

I would like to "send" input2 from class Create to class(screen) All. For example I could method which add label with text = input2. How to do it?
My .py:
from kivy.app import App
import sys
from kivy.clock import Clock
from kivy.core.clipboard import Clipboard
from tinydb import TinyDB, Query
from kivy.storage.jsonstore import JsonStore
from kivy.graphics import RoundedRectangle, Color
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.floatlayout import FloatLayout
from kivy.lang.builder import Builder
from kivy.properties import ObjectProperty, StringProperty
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.core.window import Window
Window.size=(400,600)
Window.clearcolor = (214/255, 201/255, 78/255, 1)
#TODO:JSONSTORE,LAYOUT,IF TIME:GRAPHICAL NOTES
#FIX SIZE_HINT AND POS_HINT
class MainWindow(Screen, FloatLayout):
def submit(self):
self.manager.current = "all"
class Create(Popup):
input1 = ObjectProperty(None)
input2 = ObjectProperty(None)
def clear(self):
self.input1.text = ""
self.input2.text = ""
def give(self, *args):
name = (self.input2.text + ".json")
if self.input2.text:
with open(name, 'w') as fp:
store = JsonStore(name)
store['001'] = {self.input2.text:self.input1.text}
class All(Screen,FloatLayout):
def opening(self):
crt = Create()
crt.open()
def test(self):
print("Worked")
def closing(self):
crt = Create()
crt.dismiss()
def __init__(self, **kwargs):
super(All, self).__init__(**kwargs)
class Note(Screen, FloatLayout):
pass
class Notes(App):
title = "NOTES"
def build(self):
Builder.load_file("001.kv")
sm = ScreenManager()
sm.add_widget(MainWindow(name="main"))
sm.add_widget(All(name="all"))
sm.add_widget(Note(name="note"))
return sm
if __name__ == "__main__":
Notes().run()
My .kv:
<MainWindow>:
name:"main"
canvas:
Color:
rgba : 255,255,255,1
RoundedRectangle:
pos:(95, 450)
size : 210,120
radius: [30, 30, 30, 30]
FloatLayout:
Label:
text:"NOTES"
pos:(100,450)
size_hint:(.5,.2)
color:0,0,0,1
Label:
text: "simple"
pos:(100,425)
size_hint:(.5,.2)
color:0,0,0,1
Button:
text:"ALL NOTES"
pos :(100, 180)
size_hint : (.5,.2)
on_release:app.root.current="all"
Button:
text:"EXIT"
pos :(100, 60)
size_hint : (.5,.2)
on_release:app.stop()
<Create>:
title:"ADD"
input1:input1
input2:input2
size_hint: .5,.8
auto_dismiss:True
BoxLayout:
orientation:"vertical"
pos:self.pos
size:root.size
TextInput:
id:input2
size_hint: 1,.1
multiline: False
hint_text:"Your note name here"
TextInput:
id:input1
multiline: True
hint_text:"Your note here"
Button:
id:save
text:"SAVE"
size_hint:1,.2
on_release:root.give()
<All>:
FloatLayout:
Button:
text:"back"
size_hint:(1,.2)
pos_hint:{"x":0,"top":.2}
on_release:app.root.current="main"
Button:
background_color: (214/255, 201/255, 78/255, 0)
text: '+'
on_release:root.opening()
size_hint:(.2,.1)
pos_hint:{"x":.8,"top":1}
Button:
background_color: (214/255, 201/255, 78/255, 0)
text: 'INFO'
size_hint:(.2,.1)
pos_hint:{"x":.6,"top":1}
<Note>:
name:"note"
input3 : input3
input4 : input4
FloatLayout:
Label:
text:"NOTE NAME:"
pos_hint:{"x":0.07,"top":.95}
size_hint : (.1,0.05)
TextInput:
id : input3
multiline:True
pos_hint :{"x":.1,"top":.8}
size_hint : (.8,.4)
TextInput:
id:input4
multiline : False
pos_hint:{"x":.25,"top":.95}
size_hint:(.4,0.05)
Button:
id:"submit"
text:"SAVE"
pos_hint :{"x":.25,"top":.4}
size_hint : (.5,.2)
on_release:
app.root.current="all"
root.manager.get_screen("All").test()
root.manager.get_screen("All").closing()
Button:
id:"back"
text:"BACK"
pos_hint :{"x":.25,"top":.2}
size_hint : (.5,.2)
on_release:app.root.current="main"
class All(Screen,FloatLayout):
def __init__(self, **kwargs):
super(All, self).__init__(**kwargs)
self.crt = Create()
def opening(self):
self.crt.open()
def test(self):
print(self.crt.input2.text)
def closing(self):
self.crt.dismiss()

Why does rescheduling Clock intervals cause image positions to reset?

Within the program, one can see that as the ball bounces around, one has the ability to open settings page from the top right corner. Doing so pauses the ball's motion and opens the settings page.
Sorry if this is an obvious question, but I am again stumped by the strange inner workings of Kivy, and the docs aren't much use on these types of issues.
Problem
Ball always start at centre position. Want ball to continue / resume from previous position before switching screen?
Steps to recreate problem
Click on label, "Tap to start". Ball started bouncing from centre position
Click on cogwheel image. Settings screen is displayed
Click on "x" to close Settings screen. Ball started bouncing from centre position.
Code:
from kivy.lang import Builder
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image
from kivy import Config
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition,\
SlideTransition
from kivy.uix.widget import Widget
from kivy.animation import Animation
from kivy.properties import NumericProperty, ReferenceListProperty,\
ObjectProperty
from kivy.clock import Clock
from kivy.vector import Vector
from random import randint
Builder.load_string('''
<Ball>:
size_hint: None, None
source: '58-Breakout-Tiles.png'
pos: self.pos
size: 15, 15
<SettingsScreen>:
close: close
AnchorLayout:
anchor_x: 'left'
anchor_y: 'top'
Image:
id: close
size_hint: .03, .03
source: 'grey_crossGrey.png'
GridLayout:
cols: 2
Label:
font_name: 'vgafix.fon'
text: 'Music: '
Switch:
active: True
Label:
font_name: 'vgafix.fon'
text: 'Sounds: '
Switch:
active: True
<MenuScreen>:
cog: cog
AnchorLayout:
anchor_x: 'right'
anchor_y: 'top'
Image:
id: cog
size_hint: .03, .03
source: 'settings-cog.png'
BoxLayout:
orientation: 'vertical'
Image:
source: 'brickbreaker log.png'
Label:
font_name: 'vgafix.fon'
text: 'Tap to start'
<GameScreen>:
ball: ball
cog: cog
AnchorLayout:
anchor_x: 'right'
anchor_y: 'top'
Image:
id: cog
size_hint: .03, .03
source: 'settings-cog.png'
Ball:
id: ball
size_hint: None, None
center: self.parent.center
''')
Config.set('graphics', 'multisamples', '0')
class Ball(Image):
velocityX, velocityY = NumericProperty(0), NumericProperty(0)
velocity = ReferenceListProperty(velocityX, velocityY)
def move(self):
self.pos = Vector(*self.velocity) + self.pos
class Player(Widget):
pass
class Brick(Widget):
pass
class SettingsScreen(Screen):
def __init__(self, **kwargs):
super(SettingsScreen, self).__init__(**kwargs)
self.previous = False
def on_touch_down(self, touch):
if self.close.collide_point(*touch.pos):
sm.transition = SlideTransition(direction = 'right')
if self.previous == 'game':
sm.get_screen('game').interval()
sm.current = self.previous
class MenuScreen(Screen):
def __init__(self, **kwargs):
super(MenuScreen, self).__init__(**kwargs)
def on_touch_down(self, touch):
if self.cog.collide_point(*touch.pos):
sm.transition = SlideTransition(direction = 'left')
sm.get_screen('settings').previous = 'menu'
sm.current = 'settings'
else:
sm.transition = FadeTransition()
sm.current = 'game'
class GameScreen(Screen):
def __init__(self, **kwargs):
super(GameScreen, self).__init__(**kwargs)
self.initBall()
self.interval = Clock.schedule_interval(self.update, 1.0/60.0)
def on_touch_down(self, touch):
if self.cog.collide_point(*touch.pos):
sm.transition = SlideTransition(direction = 'left')
sm.get_screen('settings').previous = 'game'
self.interval.cancel()
sm.current = 'settings'
def initBall(self):
self.ball.center = self.center
self.ball.velocity = Vector(0, 4).rotate(randint(0, 360))
def update(self, dt):
self.ball.move()
if (self.ball.y < 0) or (self.ball.y > self.height-15):
self.ball.velocityY *= -1
# bounce off left and right
if (self.ball.x < 0) or (self.ball.x > self.width-15):
self.ball.velocityX *= -1
sm = ScreenManager(transition = FadeTransition())
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(GameScreen(name='game'))
sm.add_widget(SettingsScreen(name='settings'))
class BrickBreakerInsanityApp(App):
def build(self):
return sm
if __name__ == '__main__':
BrickBreakerInsanityApp().run()
Code assets (required):
https://drive.google.com/open?id=1GAnv5DfjNUuAXTybmsan90Dm0OuSVOfb
https://i.stack.imgur.com/rR799.png
https://i.stack.imgur.com/ngYvL.png
https://i.stack.imgur.com/AuxI3.png
https://i.stack.imgur.com/ypd7C.png
Root cause
The SlideTransition and direction is causing the ball to start at the centre.
Solution
Remove all references to SlideTransition.
Example
from kivy.lang import Builder
from kivy.app import App
from kivy.uix.image import Image
from kivy import Config
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition,
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty, ReferenceListProperty, StringProperty
from kivy.clock import Clock
from kivy.vector import Vector
from random import randint
Builder.load_string('''
<Ball>:
size_hint: None, None
source: './assets/icons/58-Breakout-Tiles.png'
pos: self.pos
size: 15, 15
<SettingsScreen>:
close: close
AnchorLayout:
anchor_x: 'left'
anchor_y: 'top'
Image:
id: close
size_hint: .03, .03
source: './assets/icons/grey_crossGrey.png'
GridLayout:
cols: 2
Label:
font_name: "./assets/fonts/vgafix.fon"
text: 'Music: '
Switch:
active: True
Label:
font_name: "./assets/fonts/vgafix.fon"
text: 'Sounds: '
Switch:
active: True
<MenuScreen>:
cog: cog
AnchorLayout:
anchor_x: 'right'
anchor_y: 'top'
Image:
id: cog
size_hint: .03, .03
source: './assets/icons/settings-cog.png'
BoxLayout:
orientation: 'vertical'
Image:
source: "./assets/icons/brickbreaker log.png"
Label:
font_name: "./assets/fonts/vgafix.fon"
text: 'Tap to start'
<GameScreen>:
ball: ball
cog: cog
AnchorLayout:
anchor_x: 'right'
anchor_y: 'top'
Image:
id: cog
size_hint: .03, .03
source: './assets/icons/settings-cog.png'
Ball:
id: ball
size_hint: None, None
center: self.parent.center
''')
Config.set('graphics', 'multisamples', '0')
class Ball(Image):
velocityX, velocityY = NumericProperty(0), NumericProperty(0)
velocity = ReferenceListProperty(velocityX, velocityY)
def move(self):
self.pos = Vector(*self.velocity) + self.pos
class Player(Widget):
pass
class Brick(Widget):
pass
class SettingsScreen(Screen):
def __init__(self, **kwargs):
super(SettingsScreen, self).__init__(**kwargs)
self.previous = StringProperty('')
def on_touch_down(self, touch):
if self.close.collide_point(*touch.pos):
self.manager.current = self.previous
class MenuScreen(Screen):
def on_touch_down(self, touch):
if self.cog.collide_point(*touch.pos):
self.manager.get_screen('settings').previous = self.manager.current
self.manager.current = 'settings'
else:
self.manager.transition = FadeTransition()
self.manager.current = 'game'
class GameScreen(Screen):
def __init__(self, **kwargs):
super(GameScreen, self).__init__(**kwargs)
self.initBall()
def on_pre_enter(self, *args):
self.interval = Clock.schedule_interval(self.update, 1.0 / 60.0)
def on_touch_down(self, touch):
if self.cog.collide_point(*touch.pos):
self.manager.get_screen('settings').previous = self.manager.current
self.manager.current = 'settings'
def initBall(self):
self.ball.center = self.center
self.ball.velocity = Vector(0, 4).rotate(randint(0, 360))
def update(self, dt):
self.ball.move()
if (self.ball.y < 0) or (self.ball.y > self.height - 15):
self.ball.velocityY *= -1
# bounce off left and right
if (self.ball.x < 0) or (self.ball.x > self.width - 15):
self.ball.velocityX *= -1
def on_pre_leave(self, *args):
self.interval.cancel()
sm = ScreenManager(transition=FadeTransition())
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(GameScreen(name='game'))
sm.add_widget(SettingsScreen(name='settings'))
class BrickBreakerInsanityApp(App):
def build(self):
return sm
if __name__ == '__main__':
BrickBreakerInsanityApp().run()

Python : How to remove focus from button when click anywhere on window

I am using python-2.7 and kivy .When i run test.py then i set focus on button.After that i click anywhere on window using mouse then focus does not remove . Because after click on window i press enter then it call def self.add().
Can someone tell me how to remove focus from button when click anywhere on
window ?
test.py
from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty, ObjectProperty
from kivy.clock import Clock
Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (500, 150)
class User(Screen):
name = ObjectProperty(None)
def __init__(self, **kwargs):
super(User, self).__init__(**kwargs)
Window.bind(on_key_down=self._on_keyboard_down)
Clock.schedule_once(self.name_focus, 1)
def name_focus(self, *args):
self.postUser.focus = True
self.postUser.background_color = [0.5, 0.5, 0.5, 1]
def _on_keyboard_down(self, instance, keyboard, keycode, text, modifiers):
if (hasattr(self.postUser, 'focus') and self.postUser.focus) and keycode == 40:
self.add()
def add(self):
print('button Event Call')
class Test(App):
def build(self):
return self.root
if __name__ == '__main__':
Test().run()
test.kv
#:kivy 1.10.0
User:
name: name
postUser : postUser
BoxLayout:
orientation: "vertical"
GridLayout:
cols: 2
padding: 20, 20
spacing: 10, 10
Label:
text: "Name"
text_size: self.size
valign: 'middle'
TextInput:
id:name
text_size: self.size
GridLayout:
cols: 2
padding: 0, 0
spacing: 5, 0
size_hint: .5, .35
pos_hint: {'x': .25, 'y': 0}
Button:
id:postUser
size_hint_x: .5
text: "Ok"
focus: False
on_release:
root.add()
You can add on_touch_up method in your User class.
def on_touch_up(self, touch):
if (hasattr(self.postUser, 'focus') and self.postUser.focus):
self.postUser.focus = False
self.postUser.background_color = [1, 1, 1, 1]
I am posting complete code.
test.py
from kivy.uix.screenmanager import Screen
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty, ObjectProperty
from kivy.clock import Clock
Window.clearcolor = (0.5, 0.5, 0.5, 1)
Window.size = (500, 150)
class User(Screen):
name = ObjectProperty(None)
def __init__(self, **kwargs):
super(User, self).__init__(**kwargs)
Window.bind(on_key_down=self._on_keyboard_down)
Clock.schedule_once(self.name_focus, 1)
def name_focus(self, *args):
self.postUser.focus = True
self.postUser.background_color = [0.5, 0.5, 0.5, 1]
def _on_keyboard_down(self, instance, keyboard, keycode, text, modifiers):
if (hasattr(self.postUser, 'focus') and self.postUser.focus) and keycode == 40:
self.add()
def add(self):
print('button Event Call')
def on_touch_up(self, touch):
if (hasattr(self.postUser, 'focus') and self.postUser.focus):
self.postUser.focus = False
self.postUser.background_color = [1, 1, 1, 1]
class Test(App):
def build(self):
return self.root
if __name__ == '__main__':
Test().run()
test.kv
#:kivy 1.10.0
User:
name: name
postUser : postUser
BoxLayout:
orientation: "vertical"
GridLayout:
cols: 2
padding: 20, 20
spacing: 10, 10
Label:
text: "Name"
text_size: self.size
valign: 'middle'
TextInput:
id:name
text_size: self.size
GridLayout:
cols: 2
padding: 0, 0
spacing: 5, 0
size_hint: .5, .35
pos_hint: {'x': .25, 'y': 0}
Button:
id:postUser
size_hint_x: .5
text: "Ok"
focus: False
on_release:
root.add()

How to Change BG Color of Dynamically Created Widget with On_Press and Save with Pickle? (Python with Kivy)

Goal:
Change background color of dynamically created widget with on-press.
Save this state with pickle such that when I open the program back up, the new color change is preserved
Note: You'll see in my code that I haven't made an attempt on saving the button bg color state to a file yet, as I'm still trying to get the on-press event to function.
I get the following error:
File "C:/Users/phili/scrollablelabelexample.py", line 45, in create_button
button_share.bind(on_press = self.update_buttons_departoverride(self))
TypeError: update_buttons_departoverride() takes 1 positional argument but 2 were given
Python code:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.textinput import TextInput
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.uix.scrollview import ScrollView
from kivy.properties import StringProperty, ObjectProperty, NumericProperty
from kivy.clock import Clock
import pandas as pd
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
class AnotherScreen(Screen):
pass
class BackHomeWidget(Widget):
pass
class Sequence(Screen):
pass
class ScreenManagement(ScreenManager):
pass
class MainScreen(Screen):
pass
class CleanScreen(BoxLayout):
def __init__(self, **kwargs):
super(CleanScreen, self).__init__(**kwargs)
self.orientation = "vertical"
Clock.schedule_once(lambda *args:self.create_button(self.ids.box_share))
def create_button(self, box_share):
top_button_share = 1.1
color = [.48,.72,.23,1]
for i in range(len(parts)):
top_button_share -= .4
button_share = Button(background_normal = '', background_color = color, id = "part"+str(i+1),pos_hint={"x": 0, "top": top_button_share}, size_hint_y=None, height=60, text=str(i))
button_share.bind(on_press = self.update_buttons_departoverride(self))
box_share.add_widget(button_share)
def update_buttons_departoverride(self):
self.background_color = 1.0, 0.0, 0.0, 1.0
presentation = Builder.load_file("garagemainexample.kv")
class MainApp(App):
def build(self):
return presentation
if __name__ == "__main__":
MainApp().run()
Kv Code:
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManagement:
transition: FadeTransition()
MainScreen:
Sequence:
<BigButton#Button>:
font_size: 40
size_hint: 0.5, 0.15
color: 0,1,0,1
<SmallNavButton#Button>:
font_size: 32
size: 125, 50
color: 0,1,0,1
<BackHomeWidget>:
SmallNavButton:
on_release: app.root.current = "main"
text: "Home"
pos: root.x, root.top - self.height
<MainScreen>:
name: "main"
FloatLayout:
BigButton:
on_release: app.root.current = "sequence"
text: "Sequence"
pos_hint: {"x":0.25, "top": 0.4}
<CleanScreen>:
ScrollView:
GridLayout:
id: box_share
cols: 1
size_hint_y: None
size_hint_x: 0.5
spacing: 5
padding: 90
height: self.minimum_height
canvas:
Color:
rgb: 0, 0, 0
Rectangle:
pos: self.pos
size: self.size
<Sequence>:
name: "sequence"
CleanScreen:
id: cleanscreen
BackHomeWidget:
With button_share.bind (on_press = self.update_buttons_departoverride (self)) you're calling the method, so you're trying to bind on_press with None (self.update_buttons_departoverride return None). If you want to pass arguments, use lambda or functools.partial:
from functools import partial
button_share.bind(on_press=partial(self.update_buttons_departoverride, arg1,...))
However, if you need to pass only the button's reference, it is already passed automatically. You just have to do:
button_share.bind(on_press=self.update_buttons_departoverride)
and:
def update_buttons_departoverride(self, button):
To store the configuration of your widgets you can use Storage. A simplified example using DictStore:
main.py:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.widget import Widget
from kivy.clock import Clock
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.properties import ObjectProperty
from kivy.storage.dictstore import DictStore
class AnotherScreen(Screen):
pass
class BackHomeWidget(Widget):
pass
class Sequence(Screen):
pass
class ScreenManagement(ScreenManager):
pass
class MainScreen(Screen):
pass
class CleanScreen(BoxLayout):
box_share = ObjectProperty()
config_file = DictStore('conf.dat')
def __init__(self, **kwargs):
super(CleanScreen, self).__init__(**kwargs)
self.orientation = "vertical"
Clock.schedule_once(self.create_button)
def create_button(self, *args):
top_button_share = 1.1
color = (.48, .72, .23, 1)
for i in range(5):
top_button_share -= .4
id_ = "part" + str(i + 1)
if self.config_file.exists(id_):
btn_color = self.config_file[id_]["background_color"]
else:
self.config_file.put(id_, background_color=color)
btn_color = color
button_share = Button(background_normal='',
background_color=btn_color,
id=id_,
pos_hint={"x": 0, "top": top_button_share},
size_hint_y=None,
height=60,
text=str(i))
button_share.bind(on_press=self.update_buttons_departoverride)
self.box_share.add_widget(button_share)
def update_buttons_departoverride(self, button):
button.background_color = 1.0, 0.0, 0.0, 1.0
self.config_file.put(button.id, background_color=(1.0, 0.0, 0.0, 1.0))
presentation = Builder.load_file("garagemainexample.kv")
class MainApp(App):
def build(self):
return presentation
if __name__ == "__main__":
MainApp().run()
garagemainexample.kv:
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManagement:
transition: FadeTransition()
MainScreen:
Sequence:
<BigButton#Button>:
font_size: 40
size_hint: 0.5, 0.15
color: 0,1,0,1
<SmallNavButton#Button>:
font_size: 32
size: 125, 50
color: 0,1,0,1
<BackHomeWidget>:
SmallNavButton:
on_release: app.root.current = "main"
text: "Home"
pos: root.x, root.top - self.height
<MainScreen>:
name: "main"
FloatLayout:
BigButton:
on_release: app.root.current = "sequence"
text: "Sequence"
pos_hint: {"x":0.25, "top": 0.4}
<CleanScreen>:
box_share: box_share
ScrollView:
GridLayout:
id: box_share
cols: 1
size_hint_y: None
size_hint_x: 0.5
spacing: 5
padding: 90
height: self.minimum_height
canvas:
Color:
rgb: 0, 0, 0
Rectangle:
pos: self.pos
size: self.size
<Sequence>:
name: "sequence"
CleanScreen:
id: cleanscreen
BackHomeWidget:

Custom Closable Tab in Kivy

I am trying to implement a custom closable tab header in kivy.
What I did was combine a class:TabbedPanelHeader object with a custom class:CloseButton object. Both of these widgets are inside a class:BoxLayout, side-by-side.
However, once I add this into a class:TabbedPanel object, nothing shows up..
I am not sure how to move forward and would greatly appreciate all the help!
Below is the relevant part of the code.
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import Image
from kivy.graphics import *
from kivy.uix.tabbedpanel import TabbedPanelHeader
class CloseButton(ButtonBehavior, Image):
def __init__(self, **kwargs):
super(CloseButton, self).__init__(**kwargs)
self.source = 'atlas://data/images/defaulttheme/close'
self.size_hint_x = .2
def on_press(self):
self.source = 'atlas://data/images/defaulttheme/checkbox_radio_off'
def on_release(self):
self.source = 'atlas://data/images/defaulttheme/checkbox_radio_off'
## do the actual closing of the tab
class ClosableTabHeader(BoxLayout):
def __init__(self, **kwargs):
super(ClosableTabHeader, self).__init__(**kwargs)
self.size = (100, 30)
self.size_hint = (None, None)
self.canvas.before.add(Color(.25, .25, .25))
self.canvas.before.add(Rectangle(size=(105, 30)))
self.add_widget(TabbedPanelHeader(background_color=(.65, .65, .65, 0), text='testing'))
self.add_widget(CloseButton())
if __name__ == '__main__':
from kivy.app import App
class TestApp(App):
def build(self):
return ClosableTabHeader()
TestApp().run()
Here is some code which comes close to achieve what you are trying to achieve
from kivy.app import App
from kivy.animation import Animation
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.tabbedpanel import TabbedPanel, TabbedPanelHeader
from kivy.factory import Factory
from kivy.lang import Builder
class CloseableHeader(TabbedPanelHeader):
pass
class TestTabApp(App):
def build(self):
return Builder.load_string('''
TabbedPanel:
do_default_tab: False
FloatLayout:
BoxLayout:
id: tab_1_content
Label:
text: 'Palim 1'
BoxLayout:
id: tab_2_content
Label:
text: 'Palim 2'
BoxLayout:
id: tab_3_content
Label:
text: 'Palim 3'
CloseableHeader:
text: 'tab1'
panel: root
content: tab_1_content.__self__
CloseableHeader:
text: 'tab2'
panel: root
content: tab_2_content.__self__
CloseableHeader:
text: 'tab3'
panel: root
content: tab_3_content.__self__
<CloseableHeader>
color: 0,0,0,0
disabled_color: self.color
# variable tab_width
text: 'tabx'
size_hint_x: None
width: self.texture_size[0] + 40
BoxLayout:
pos: root.pos
size_hint: None, None
size: root.size
padding: 3
Label:
id: lbl
text: root.text
BoxLayout:
size_hint: None, 1
orientation: 'vertical'
width: 22
Image:
source: 'tools/theming/defaulttheme/close.png'
on_touch_down:
if self.collide_point(*args[1].pos) :\
root.panel.remove_widget(root); \
''')
if __name__ == '__main__':
TestTabApp().run()
It is based on https://github.com/kivy/kivy/blob/master/examples/widgets/tabbed_panel_showcase.py

Categories