Search code examples
pythonuser-interfacekivykivy-language

Cannot get Widget to Update its Size


I'm quite new to kivy and programming in general, and am having issues with my code. I simplified it down a lot to try get to the root issue, but it still seems to be not be working. Basically this kivy file is supposed to produce an orange square, and then change the size of the square to an integer value produced by a sensor at COM7. So far, at startup a square is produced, and the sensor does successfully pass integer values to python. I tried changing the value of the NumericProperty "first_rect," but no matter how I try to redefine first_rect, it's value doesn't change, and the size of the rectangle never changes. I have the feeling that there is something very small and straightforward missing, but I'm not sure what it is.

Any help would be greatly appreciated!

.py file

import serial
import time 
from functools import partial
from kivy.uix.widget import Widget
from kivy.app import App 
#kivy.require("1.10.0")
from kivy.clock import *
from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition 
from kivy.properties import ObjectProperty, NumericProperty
from kivy.properties import StringProperty
from kivy.uix.image import Image
from kivy.uix.label import Label 
from kivy.graphics import *
from kivy.core.window import Window 
from kivy.uix.slider import Slider
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout   
from kivy.config import Config
from kivy.lang import Builder
from functools import partial
import time
i = 10
class Bar(Widget):
    pass

class Trial(Screen):
    rect1 = ObjectProperty(None)

#    Clock.schedule_interval(Trial().update, 1.0/60.0)

    def update(self):

        ser = serial.Serial('COM7', 9600, timeout=0)
        thing = [0,0]
        a = 1
        while a==1:
            Byte = ser.readline()
            Byte = Byte.decode('utf-8')
            Byte = Byte.strip()
            Byte_proc = Byte.split(',')
            if Byte_proc[0] != '':
                try:
                    if Byte_proc[1] != '':
                        Byte_proc[0] = int(Byte_proc[0])
                        Byte_proc[1] = int(Byte_proc[1])
                        TestTrial_SmallApp().first_rect = Byte_proc[0]
                        print(TestTrial_SmallApp().first_rect)
                        dummy_var = int(Byte_proc[0])
                        print(dummy_var)
                        a = 0
                        return dummy_var
                except:
                    pass
            time.sleep(.02)

class TestTrial_SmallApp(App):
    first_rect = NumericProperty(100)
    def build(self):

    #Builder.load_file('TestTrial.kv')
        Trial().add_widget(Bar())
        Clock.schedule_interval(lambda dt: Trial().update(), 1.0/60.0)
#       Trial().rect1.bind(pos = (200,200))
        Trial().update()
        return Trial()

TestTrial_SmallApp().run()

.kv file

#:import utils kivy.utils
#:import Window kivy.core.window
#:import Widget kivy.uix.widget

<Trial>:
    id: "trial"
    rect1: bar_left

    canvas.before:
        Color:
            rgb: 1, 1, 1
        Rectangle:
            pos: self.pos
            size: self.size

        #Rectangle orange bars

    Bar:
        id: bar_left
        pos: 0.25*self.center_x, 0.02*self.height

<Bar>:
    canvas:
        Color:
            rgb: utils.get_color_from_hex('#F68C43')
        Rectangle:
            pos: 300, 300
            size: 300, app.first_rect

Solution

  • In your statements below

    TestTrial_SmallApp().first_rect = Byte_proc[0]
    print(TestTrial_SmallApp().first_rect)
    

    each TestTrial_SmallApp() is creating a new instance of TestTrial_SmallApp class.I think that is not what you want. Create an instance that you assign to a reference and then use that reference to use the instance like

    app = TestTrial_SmallApp()
    app.first_rect = ...
    print(app.first_rect)