I want to be able to scroll within the buttons numbered 10-1 but it wont let me scroll. Any solutions? I was finally able to put the buttons in a scrollview but after all my effort I am not able to move it. It just crams into the space provided and doesn't move at all.
orientation: "vertical"
SongText:
size_hint: 1,0.35
PausePlay:
size_hint: 1,0.1
ScrollView:
orientation: "vertical"
PlayList:
orientation: "vertical"
AddDeleteShuffle:
size_hint: 1,0.2
<SongText>:
Label:
id: songtitle
text: "Song Title"
<PausePlay>:
Button:
id: play
text: "Play"
Button:
id: pause
text: "Pause"
This is the main area of focus I think nothing is needed here but who knows.
<PlayList>: #Main area of conceren
<AddDeleteShuffle>:
BoxLayout:
Button:
text: "Shuffle"
Button:
text: "Delete"
Button:
text: "Shuffle"
kv file ^
py file
import kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.scrollview import ScrollView
from kivy.uix.stacklayout import StackLayout
from kivy.uix.gridlayout import GridLayout
from kivy.metrics import dp
from kivy.core.audio import SoundLoader
from kivy.properties import ObjectProperty, StringProperty
from kivy.uix.screenmanager import ScreenManager, Screen
class AddDeleteShuffle(BoxLayout):
pass
class PlayList(BoxLayout):
def __init__(self,**kwargs):
super().__init__(**kwargs)
for i in range(0,10):
size = dp(100)
b = Button(text=str(i+1), size_hint=(1,None), size=(size,size))
self.add_widget(b)
Class for the playlist it is inside a scroll view, but I can't move it. Maybe I have to add a layout in the kv file. I have no idea.
class PausePlay(BoxLayout):
pass
class SongText(BoxLayout):
pass
class AppLayout(BoxLayout):
pass
class Sound5App(App):
def build(self):
return AppLayout()
if __name__ == "__main__":
Sound5App().run()
Related
I am able to get the data in main app class and run it to get as Label but when I want to do the same by getting data in main app class where as defining Label in another class, it's giving name error.
import kivy
from kivy.app import App # import Kivy App module to create
from kivy.uix.label import Label # import Label Module
kivy.require('1.11.1')
class MyKivyApp(App):
def build(self):
f=open('tesit.txt','r')
t=f.read()
return Label(text =t)
rt=MyKivyApp()
rt.run()
Your lines:
rt=MyKivyApp()
rt.run()
are inside the MyKivyApp. Just unindent those two lines to get them outside of the App class.
This's how we can read a text file and display it as label in another screen in kivy.
from kivy.app import App
from kivy.uix.screenmanager import Screen
from kivy.lang import Builder
from kivy.properties import StringProperty
class Screen1(Screen):
f=open('test.txt','r')
t=f.readlines()
b=t[0]
f.close()
class Screen2(Screen):
username = StringProperty("")
root = Builder.load_string('''
<Screen1>:
name:'xy'
id:loginscreen
BoxLayout:
orientation: "vertical"
Button:
text:'Execution button'
on_release: root.manager.current = "screen2"
<Screen2>:
name: "screen2"
FloatLayout:
Label:
text: root.username
Button:
text:'Back to menu'
size_hint:.3,.3
on_release: root.manager.current = "xy"
ScreenManager:
Screen1:
id:loginscreen
Screen2:
username: loginscreen.b
''')
class MyApp(App):
def build(self):
return root
r=MyApp()
r.run()
I have a code where there are two Screens: "Main" & "Second", when you press a button in the Main Screen a new button is created in the same screen. I need the new button created to take me to the Second Screen. Please Help as I need it for a bigger project soon.
Normal Code:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import Screen,ScreenManager
class Main(Screen):
pass
class Second(Screen):
pass
class Manager(ScreenManager):
pass
kv=Builder.load_file("test1.kv")
movie=Manager()
movie.add_widget(Main(name="main"))
movie.add_widget(Second(name="second"))
class Movie(App):
def build(self):
return movie
Movie().run()
Kv Code:
#: import Button kivy.uix.button.Button
<Main>:
name: "main"
GridLayout:
id: GL
cols: 1
size_hint: (.5,.5)
pos_hint: {"center_x":.5,"center_y":.5}
Button:
text: "Press Me"
on_press:
Btn=Button(text="Second",size_hint=(.5,.5),pos_hint= {"center_x":.5,"center_y":.5})
GL.add_widget(Btn)
#Btn.bind(on_press= ????)
<Second>:
name: "second"
Button:
text: "Go Back!"
size_hint: (.5,.5)
pos_hint: {"center_x":.5,"center_y":.5}
on_press:
app.root.current="main"
The simplest way is to just define a method in the Main class that does the Screen change:
class Main(Screen):
def switch(self, button_instance):
self.manager.current = 'second'
Then reference that method in the kv:
Button:
text: "Press Me"
on_press:
Btn=Button(text="Second",size_hint=(.5,.5),pos_hint= {"center_x":.5,"center_y":.5}, on_press=root.switch)
GL.add_widget(Btn)
A more complex approach that doesn't require any additional methods works by using the python setattr() method to set the current Screen of the ScreenManager:
Button:
text: "Press Me"
on_press:
Btn=Button(text="Second",size_hint=(.5,.5),pos_hint= {"center_x":.5,"center_y":.5})
Btn.bind(on_press=lambda ins: partial(setattr, root.manager, 'current', 'second')())
GL.add_widget(Btn)
The lambda is required to avoid passing the Button instance to the setattr() method. Also, an import is required for partial in the kv:
#: import partial functools.partial
This is the output I am getting.I am trying to place a label at the very top and centre of the top area(centre top), but when I try to do it it just goes out of the screen.Please help by changing the only code I am writing.
Thanks.
What I tried.
.py file:
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.config import Config
from kivy.uix.widget import Widget
from kivy.graphics import Line, Color
from kivy.lang import Builder
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
class kivyclass(Widget):
pass
class MainApp(App):
def build(self):
return kivyclass()
if __name__ == '__main__':
MainApp().run()
.kv file:
#:import utils kivy.utils
<kivyclass>:
canvas.before:
Color:
rgba: utils.get_color_from_hex('#d6fffc')
Rectangle:
size: self.size
pos: self.pos
AnchorLayout:
anchor_x: 'center'
anchor_y: 'top'
Label:
id: labcovid
text: 'Covid-19 Uttarakhand'
font_size: root.width/15
font_name: 'vollkorn.ttf'
color: utils.get_color_from_hex('#4a4a4a')
this is the output I am getting
Your problem is two-fold
Your root widget is a simple widget that does not handle its children in any way.
The Label that has the text gets as big as the whole window.
The solution to the first is to make a FloatLayout as root widget
class kivyclass(FloatLayout):
pass
and to the second, to add this to your label on the kv part
size_hint: None, None
size: self.texture_size
How do I change the button's color when it's down? As in, when you press the button, replace the blue-ish colour into another one?
I've tried fiddling around with background_color_down, background_color_normal, I tried using canvas and the sort but nothing seems to have the effect I intend it to
Or you could try giving the path to a png file with your design of the pressed button to the background_down attribute. Here my file is called "pressed.png" and is located within the same folder as the python programm. this is a link to something i made within 30 seconds in inkscape.
#!/usr/bin/python3.5
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang.builder import Builder
from kivy.properties import ObjectProperty, ListProperty
from kivy.atlas import Atlas
kv= Builder.load_string('''
<MainScreen>
my_but:my_but
orientation: "vertical"
size_hint:1,1
# size_hint: 0.3,0.3
# pos_hint:{"center_x": 1, "center_y":1}
Label:
size_hint: 0.5,0.4
text: "Look at my Button go:"
Button:
id:my_but
size_hint: 0.5,0.4
text: "klick me"
# background_normal: ""
background_down: "pressed.png"
''')
class MainScreen(BoxLayout):
my_but = ObjectProperty(None)
def __init__(self, **kwargs):
super(MainScreen, self).__init__(**kwargs)
class myApp(App):
def build(self):
return MainScreen()
if __name__ == "__main__":
myApp().run()
it is a simple program but i can not find a way to make it work. i just want to add a widget in boxlayout2 when the user presses the button (and has not writed anything in textinput) which is located in boxlayout1 .The widget do not display in screen.What should i do?
main.py
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
class BoxLayout1(BoxLayout):
def Search(self):
if self.ids.textinput.text!='':
BoxLayout2()
class BoxLayout2(BoxLayout):
def Print(self):
self.add_widget(Button(text='hello'))
class TestApp(App):
pass
TestApp().run()
and here is my kivy code
test.kv
<BoxLayout1>:
BoxLayout:
Label:
text:'Hello'
TextInput:
id: textinput
Button:
text: 'write'
on_press: root.Search()
BoxLayout:
orientation: 'vertical'
BoxLayout1:
BoxLayout2:
I see the presentation layout i want but the button is nowhere to be found.
To make it clear let's follow the stream of the app you've written.
it creates a BoxLayout and puts BoxLayout1 and BoxLayout2 in it, the second one doesn't have any content. When you click on write, the app checks the content of the text box and if valid, calls the constructor of BoxLayout2! Now at this point it creates an instance of this class, but does not keep it's reference so it will be discarded immediately. Now what you want is to call a function of a currently existing instance, not to create another one. Here's the code:
python:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder
class BoxLayout1(BoxLayout):
def Search(self):
if self.ids.textinput.text!='':
self.parent.ids.bxl2.addButton()
# BoxLayout2()
class BoxLayout2(BoxLayout):
def addButton(self):
button=Button(text='hello')
self.add_widget(button)
kivy language:
<BoxLayout1>:
BoxLayout:
Label:
text:'Hello'
TextInput:
id: textinput
Button:
text: 'write'
on_press: root.Search()
BoxLayout:
orientation: 'vertical'
BoxLayout1:
BoxLayout2:
id:bxl2