Forum Archive

Plotting on a single screen with text fields

avv

Give me an example of the output of the graphic information below ui.Textfield.

JonB

Can you clarify your question? What is it you are trying to do exactly? Are you trying to display some kind of ascii art in a textfield? Textfields are intended to display text, not graphics (in general)

avv

There teksovoe field. Below, you need to draw a graph based on the entered text

avv

In other words, the need for an example screen display of a TextField and below the graph.

ccc

The more you type, the smaller the red oval gets...

import ui

class GraphicsBelowTextView(ui.View):
    def __init__(self):
        self.hidden = True
        self.present()

        text_field = ui.TextField()
        text_field.width = self.bounds[2]
        text_field.height = 25
        text_field.delegate = self
        self.add_subview(text_field)

        self.image_view = ui.ImageView()
        self.image_view.frame = self.bounds
        self.image_view.y      += text_field.height
        self.image_view.height -= text_field.height
        self.add_subview(self.image_view)

        self.textfield_did_change(text_field)
        self.hidden = False

    def textfield_did_change(self, textfield):
        _, _, w, h = self.image_view.frame
        min_wh = min(w, h)
        x = (len(textfield.text) + 1) * min_wh / 100
        with ui.ImageContext(w, h) as ctx:
            ui.set_color('white')
            ui.fill_rect(0, 0, w, h)
            ui.set_color('red')
            circle = ui.Path.oval(x, x, w-2*x, h-2*x)
            circle.fill()
            self.image_view.image = ctx.get_image()

GraphicsBelowTextView()
avv

It works. How to implement what is written in the documentation:

"integration with the ui Module

If you want to combine the render loop-based drawing functionality of the scene modules with standard user interface elements, like text fields, you can do so with the SceneViewclass, which inherits from ui.View. Using a SceneView is an alternative to presenting a scene with the run() function."
Thanks for the detailed answer.

ccc
import scene, ui

class MinScene(scene.Scene):
    def __init__(self):
        self.set_char_count(0)

    def set_char_count(self, char_count):
        self.char_count = char_count

    def draw(self):
        scene.background(0.40, 0.80, 1.00)
        _, _, w, h = self.bounds
        min_wh = min(w, h)
        x = (self.char_count + 1) * min_wh / 100
        scene.fill(1, 0, 0)
        scene.ellipse(x, x, w-2*x, h-2*x)

class SceneBelowTextView(ui.View):
    def __init__(self):
        self.hidden = True
        self.present()

        text_field = ui.TextField()
        text_field.width = self.bounds[2]
        text_field.height = 25
        text_field.delegate = self
        self.add_subview(text_field)

        self.scene_view = scene.SceneView()
        self.scene_view.frame = self.bounds
        self.scene_view.y      += text_field.height
        self.scene_view.height -= text_field.height
        self.scene_view.scene = MinScene()
        self.add_subview(self.scene_view)

        self.hidden = False

    def textfield_did_change(self, textfield):
        self.scene_view.scene.set_char_count(len(textfield.text))

SceneBelowTextView()
avv

Thank you so much for the prompt support. This is perhaps the best forum in the internet!