I'm writing a clock application (will eventually run on Raspberry Pi, but developing on Windows), and whenever a label updates the changed characters are drawn on top of the old ones, but the old one doesn't disappear.
It looks like this.
Here's my code:
main.py:
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.clock import Clock
from kivy.properties import StringProperty
from kivy.config import Config
import time
class RPiClock(BoxLayout):
timeString = StringProperty()
def __init__(self, **kwargs):
super(RPiClock, self).__init__(**kwargs)
def update(self, *args):
self.timeString = str(time.strftime("%I:%M:%S %p"))
class RPiClockApp(App):
def build(self):
Config.set('graphics', 'width', '800')
Config.set('graphics', 'height', '480')
appWindow = RPiClock()
Clock.schedule_interval(appWindow.update, 1)
return appWindow
if __name__ == "__main__":
RPiClockApp().run()
RPiClock.kv:
<RPiClock>
Label:
id: TimeLabel
text: root.timeString
font_size: '50sp'
What's causing these graphics to stick around?
I had a similar issue, try adding a colored canvas in the background -- in your case I would likely use a black rectangle that fills up your layout. It may solve your problem (it did for me).
Related
I'm using kivy with Python 3.9
I have the window at 800x600
Config.set('graphics', 'resizable', False)
Config.set('graphics', 'width', 800)
Config.set('graphics', 'height', 600)
This for some reason still allows me to later change window size, if there is a way to change that please help.
So I have a few buttons set up in a .kv file, and everything runs smoothly when I run it. The problem comes when I change the size of the window ( either by fullscreen or just messing with the window). When I do this the buttons start to change size randomly and just mess up. Is there a way to make the buttons adjust automaticcly with the size? Or to just make the stay a fixed size and not change at all? Thanks.
Update:
Kivy file
search : search
explore : explore
GridLayout:
cols:1
size: root.width -500, root.height -550
pos: 250,350
GridLayout:
cols:1
TextInput:
id: search
multinline : False
Button:
text:"Search"
Python file
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.config import Config
from kivy.properties import ObjectProperty
import subprocess
Config.set('graphics', 'resizable', False)
Config.set('graphics', 'width', 800)
Config.set('graphics', 'height', 600)
class MyGrid(Widget):
explore = ObjectProperty(None)
def btn_touch_up(self):
from subprocess import Popen, PIPE
subprocess.Popen(['python', 'UI_1.py'])
exit()
class MyApp(App):
def build(self):
return MyGrid()
From the Config documentation:
Configuration options control the initialization of the App. In order
to avoid situations where the config settings do not work or are not
applied before window creation (like setting an initial window size),
Config.set should be used before importing any other Kivy modules.
Ideally, this means setting them right at the start of your main.py
script.
Just move the lines:
from kivy.config import Config
Config.set('graphics', 'resizable', False)
Config.set('graphics', 'width', 800)
Config.set('graphics', 'height', 600)
to the very top of your py file.
I am trying to change to another screen by swiping the screen. I've tried carousel but it seems that it only works with images, so I've tried detecting a swipe motion and changing the screen after it has been detected.
main.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
from kivy.uix.button import ButtonBehavior
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.carousel import Carousel
from kivy.uix.widget import Widget
from kivy.uix.popup import Popup
class HomeScreen(Screen):
def on_touch_move(self, touch):
if touch.x < touch.ox: # this line checks if a left swipe has been detected
MainApp().change_screen(screen_name="swipedhikr_screen") # calls the method in the main app that changes the screen
class ImageButton(ButtonBehavior, Image):
pass
class LabelButton(ButtonBehavior, Label):
pass
class SettingsScreen(Screen):
pass
class SwipeDhikrScreen(Screen):
pass
#def quit_verification():
# pop = Popup(title="verification", content=Label(text= "Are you sure?"))
GUI = Builder.load_file("main.kv")
class MainApp(App):
def build(self):
return GUI
def change_screen(self, screen_name):
# get the screen manager from the kv file
screen_manager = self.root.ids["screen_manager"]
screen_manager.transition.direction = "up"
screen_manager.current = screen_name
def quit_app(self):
MainApp().stop()
MainApp().run()
I got an attribute error: "None type object has no attribute 'ids'"
MainApp().change_screen(screen_name="swipedhikr_screen")
This line creates a new instance of MainApp, which doesn't have any widgets and therefore naturally fails when you try to access them.
Use the existing instance of MainApp, i.e. the one that you're actually running, via MainApp.get_running_app().
Also you are not correct that Carousel works only with images.
I used Config.set('modules', 'monitor', '') on one of my Kivy apps to get the fps bar, but I can't get it disabled now.
Kivy version -- 1.10.1
I've tried everything and checked every line of code but can't get it disabled. Also if I write a basic code with just
from kivy.app import App
from kivy.uix.button import Button
imports I still getting the fps bar. I even reinstalled the whole Kivy environment but still I have the fps bar.
These are all the imports that I used in my file which created all the problem.
import kivy
kivy.require("1.10.1")
# from kivy.config import Config
# Config.set('graphics', 'resizable', 0)
# Config.set('graphics', 'width', 500)
# Config.set('graphics', 'height', 300)
# Config.set('modules', 'monitor', '')
from kivy.metrics import *
from kivy.core.window import Window
Window.size = (sp(500), sp(300))
from kivy.app import App
from kivy.uix.label import Label
from kivy.graphics import Line, InstructionGroup, Color
from kivy.properties import ObjectProperty, Property
from kivy.core.audio import SoundLoader
from kivy.clock import Clock
from kivy.uix.screenmanager import Screen, ScreenManager, FadeTransition
from kivy.lang import Builder
import random as rnd
import time
This is the basic code.
from kivy.app import App
from kivy.uix.button import Button
class TestApp(App):
def build(self):
return Button(text='Hello World')
TestApp().run()
Output Image
From eyllanesc's comment I've found a fix to this problem in case anyone else gets into the same problem.
Note: Always create a backup of the file.
Go to this directory.
Windows: C:\Users\tito\.kivy\config.ini
macOS: /Users/tito/.kivy/config.ini
Linux: /home/tito/.kivy/config.ini
Open config.ini
Find
[modules]
monitor =
Delete monitor = and Save it.
If anything happens just delete the config.ini and Kivy will create a new one but all your configurations will reset.
I have unfortuanlly have encourtered an error in kivy and Python 3. I have not found a soultion via Google. I wanted to get text input at the very least but it does not show up. Just the text itself. Thank you for your time!
import kivy
kivy.require('1.10.1') # replace with your current kivy version !
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
class ColdKivyApp(App):
def build(self):
f = FloatLayout()
label = Label(text="Cold") #I acutally orginally called it Zone unitil I changed it into Cold cause it's really cold now
f.add_widget(label)
txt = TextInput(text='', focus=True, multiline=True, cursor_blink=True, background_color=(1,1,1,1))
f.add_widget(txt)
return f
if __name__ == '__main__':
ColdKivyApp().run()
It seems that there is a bug in TextInput when setting the focus in the constructor, a workaround is to set the focus an instant after the window is shown through Clock:
import kivy
kivy.require('1.10.1') # replace with your current kivy version !
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.clock import Clock
class ColdKivyApp(App):
def build(self):
f = FloatLayout()
label = Label(text="Cold")
f.add_widget(label)
txt = TextInput(multiline=True, cursor_blink=True, background_color=(1,1,1,1))
f.add_widget(txt)
Clock.schedule_once(lambda *args: setattr(txt, "focus", True))
return f
if __name__ == '__main__':
ColdKivyApp().run()
I've tried to create an Icon that can be clicked, which means an Image with a ButtonBehavior.I followed the documentation (http://kivy.org/docs/api-kivy.uix.behaviors.html), and I've got a FactoryException with the following code:
# coding: utf-8
from kivy.uix.behaviors import ButtonBehavior
from kivy.core.image import Image
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
kv_string = """
BoxLayout:
IconButton
"""
class IconButton(ButtonBehavior, Image):
def on_press(self):
print("on_press")
class DashboardApp(App):
pass
Builder.load_string(kv_string)
if __name__ == "__main__":
DashboardApp().run()
When I change the parent class of IconButton from (ButtonBehavior, Image) to (ButtonBehavior, Widget), the problem disappears.
You want kivy.uix.image, not kivy.core.image.