from kivy.uix.button import Button
from kivy.uix.behaviors.button import ButtonBehavior
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock
from kivy.uix.widget import Widget
from kivy.graphics import BorderImage
from kivy.graphics import Color, RoundedRectangle, Rectangle, Triangle
from kivy.core.window import Window
#from kivy.config import Config
from kivy.uix.checkbox import CheckBox
from kivy.uix.popup import Popup
from kivy.uix.scrollview import ScrollView
from kivy.uix.filechooser import FileChooserListView
from kivy.properties import ObjectProperty
class OpeningPage(FloatLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.but = RoButton(text = 'START', pos = (350,100), font_size=14, size=(100,60), size_hint=(None,None))
self.add_widget(self.but)
class RoButton(Button):
butt = ObjectProperty()
def __init__(self, **kwargs):
super(RoButton, self).__init__(**kwargs)
text = self.text
with self.canvas:
# Color(1., 0, 0)
self.butt = RoundedRectangle( size= self.size, pos = self.pos, radius =[400])
class UI(App):
def build(self):
self.screen_manager = ScreenManager()
self.opening_page = OpeningPage()
screen = Screen(name ='Opening_Page')
screen.add_widget(self.opening_page)
self.screen_manager.add_widget(screen)
return self.screen_manager
if __name__ == '__main__':
the_app = UI()
the_app.run()
Everytime I try to make a circular button using this code I get a box behind the circle. I tried doing self.canvas.before but still no luck if possible could answers be in python rather than .kv language thanks.
Attached image of problem]1
The box you see is the normal Button image. If you don't want that, probably don't use a Button, instead use class RoButton(ButtonBehavior, Widget):.
Related
from kivymd.app import MDApp
from kivy.uix.boxlayout import BoxLayout
from kivymd.uix.boxlayout import MDBoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivymd.uix.screen import Screen, MDScreen
from kivymd.uix.fitimage import FitImage
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivymd.uix.card import MDCard
from kivy.uix.image import Image
from kivy.animation import Animation
from kivymd.uix.snackbar import Snackbar
from kivy.clock import Clock
from kivy.uix.label import Label
from kivymd.uix.label import MDLabel
from kivy.core.audio import SoundLoader
from kivy.uix.popup import Popup
from kivy.core.window import Window
from skpy import SkypeEventLoop
from skpy import SkypeEvent
from skpy import SkypeNewMessageEvent
from skpy import SkypeCallMsg
from skpy import Skype
import subprocess
from kivymd.uix.selectioncontrol import MDSwitch
import random
class SkypeRecordApp(MDApp):
def func(self, dt):
class MySkype(SkypeEventLoop):
def onEvent(self, event):
print('Проверка...')
try:
print(event.msg.content)
if event.type == 'NewMessage':
print(event.msg.content)
check_for_call = event.msg.content[:6]
if check_for_call == """{"cp":""":
print("Входящий звонок")
except:
pass
if __name__ == "__main__":
sk = MySkype("email", 'password', autoAck=True)
sk.subscribePresence() # Only if you need contact presence events.
sk.loop()
#problem here
def click(self, switch, value):
if (value):
self.www = Clock.schedule_interval(self.func, 1)
else:
print('off')
self.www.cancel()
def __init__(self, **kwargs):
super().__init__(**kwargs)
Window.clearcolor = (100, 100, 100, 100)
self.window = MDBoxLayout(opacity=0, md_bg_color=[.2, .68, .93, 1], orientation='vertical')
anim = Animation()
anim = Animation(opacity=1)
anim.start(self.window)
def build(self):
self.theme_cls.material_style = "M3"
Window.size = (450, 600)
logo_screen = MDBoxLayout(orientation="vertical")
logo_image = Image(source='skype.png',pos_hint={"center_x":.3, "center_y":.19})
logo_screen.add_widget(logo_image)
logo_screen.add_widget(Label(text="Skyper", font_name="Segoe UI Bold.ttf", font_size=58, pos_hint={"center_x":.3, "center_y":.1}))
self.window.add_widget(logo_screen)
switch = MDSwitch(pos_hint={"center_x":.28, "center_y":.1}, track_color_active="white", on_active=self.click)
self.window.add_widget(switch)
switch.bind(active=self.click)
rec = Label(text="Record", font_size=28, pos_hint={"center_x":.3, "center_y":.1}, size_hint_y=None)
self.window.add_widget(rec)
self.window.add_widget(BoxLayout(orientation="vertical"))
return self.window
SkypeRecordApp().run()
I trying to get events of SkPy with kivymd MDSwitch (when is active), but im already create python kivy loop with
self.www = Clock.schedule_interval(self.func, 1)
problem in because is that loop, Python kivy app stopping when this work, I know about cycles in kivy (can't use in kivy), but how I can do this?
sk.loop()
How I can get events of SkPy in Python kivy?
I have put the video inside kivy canvas, first 3 sec showing image afterthat will be play video. But only show all time image only.
Any suggestion.
Thanks
import kivy
from kivymd.app import MDApp
from kivy.uix.widget import Widget
from kivy.graphics import Rectangle ,Color
from kivy.core.video import Video as CoreVideo
from kivy.clock import Clock
class Mycanvas(Widget):
def __init__(self,**kw):
super(Mycanvas,self).__init__(**kw)
with self.canvas:
self.bg = Rectangle(source='Flower.jpg', pos=self.pos, size=self.size)
self.video = CoreVideo()
self.video.bind(on_frame=self.set_bg_texture)
self.video.filename = 'Garden.mp4'
Clock.schedule_once(self.start_vid, 3)
self.start_vid(3)
def start_vid(self, dt):
self.video.play()
def set_bg_texture(self, *args):
self.bg.texture = self.video.texture
class mainapp(MDApp):
def build(self):
return Mycanvas()
mainapp().run()
Hello i'm relatively new to python and kivy and I've been looking around but i cant quite find the answer, i'm trying to make a hub where I can display live data from my rasberry-pi. I made a clock widget using a Label, and it updates time, but when i try to add my CPU usage only one label shows up. I thought the widgets might be covering each other but I used size_hint and it wont help. When i run the code below, only the timex will show up and only if i delete the Clock.schedule_interval(display32.timex, (1)) will the *updatex * display.
Thanks alot for the help
from kivy.app import App
from kivy.uix.label import Label
from kivy.clock import Clock
from kivy.uix.floatlayout import FloatLayout
import psutil
from kivy.lang import Builder
import time
class Display(Label, FloatLayout):
def __init__(self):
super(Display, self).__init__()
def updatex(self, *args):
self.cpu2 = psutil.cpu_percent()
self.text=str(self.cpu2)
self.size_hint=(.5, .25)
self.pos=(500, 500)
self.texture_size=(0.1,0.1)
def timex(self, *args):
self.time2 = time.asctime()
self.text = str(self.time2)
self.size_hint = (.5, .25)
self.pos = (30, 500)
self.size_hint=(0.1,0.1)
class TimeApp(App):
def build(self):
display32 = Display()
Clock.schedule_interval(display32.updatex, (1))
Clock.schedule_interval(display32.timex, (1))
return display32
if __name__ == "__main__":
TimeApp().run()
Instead of passing both labels to the clock, you can define a widget, add both labels and start the clock on the widget.
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.label import Label
from kivy.clock import Clock
import psutil
import time
class Display(Widget):
def draw(self, *args):
self.clear_widgets()
self.add_widget(Label(text=str(psutil.cpu_percent()), pos=(500, 500)))
self.add_widget(Label(text=str(time.asctime()), pos=(30, 500)))
class TimeApp(App):
def build(self):
display32 = Display()
Clock.schedule_interval(display32.draw, 1)
return display32
if __name__ == '__main__':
TimeApp().run()
This question already has answers here:
What is the purpose of the `self` parameter? Why is it needed?
(26 answers)
Closed 3 years ago.
I would like to change a Label-text in Python/Kivy after a swipe event has been detected. Changing the text works basically via the following line
self.Translation.text = "test"
but I have to change the text after detecting a swipe event from another class, in which I call a function to change the label text:
MyWidget.ThisDoesntWork("self_dummy")
In this function the exact same line as above gives me an error.
How can I change the Label-text from class "Swiping_class" calling function "MyWidget.ThisDoesntWork("self_dummy")"?
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.base import EventLoop
from kivy.clock import Clock
from kivy.factory import Factory
from kivy.uix.stacklayout import StackLayout
from kivy.uix.image import Image
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics import *
from kivy.properties import ListProperty
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen
from random import random
import pickle
import random
kv = '''
<ColoredLabel>:
size: (self.size_x,self.size_y)
pos: (0,0) # no effect
background_color:
canvas.before:
Color:
rgba: self.background_color
Rectangle:
pos: self.pos
size: (self.size_x,self.size_y)
'''
Builder.load_string(kv)
class ColoredLabel(Label):
background_color = ListProperty((0,0,0,1))
s_global = Window.size
size_x = s_global[0]
size_y = s_global[1]/3
class MyWidget(BoxLayout):
#init
def __init__(self, **kwargs):
super().__init__(**kwargs)
s_global = Window.size
size_x = s_global[0]
size_y = s_global[1]/3
self.ForeignLanguage = ColoredLabel(text="str_fl", size_hint=(None, None),size = (size_x,size_y), background_color=(0/255,171/255,169/255, 1))
self.Translation = ColoredLabel(text="str_tr", size_hint=(None, None),size = (size_x,size_y), background_color=(45/255,137/255,239/255, 1))
self.Example = ColoredLabel(text="str_ex", size_hint=(None, None),size = (size_x,size_y), background_color=(43/255,87/255,151/255, 1))
self.verticalBox = BoxLayout(orientation='vertical')
self.verticalBox.add_widget(self.ForeignLanguage)
self.verticalBox.add_widget(self.Translation)
self.verticalBox.add_widget(self.Example)
self.Translation.text = "test"
s=Swiping_class()
s.add_widget(self.verticalBox)
self.add_widget(s)
def ThisDoesntWork(self):
print("this is printed")
self.Translation.text = "I wanna change this via fucntion"
print("this is not printed anymore")
class Swiping_class(Screen):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.initial = 0
def on_touch_down(self, touch):
self.initial = touch.x
def on_touch_up(self, touch):
if touch.x < self.initial:
print("swiped left")
MyWidget.ThisDoesntWork("self_dummy")
else:
print("swiped right")
class BoxLayoutDemo(App):
def build(self):
return MyWidget()
if __name__ == '__main__':
BoxLayoutDemo().run()
I think I found a solution. Within your Swiping_class, replace this line:
MyWidget.ThisDoesntWork("self_dummy")
with this line:
MyWidget.ThisDoesntWork(self.parent)
That way, instead of passing a string to your method, you pass the label object, which contains the text attribute you are trying to modify.
How to get access to different widgets in python kivy?
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
class kxApp(App):
def build(self):
gl_main = GridLayout(rows=2,padding=5, spacing=5)
gl_left = GridLayout(padding=5, spacing=5)
gl_middle = GridLayout(padding=5, spacing=5)
gl_right = GridLayout(padding=5, spacing=5)
gl_main.add_widget(gl_left)
gl_main.add_widget(gl_middle)
gl_main.add_widget(gl_right)
gl_left.add_widget(TextInput(text='Input Here'))
gl_middle.add_widget(Label(text='Just Label'))
gl_right.add_widget(Button(text='Remove Input Field', on_press killFunc))
def killFunc(self,obj):
#how to get access to TextInput() and remove it?
kxApp().run()
What is the natural way to 'get' widget to manipulate it?
By saving a reference to the TextInput, you can simply use remove_widget:
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
class kxApp(App):
def build(self):
gl_main = GridLayout(rows=2,padding=5, spacing=5)
gl_left = GridLayout(cols=1, padding=5, spacing=5)
gl_middle = GridLayout(cols=1, padding=5, spacing=5)
gl_right = GridLayout(cols=1, padding=5, spacing=5)
gl_main.add_widget(gl_left)
gl_main.add_widget(gl_middle)
gl_main.add_widget(gl_right)
self.text_input = TextInput(text='Input Here')
gl_left.add_widget(self.text_input)
gl_middle.add_widget(Label(text='Just Label'))
gl_right.add_widget(Button(text='Remove Input Field', on_press=self.killFunc))
return gl_main
def killFunc(self,obj):
self.text_input.parent.remove_widget(self.text_input)
kxApp().run()