I wanted to remove the 'bl' widget by clicking the 'horny mode' button, but it is not initially on the screen. How can I make the widget be removing by clicking on the button?
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.config import Config
from kivy.core.window import Window
from kivy.uix.image import Image
from kivy.uix.widget import Widget
class ScraperApp(App, FloatLayout):
def PhotGif(self):
wentil = Image(source='img.gif', size_hint = (.5, .5), anim_loop = 99999)
photo = AnchorLayout(anchor_x='center', anchor_y='top', padding = [0, 75, 0, 0])
photo.add_widget(wentil)
return photo
def build(self):
bl = BoxLayout(orientation='horizontal', padding = [50, 100, 50, 150], spacing = 5)
bl.add_widget( Button(text = '1', on_press = self.first, font_size = 20, size_hint = (.3, .1)))
bl.add_widget( Button(text = '2', on_press = self.second, font_size = 20, size_hint = (.3, .1)))
#bl.add_widget( Button(text = 'Wallpaper mode', on_press = self.wallpaper, font_size = 20, size_hint = (.3, .1)))
wid = FloatLayout()
wid.add_widget(ScraperApp().PhotGif())
wid.add_widget(bl)
return wid
def first(self, instance):
print('Horny mode')
instance.text = 'кнопка нажата'
ScraperApp().build().remove_widget(bl)
stop()
def stop():
ScraperApp().build().remove_widget(bl)
def second(self, instance):
print('Soft mode')
instance.text = 'кнопка нажата'
if __name__ == '__main__':
ScraperApp().run()
Can you help me make so that when you click on any button, the widget with the buttons removed?
First, save a reference to the widget that you want to remove:
def build(self):
self.bl = BoxLayout(orientation='horizontal', padding=[50, 100, 50, 150], spacing=5)
self.bl.add_widget(Button(text='1', on_press=self.first, font_size=20, size_hint=(.3, .1)))
self.bl.add_widget(Button(text='2', on_press=self.second, font_size=20, size_hint=(.3, .1)))
# self.bl.add_widget( Button(text = 'Wallpaper mode', on_press = self.wallpaper, font_size = 20, size_hint = (.3, .1)))
wid = FloatLayout()
wid.add_widget(ScraperApp().PhotGif())
wid.add_widget(self.bl)
return wid
Then modify the first() method to remove it:
def first(self, instance):
print('Horny mode')
instance.text = 'кнопка нажата'
self.root.remove_widget(self.bl)
self.stop()
Note that the code:
ScraperApp().build().remove_widget(bl)
creates a new instance of the ScaperApp and tried to remove bl from that instance. Aside from other problems, nothing you do to that new instance will affect the instance that is currntly running.
Related
I am new to Kivy and I am sort of stuck. I am using GridLayout to make my app and I am having some trouble putting a video in my background. The code I will post makes an app with a black background. How do I replace the black background with a video, particularly mp4. I would also like to make the video darker if possible. I wanted to use AnchorPoint but I am not quite sure how to put both in there. Any help will be appreciated.
from kivy.app import App
from kivy.uix.video import Video
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.image import Image
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
class MZ_Invest(App):
def build(self):
self.root_layout = FloatLayout()
self.window = GridLayout()
self.window.cols = 1
self.window.size_hint = (0.6,0.7)
self.window.pos_hint = {"center_x":0.5, "center_y":0.5}
#add widgets
#Video
video = Video(source='birds.mp4', state='play')
video.opacity = 0.5
#Image
self.window.add_widget(Image(
source="mzi.png",
size_hint = (1.5,1.5)
))
#Label
self.greeting = Label(
text="How much would you like to invest?",
font_size = 18,
color='90EE90'
)
self.window.add_widget(self.greeting)
#User Input
self.user = TextInput(
multiline=False,
padding_y= (20,20),
size_hint = (1, 0.5)
)
self.window.add_widget(self.user)
#Button
self.button = Button(
text="Submit",
size_hint = (1,0.5),
bold = True,
background_color = '90EE90',
)
self.button.bind(on_press=self.callback)
self.window.add_widget(self.button)
#self.root_layout.add_widget(video)
self.root_layout.add_widget(self.window)
return self.root_layout
def callback(self, instance):
if self.user.text.isnumeric() and int(self.user.text) >= 10000:
self.greeting.text = "Calculating: $" + self.user.text
self.greeting.color = '90EE90'
else:
self.greeting.text = "Invalid"
self.greeting.color = "#FF0000"
if __name__ == "__main__":
MZ_Invest().run()
Most Kivy widgets have a transparent background, so you can just display a video and then display your GUI over that. Try adding something like this at the end of your build() method:
self.root_layout = FloatLayout()
video = Video(source='BigBuckBunny.mp4', state='play')
video.opacity = 0.5 # adjust to make the video lighter/darker
self.root_layout.add_widget(video) # add the video first
self.root_layout.add_widget(self.window) # now add the GUI window so it will be drawn over the video
return self.root_layout # return the root_layout instead of the window
I know this question may have been asked already, but I am a beginner to Kivy, so I would like for someone to explain how to put two buttons on the same screen.
The problem is I try to return the button variable, and it works. However, when I try to return two at the same time, it will give me an error.
here is my code:
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from functools import partial
class App(App):
def com1(self, instance, *args):
label1 = Label(text = "Hi")
return label1
def com2(self, instance, *args):
label = Label(text= "Bye")
def build(self):
button1 = Button(text = "Hi", size_hint = (0.25, 0.18), pos = (350, 100))
button1.bind(on_press=partial(self.com1, button1))
button2 = Button(text = "Bye", size_hint = (0.25, 0.18), pos = (350, 200))
button2.bind(on_press=partial(self.com2, button2))
return button1, button2
App().run()
The build method must return a single widget, so depending on how you want the buttons to be arranged you have several options such as BoxLayout, RelativeLayout, FloatLayout, etc. In this case for simplicity I will use a BoxLayout:
# ...
from kivy.uix.boxlayout import BoxLayout
# ...
class App(App):
# ...
def build(self):
button1 = Button(text="Hi", size_hint=(0.25, 0.18), pos=(350, 100))
button1.bind(on_press=partial(self.com1, button1))
button2 = Button(text="Bye", size_hint=(0.25, 0.18), pos=(350, 200))
button2.bind(on_press=partial(self.com2, button2))
boxlayout = BoxLayout()
boxlayout.add_widget(button1)
boxlayout.add_widget(button2)
return boxlayout
# ...
I am trying to create a menu using box layout in kivy. I wanted to use "root.top-self.height" so that it sticks the vertical layout from the top of screen but its still sticking from bottom. Also when I print(root.top) its strangely giving 100 which is not my screen resolution. Please let me know how can I place it accurately.
Furthermore I read somewhere that I need to use root=BoxLayout(), now after using this the button's are not clickable after adding that, before adding this I could use the buttons. Please do let me know how to deal with "root" ie screen or App size functionality.
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.label import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.layout import Layout
from kivy.uix.button import Button
from kivy.lang import Builder
## CREATING A CLASS WHICH HAS SCREEN CONTENT:
class firstScreen(BoxLayout):
def __init__(self,**kwargs):
super(firstScreen, self).__init__(**kwargs)
self.orientation = 'vertical'
root = BoxLayout()
self.pos = (0 ,root.top-self.height)
print(root.top)
self.myButton1 = Button(text='Home',
color = (1,0,0,1),
size_hint = (0.1,None),
## pos_hint = {'x':.8, 'y':'.7'},
## pos_hint = {'x':0, 'top':'0'},
pos = (0,0)
)
self.myButton2 = Button(text='Buy Now',
color = (1,0,0,1),
size_hint = (0.1,None))
self.myButton3 = Button(text='Blog',
color = (1,0,0,1),
size_hint = (0.1,None))
self.myButton4 = Button(text='Contant Us',
color = (1,0,0,1),
size_hint = (0.1,None))
self.add_widget(self.myButton1)
self.add_widget(self.myButton2)
self.add_widget(self.myButton3)
self.add_widget(self.myButton4)
def on_touch_down(self,touch):
print(touch)
def on_touch_move(self,touch):
print(touch)
def on_touch_up(self,touch):
print(touch)
## CREATING A CLASS WHICH RETURNS SOME SCREEN:
class myKivyApp(App):
def build(self):
return firstScreen()
## THIS CODE RUNS THE CLASS WHICH HAS SOME SCREEN
if __name__ == "__main__":
myKivyApp().run()
Eliminating the unused BoxLayout and setting the y component of size_hint to 1.0 means that each button will share equally in the vertical space available for the firstScreen.
class firstScreen(BoxLayout):
def __init__(self,**kwargs):
super(firstScreen, self).__init__(**kwargs)
self.orientation = 'vertical'
self.myButton1 = Button(text='Home',
color = (1,0,0,1),
size_hint = (0.1,1.0))
self.myButton2 = Button(text='Buy Now',
color = (1,0,0,1),
size_hint = (0.1,1.0))
self.myButton3 = Button(text='Blog',
color = (1,0,0,1),
size_hint = (0.1,1.0))
self.myButton4 = Button(text='Contant Us',
color = (1,0,0,1),
size_hint = (0.1,1.0))
self.add_widget(self.myButton1)
self.add_widget(self.myButton2)
self.add_widget(self.myButton3)
self.add_widget(self.myButton4)
By the way, the root.top will always be 100 in the __init__() method. The 100 is the default value and is not updated until the app is actually displayed.
I am trying to use two sliders in layout. The code is given below. The problem is only the second slider responds to the mouse actions. The first slider (and also the button) does not respond at all to the mouse actions. How to make both sliders responsive?
Thanks in advance
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.slider import Slider
from kivy.uix.button import Button
def Exit(instance):
print('Exit the screen')
App.get_running_app().stop()
def MainScreen():
flt = FloatLayout()
button = Button(text='Hello Kivy World')
button.size = (200, 100)
button.size_hint = (None, None)
button.pos_hint = {'center_x': .5, 'center_y': .2}
button.bind(on_press=Exit)
flt.add_widget(button)
slid1 = Slider()
slid1.id = 'Slider 01'
slid1.size_hint_x = .75
slid1.pos_hint = {'x': .125, 'center_y': .5}
slid1.value_track = True
slid1.value_track_color = [0, 1, 0, 1]
slid1.sensitivity = 'handle'
flt.add_widget(slid1)
slid2 = Slider()
slid2.id = 'Slider 02'
slid2.size_hint_x = .75
slid2.pos_hint = {'x': .125, 'center_y': .75}
slid2.value_track = True
slid2.value_track_color = [1, 0, 0, 1]
slid2.sensitivity = 'handle'
flt.add_widget(slid2)
return flt
class MainApp(App):
def build(self):
return MainScreen()
if __name__ == "__main__":
app = MainApp()
app.run()
If you change your floatlayout to a boxlayout it works.
flt = BoxLayout(orientation='vertical')
Here is my code: I want to make a game where the main_label changes text when you press a button but I've looked everywhere for a week and still don't understand how to do it. I looked on Kivy's website but I don't understand. As you can see I'm new to kivy and not very experienced
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.clock import Clock
energy = 100
hours = 4
class app1(App):
def build(self):
self.f = FloatLayout()
#Labels
self.energy_label = Label(text = "Energy = " + str(energy), size_hint=(.1, .15),pos_hint={'x':.05, 'y':.9})
self.time_label = Label(text = "Hours = " + str(hours), size_hint=(.1, .15),pos_hint={'x':.9, 'y':.9})
self.name_label = Label(text = "Game", size_hint=(.1, .15),pos_hint={'x':.45, 'y':.9})
self.main_label = Label(text = "Default_text", size_hint=(1, .55),pos_hint={'x':0, 'y':.35})
#Main Buttons
self.inventory_button = Button(text = "Inventory", size_hint=(.3, .1),pos_hint={'x':.65, 'y':.2})
self.help_button = Button(text = "Help", size_hint=(.3, .1),pos_hint={'x':.65, 'y':.1})
self.craft_button = Button(text = "Craft", size_hint=(.3, .1),pos_hint={'x':.05, 'y':.1})
self.food_button = Button(text = "Food", size_hint=(.3, .1),pos_hint={'x':.35, 'y':.2})
self.go_button = Button(text = "Go", size_hint=(.3, .1),pos_hint={'x':.35, 'y':.1})
self.walk_button = Button(text = "Walk", size_hint=(.3, .1),pos_hint={'x':.05, 'y':.2})
def update(self, *args):
self.main_widget.text = str(self.current_text)
self.f.add_widget(self.energy_label)
self.f.add_widget(self.main_label)
self.f.add_widget(self.time_label)
self.f.add_widget(self.inventory_button)
self.f.add_widget(self.help_button)
self.f.add_widget(self.craft_button)
self.f.add_widget(self.food_button)
self.f.add_widget(self.go_button)
self.f.add_widget(self.walk_button)
self.f.add_widget(self.name_label)
self.current_text = "Default"
Clock.schedule_interval(update, 1)
return self.f
def update_label(input):
input = self.current_text
help_button.bind(on_press = update_label("success!"))
if __name__=="__main__":
app1().run()
How can I update my code so that by pressing the help_button, main_label changes its text ?
Thank you for your help.
Well! there was a real need for improvement in your code. (I understand it as you are not experienced.)
Improvement: 1
An application can be built if you return a widget on build(), or if you set
self.root.(You shouldn't make all of the gui in build function itself.)
def build(self):
return Hello() #That's what is done here
Improvement: 2
on_release/on_press both are always useful.
self.help_button = Button(text = "Help", size_hint=(.3, .1),pos_hint={'x':.65, 'y':.1},on_press = self.update)
Improvement: 3
As help_button is pressed, update function is called which changes the text of main_label.
def update(self,event):
self.main_label.text = "Changed to change"
Here is you full improved code
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.clock import Clock
energy = 100
hours = 4
class Hello(FloatLayout):
def __init__(self,**kwargs):
super(Hello,self).__init__(**kwargs)
self.energy_label = Label(text = "Energy = " + str(energy), size_hint=(.1, .15),pos_hint={'x':.05, 'y':.9})
self.time_label = Label(text = "Hours = " + str(hours), size_hint=(.1, .15),pos_hint={'x':.9, 'y':.9})
self.name_label = Label(text = "Game", size_hint=(.1, .15),pos_hint={'x':.45, 'y':.9})
self.main_label = Label(text = "Default_text", size_hint=(1, .55),pos_hint={'x':0, 'y':.35})
#Main Buttons
self.inventory_button = Button(text = "Inventory", size_hint=(.3, .1),pos_hint={'x':.65, 'y':.2})
self.help_button = Button(text = "Help", size_hint=(.3, .1),pos_hint={'x':.65, 'y':.1},on_press = self.update)
self.craft_button = Button(text = "Craft", size_hint=(.3, .1),pos_hint={'x':.05, 'y':.1})
self.food_button = Button(text = "Food", size_hint=(.3, .1),pos_hint={'x':.35, 'y':.2})
self.go_button = Button(text = "Go", size_hint=(.3, .1),pos_hint={'x':.35, 'y':.1})
self.walk_button = Button(text = "Walk", size_hint=(.3, .1),pos_hint={'x':.05, 'y':.2})
self.add_widget(self.energy_label)
self.add_widget(self.main_label)
self.add_widget(self.time_label)
self.add_widget(self.inventory_button)
self.add_widget(self.help_button)
self.add_widget(self.craft_button)
self.add_widget(self.food_button)
self.add_widget(self.go_button)
self.add_widget(self.walk_button)
self.add_widget(self.name_label)
self.current_text = "Default"
def update(self,event):
self.main_label.text = "Changed to change"
class app1(App):
def build(self):
return Hello()
if __name__=="__main__":
app1().run()
Well another way you can go about it is that in Kivy everything at the right of the kivy language properties is pure python. So you can connect your kivy files to the python by passing some tag to your function and then act in python how ever you want.
In the .kv file:
Button:
on_press: root.label_change('btn1')
In the Python file:
Class MyButton(Button):
def label_change(self, event):
self.event = event
if self.event == "btn1":
label.text = "something"
Small dirty snippet!
import kivy
kivy.require('1.9.1')
from kivy.app import App
from kivy.lang import Builder
from kivy.clock import Clock
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
import random,string
Builder.load_string('''
<updlbl>:
orientation: 'vertical'
Label:
id: updlbl
text: 'Update Label'
Button:
text: 'Click me'
on_press: root.upd('updated text')
''')
class updlbl(BoxLayout):
def __init__(self, **kwargs):
super(updlbl,self).__init__(**kwargs)
pass
def upd(self,txt):
self.ids.updlbl.text = txt
class UpdateLabel(App):
def build(self):
self.title = "Update Label"
return updlbl()
if __name__ == '__main__':
UpdateLabel().run()