Some Questions (unrelated)

68 views
Skip to first unread message

Dan Moormann

unread,
Sep 2, 2023, 9:33:59 PM9/2/23
to Kivy users support
1) Is there any way to remove the red dot used in scatter after it has been used, so that it can be used multiple times?
I want to clear the screen of the label and return it to its original form.  I got everything working with a clear button and setting everything back to normal.  But I just can't remove the red dot.

I tried to use 
scatter.do_rotation = True
scatter.do_translation_x = True
scatter.do_translation_y = True
but that just turns everything off. Is there an alternative that just removes the dot for reuse?

2} Does the
with self.canvas:
Color(1., 0, 0)
Rectangle(pos=(10, 10), size=(50, 50))
Color(0, 1, 0)
Rectangle(pos=(100, 100), size=(50, 50))
Produce only filled in rectangles?  Can it do outlines only?

3) Is there a way to prompt the user for input on the fly.  For example, after pressing a button could I ask the user to respond (perhaps to multiple items)?

Elliot Garbus

unread,
Sep 3, 2023, 10:05:46 AM9/3/23
to kivy-...@googlegroups.com
For 1, see: https://kivy.org/doc/stable/api-kivy.input.providers.mouse.html#using-multitouch-interaction-with-the-mouse

For 2: Rectangle does a filled rectangle, see Line to create an outline of a shape. https://kivy.org/doc/stable/api-kivy.graphics.html#kivy.graphics.Line



Sent from my iPhone

On Sep 2, 2023, at 6:34 PM, Dan Moormann <dan...@gmail.com> wrote:

1) Is there any way to remove the red dot used in scatter after it has been used, so that it can be used multiple times?
--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/86133abf-e2e1-41bd-9966-3c8360cd8d3an%40googlegroups.com.

AllAbout Coding

unread,
Sep 3, 2023, 11:50:49 AM9/3/23
to kivy-...@googlegroups.com
we want webview

Dan Moormann

unread,
Sep 3, 2023, 9:05:37 PM9/3/23
to kivy-...@googlegroups.com
Thanks - Looks like heavy reading for Tomorrow.

Dan Moormann

unread,
Sep 4, 2023, 7:39:15 PM9/4/23
to kivy-...@googlegroups.com
I read through the section on mouse providers, but didn;t see any reference to the infamous red dot.  In the first paragraph it said On Linux systems...  I'm on a windows system.  Is there anything else that directly addresses the red dot?

Dan Moormann

unread,
Sep 4, 2023, 7:42:35 PM9/4/23
to kivy-...@googlegroups.com
I'm already using both rectangles and line(rectangle() and turtle to do some simple graphics.  Besides turtle (or creating many many label widgets)  Is there a way to put text directly to the screen?

elli...@cox.net

unread,
Sep 4, 2023, 7:51:58 PM9/4/23
to kivy-...@googlegroups.com

This works on Windows and Mac to disable the red dot.  The red dot is multi-touch emulation.

 

from kivy.config import Config

Config.set('input', 'mouse', 'mouse,disable_multitouch')

 

Let me know this is sufficient.

elli...@cox.net

unread,
Sep 4, 2023, 7:58:46 PM9/4/23
to kivy-...@googlegroups.com

You would just create Labels to put text on the screen.  What are you trying to accomplish?

 

From: kivy-...@googlegroups.com <kivy-...@googlegroups.com> On Behalf Of Dan Moormann
Sent: Monday, September 4, 2023 4:42 PM
To: kivy-...@googlegroups.com
Subject: Re: [kivy-users] Some Questions (unrelated)

 

I'm already using both rectangles and line(rectangle() and turtle to do some simple graphics.  Besides turtle (or creating many many label widgets)  Is there a way to put text directly to the screen?

Dan Moormann

unread,
Sep 4, 2023, 10:05:59 PM9/4/23
to Kivy users support
I tried the Config.Set(...) Eliot suggested but nothing happens when I invoke it.  The red dot is still there.  No error, just nothing.

I'm trying to find an alternative to Turtle that would let me place text directly on the screen in multiple locations and colors if possible.  If you still have access to any of my earlier code, try the draw option to get an idea.

I read the section on Popup and got the first example to work.  Couldn't get content=Button(...) to work at all.  Error Button is not defined.
I'm really looking for a way to prompt the user to input some data for various reasons programmatically.

I'm puzzled why so many of the examples I try do not work (Probably just me)

I frequently have to wonder which item to import to get something new (for me) to work.  Please include "from xxx import yyy" on more examples.

elli...@cox.net

unread,
Sep 4, 2023, 10:10:22 PM9/4/23
to kivy-...@googlegroups.com

For the config.set, make sure you put this at the very top of your file.  You want to execute the Config.set before kivy.app is imported.

 

You can use a Label to put the text where you want.  Set the size, put it in a FloatLayout or RelativeLayout and use pos or pos_hint to position the text.

 

I’ll put together a small popup example.  What do you want to do with the Popup?

elli...@cox.net

unread,
Sep 4, 2023, 10:15:46 PM9/4/23
to kivy-...@googlegroups.com

Here is a popup example that uses Factory to instance the popup in kv.

 

from kivy.app import App
from kivy.lang import Builder

kv =
"""
#:import Factory kivy.factory.Factory

<MyPopUp@Popup>:
    size_hint: .7, .7
    title: 'hello'
    id: popup
    BoxLayout:
        orientation: 'vertical'
        Label:
            text: 'Hello, I am a PopUp!'
        Button:
            text: 'Dismiss'
            size_hint_y: None
            height: dp(48)
            on_release: root.dismiss()

BoxLayout:
    orientation: 'vertical'
    padding: 10
    Label:
        text: 'Use Factory to Instance a PopUp in KV'
    Button:
        text: 'Push to create MyPopUp'
        size_hint_y: None
        height: dp(48)
        on_release: Factory.MyPopUp().open()  
"""


class PopApp(App):
   
def build(self):
       
return Builder.load_string(kv)

PopApp().run()

 

 

From: kivy-...@googlegroups.com <kivy-...@googlegroups.com> On Behalf Of Dan Moormann

Sent: Monday, September 4, 2023 7:06 PM

elli...@cox.net

unread,
Sep 4, 2023, 11:15:39 PM9/4/23
to kivy-...@googlegroups.com

Here is another small popup example – perhaps a little more useful.  I added a text input.  I instanced the popup in app, just to show a different way of doing it – rather than using the Factory.

 

from kivy.app import App
from kivy.lang import Builder

from kivy.uix.popup import Popup
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty

kv =
"""
<MessagePop>:
    title: 'Update Message'
    size_hint: .7, .7
    BoxLayout:
        orientation: 'vertical'
        AnchorLayout:
            TextInput:
                id: input
                size_hint: None, None
                size: 200, 40
                hint_text: 'Enter Message'
                on_text_validate: app.root.text = self.text
        Button:
            size_hint_y: None
            height: dp(48)
            text: 'Update Message'
            on_release:
                app.root.text = input.text
                root.dismiss()
                input.text = ''

RootBoxLayout:
    orientation: 'vertical'
    Label:
        id: msg
        text: root.text
    Button:
        size_hint_y: None
        height: dp(48)
        text: 'Update Message'
        on_release: app.pop.open()
"""


class MessagePop(Popup):
   
pass


class
RootBoxLayout(BoxLayout):
    text = StringProperty(
'Your message goes here')


class PopApp(App):
   
def __init__(self, **kwargs):
       
super().__init__(**kwargs)
       
self.pop = None  # used to hold the popup

   
def build(self):
       
return Builder.load_string(kv)

   
def on_start(self):
       
self.pop = MessagePop()  # instance the popup after the kv has been processed


PopApp().run()

From: kivy-...@googlegroups.com <kivy-...@googlegroups.com> On Behalf Of Dan Moormann
Sent: Monday, September 4, 2023 7:06 PM

Dan Moormann

unread,
Sep 5, 2023, 12:24:08 PM9/5/23
to kivy-...@googlegroups.com
I like your second example better.  Trying to figure out how to make the textinput box in the popup automatically have focus when the popup is launched. Any suggestions?

Elliot Garbus

unread,
Sep 5, 2023, 12:50:36 PM9/5/23
to kivy-...@googlegroups.com
Use the popup event on_open to set the focus. 

Sent from my iPhone

On Sep 5, 2023, at 9:25 AM, Dan Moormann <dan...@gmail.com> wrote:



ElliotG

unread,
Sep 5, 2023, 1:33:19 PM9/5/23
to Kivy users support
Adding focus to the previous example:

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import StringProperty

kv = """
<MessagePop>:
    title: 'Update Message'
    size_hint: .7, .7
    on_open: input.focus = True

Dan Moormann

unread,
Sep 5, 2023, 3:22:48 PM9/5/23
to kivy-...@googlegroups.com
Thank you.  I tried every other option but there.  Now it works great.

Dan Moormann

unread,
Sep 5, 2023, 4:59:01 PM9/5/23
to kivy-...@googlegroups.com
I got your stand alone code to work.  In trying to integrate it into my code, I can't figure out how to initiate it from a button in my main

I added all of the kv code to my kv file only  making changes to text and messages
I added all the kivy code at the bottom of my code inserting some print statement so see who;s executing

   MesagePop() executes before execution main loop
   RootBoxLayout executes before execution main loop

I tried MessagePop()  - nothing happens (no error)
I tried PopApp() - it gets there but nothing else
i tried RootBoxLayout() - nothing happens (no error)

Not sure where to go????


elli...@cox.net

unread,
Sep 5, 2023, 5:50:03 PM9/5/23
to kivy-...@googlegroups.com

Looking at the example I wrote.

In on_start():

                The Popup, MessagePop() is instanced and assigned to the instance variable self.pop

 

In the kv code:

RootBoxLayout:
    orientation: 'vertical'
    Label:
        id: msg
        text: root.text
    Button:
        size_hint_y: None
        height: dp(48)
        text: 'Update Message'
        on_release: app.pop.open()

app.pop is the instance variable pop that was assigned the instance of MessagePop(), the open() method opens the Popup.

 

If this is not sufficient to get things working, share your code.

Dan Moormann

unread,
Sep 5, 2023, 9:57:01 PM9/5/23
to kivy-...@googlegroups.com
popup - I added the on_release: app.open() to line 25 kvfile - still won't fire.

Line(rectangle...) - I'm trying to use a variable (line 229 main) to set the color before displaying the rectangle - defaults to white
You will need a copy of colorcodes.txt stored in downloads or change line 160/161 main to any other directory (included with my code)
There's probably a better way to do the lookup, but I couldn't get Table to work.

Lots of print statements (one of these days I'll figure out debug)

colorcodes.txt  
[255, 255, 255, 1]   *(1, 1, 1)      *white
[0, 255, 255, 1]     *(0, 1, 1]      *aqua
[255, 0, 0, 1]       *(1, 0, 0)      *red
[0, 128, 0, 1]       *(0, 1, 0)      *green
[0, 0, 255, 1]       *(0, 0, 1)      *blue
[128, 0, 128, 1]     *(.5, 0, .5)    *purple
[255, 255, 0, 1]     *(1, 1, 0)      *yellow
[0, 255, 0, 1]       *(0, 1, 0)      *lime
[0, 128, 128, 1]     *(0, .5, .5)    *teal
[0, 0, 0, 1]         *(0, 0, 0)      *black
[255, 0, 255, 1]     *(1, 0, 1)      *fuchsia
[128, 128, 128, 1]   *(.5, .5, .5)   *gray
[128, 0, 0, 1]       *(.5, 0, 0)     *maroon
[0, 0, 128, 1]       *(0, 0, .5)     *navy
[128, 128, 0, 1]     *(.5, .5, 0)    *olive
[192, 192, 192, 1]   *(.75, .75, .75)*silver

kv file

#:kivy 1.0.9

<MessagePop>
title: 'Pop goes the weasel'
size_hint: .2, .2

on_open: input.focus = True

BoxLayout:
orientation: 'vertical'
AnchorLayout:
TextInput:
id: input
size_hint: None, None
size: 200, 40
hint_text: 'Please respond'

on_text_validate: app.root.text = self.text
Button:
size_hint_y: None
height: dp(48)
text: 'Press when finished'

on_release:
app.root.text = input.text
root.dismiss()
input.text = ''
on_release: app.open()


RootBoxLayout:
orientation: 'vertical'
Label:
id: msg
text: root.text
Button:
size_hint_y: None
height: dp(48)
text: 'xxx - title'
on_release: app.pop.open()

#-------------------------------------

<ScatterTextWidget>:
orientation: 'vertical'
id: 'Main'
name: 'Main'
effect_cls: 'ScrollEffect'

Label:
id: titl
name: 'titl'
text: 'Enter Text - Version: '+kivy.platform
size_hint_y: None
height: dp(50)
font_size: sp(20)
pos: 0,550
background_color: 0,255,255,1 #'aqua'

TextInput:
id: txti
name: 'txti'
size_hint_y: None
height: dp(50)
font_size: '25sp'
pos: 0,500
focus: True
on_text: helo.text = self.text #change helo to match txti entered
background_color: 128,128,128,1 #gray

AnchorLayout: # use to center the row of buttons
id: anch
name: 'anch'
BoxLayout: # hold the row of buttons in a horizontal layout
id: boxl
name: 'boxl'
size_hint_y: None
height: dp(70) # set the height of the buttons
spacing: dp(5) # set spacing between the buttons
padding: dp(5) # padding around the outside of the buttons

Button:
id:lsiz
name: 'lsiz'
text: " Press\n to\ndisplay"
on_press: root.Listsizes(0)
on_release: root.set_focus()
background_color: 0,255,0,1 #green
color: 'black' #text color

Button: #list sizes to file
id: lfil
name: 'lfil'
text: "Press\n to\n file"
on_press: root.Listsizes(1)
on_release: root.set_focus()
background_color: 0,0,255,1 #blue
color: 'black'

Button: #clear
id: clea
name: 'clea'
text: 'Clear'
on_press: root.clear_it()
background_color: 255,0,0,1 #red
color: 'black'

Button: #drat
id: drat
name: 'drat'
text: "Draw\nTurtle"
on_press: root.drawit()
on_release: root.set_focus()
background_color: 128,0,128,1 #'purple'
color: 'black'

Button: #dral
id: dral
name: 'dral'
text: "Draw\nLine\nRect"
on_press: root.draw_recs()
on_release: root.set_focus()
background_color: 128,0,128,1 #'purple'
color: 'black'

Button: #mode
id: mode
name: 'mode'
text: 'Mode\n'+root.xltxt
on_press: root.set_mode()
on_release: root.set_focus()
background_color: 255,255,0,1 #yellow
color: 'black'

Button: #xtest
id: test
name: 'test'
on_press: root.xtest()
text: 'xtest'
background_color: 128,128,128,1 #grey
color: 'black'


Label: #widget display text
id: disp
name: 'disp'
font_name: root.xfontname
font_size: 14
size_hint_y: None
size: 800,400
pos: 0,0
background_color: 255,255,0,1 #yellow
color: 'yellow'

RelativeLayout:
id: rela
name: 'rela'
gid: rela
size_hint: None, None # use a relative layout so the position of the scatter widget is honored
size: 0, 0 # make the size zero so the Layout does not take any space

TextInput:
id: txt2
name: 'txt2'
size_hint: None,None
size: 200,30
pos: 0,375
#text: 'txt2'
color: 255,255,255,1 #white
background_color: 0,0,0,1 #black

Scatter:
id: scat
name: 'scat'
gid: scat
size_hint: None, None
size: helo.texture_size # make the Scatter the same size as the text "Hello!"
pos: disp.center_x - helo.width/2, disp.top-helo.height
#pos: 0,60
background_color: 0,255,0,1 #'lime'

Label: #hello
id: helo
name: 'helo'
text: "Hello!"
size_hint: None, None
size: scat.size
#pos: disp.center_x - helo.width/2, disp.top-helo.height
pos: 0,0
font_size: 50
color: 'teal'
background_color: 0,128,128,1 #'teal'
#on_touch_down: print('dn',int(scat.pos[0]),int(scat.pos[1]),end=' ')
#on_touch_up: print('up',int(scat.pos[0]),int(scat.pos[1]))

ScrollView:
id: scrv
name: 'scrv'
size_hint: None,None
size: 257,340 #hard coded to emulated width of Samsung S23 (1080/4.2)
scroll_type: ['content']
do_scroll_y: True
scroll_y: 1 #top of scrollview

Label:
id: scro
name: 'scro'
font_size: 12
size_hint: None,None
size: scrv.size
font_name: root.xfontname
height: self.texture_size[1]
#pos: 257,340
background_color: 255,255,0,1 #yellow
color: 'yellow'

# Label:
# id: spot
# name: 'spot'
# #text: '+'
# pos: 10,10
# background_color: 255,255,0,1 #yellow
# color: 'yellow'


main.py
from kivy.config import Config
import os
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.scrollview import ScrollView
from kivy.properties import StringProperty
#from kivy.properties import ColorProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.utils import platform
from kivy.core.window import Window
from webcolors import rgb_to_name
#from PIL import Image
from kivy.graphics import Line
#from kivy.graphics import Rectangle
#from kivy.graphics import *
from PIL import ImageGrab
from kivy.core.text import Label as CoreLabel
from kivy.clock import Clock
import kivy.core.text
from inspect import currentframe, getframeinfo
from kivy.graphics import *
from kivy.uix.popup import Popup
import win32gui
hwnd = win32gui.GetForegroundWindow()
win32gui.MoveWindow(hwnd, 1380,50,816,639,True) #move console output to monitor 1

xmode= 'W' # Win/Emulate/Android
xx600=Window.height
xx300=xx600/2
xx800=Window.width
xx400=xx800/2

class ScatterTextWidget(BoxLayout):
#background_color = ColorProperty() # The ListProperty will also work.
xltxt = "Windows"
if platform == 'android':
xmode="A"
from android.storage import primary_external_storage_path
dir = primary_external_storage_path()
xfontname=os.path.join(dir,'/system/fonts/DroidSansMono.ttf')
xxfile = os.path.join(dir, 'Download') + '/Crash.txt'
else:
xfontname='C:/Windows/Fonts/Courbd'
xxfile = os.path.join(os.path.expanduser("~"), "Downloads") + '\\Crash.txt'
xtfile = os.path.join(os.path.expanduser("~"), "Downloads") + '\\turtle.png'
xmode='W'
#class end scattertextwidget

def __init__(self, **kwargs):
super(ScatterTextWidget, self).__init__(**kwargs)
#self.save_initial_pos_size('init')
return

def on_kv_post(self, base_widget):
#self.save_initial_pos_size('kvpost')
Clock.schedule_once(self.after_post) # provide another clock tick...

def after_post(self, dt):
self.save_initial_pos_size('afterpost')

def save_initial_pos_size(self,xcaller):
#self.print_it(xcaller)
self.xihp = self.ids.helo.pos
self.xihs = self.ids.helo.size
self.xisp = self.ids.scat.pos
self.xiss = self.ids.scat.size
return

# def spot_it(self):
# label=Label(text='+',pos=(100,100))
# label.add_widget(Label)
# return

# def print_it(self, xcaller):
# # print(self.ln(),xwho,'scat',self.ids.scat.pos,self.ids.scat.size)
# # print(self.ln(),xwho,'helo',self.ids.helo.pos,self.ids.helo.size)
# print(self.ln(), 'scrv', self.ids.scrv.pos, self.ids.scrv.size,
## 'scro', self.ids.scro.pos, self.ids.scro.size,
# 'scat', self.ids.scat.pos, self.ids.scat.size,
# 'helo', self.ids.helo.pos, self.ids.helo.size, xcaller)
# return

def clear_it(self):
for i in range(2):
self.ids.scat.pos = self.xisp # reset to initial scat.pos/size/scale/rotation
#self.ids.scro.size_hint_y=None
if self.xmode!='W':
self.ids.scat.pos=64,341
#self.ids.spot.pos=self.ids.scat.pos
else:
self.ids.scat.pos=self.xisp
self.ids.scat.size = self.xiss
self.ids.scat.scale = 1
self.ids.scat.rotation = 0
#self.ids.scro.size= 257,0

self.ids.helo.pos = self.xihp # reset helo
self.ids.helo.size = self.xihs
self.ids.helo.text = 'Hello!'

self.ids.disp.text = ''
self.ids.scro.text = ''
self.ids.txti.text = ''

Config.set('input', 'mouse', 'mouse,disable_multitouch')
print('config',i)
return # clear_it
def set_focus(self):
if platform == "win":
self.ids.txti.focus = True
return #set_focus

def set_mode(self):
if self.xmode=="A": #leave everything alone
return
if self.xmode=="E": #switch to Windows mode
Window.size= (xx800,xx600)
self.ids.disp.font_size = 14
self.xmode = "W"
self.xltxt='Windows'
else: #switch to Emulate mode
self.xresize = 4.2
x=self.xresize
Window.size = (1080 / x, 2340 / x) # 1080x2340 samsung s23 result 257x557
#self.ids.disp.font_size = 8
self.xmode='E'
self.xltxt='Emu\nlate'
self.ids.mode.text='Mode\n '+self.xltxt
self.ids.disp.background_color = 255, 255, 0, 1 # yellow
#self.ids.disp.text=str(self.ids.disp.background_color)+' '+str(self.ids.disp.font_size)
self.clear_it()
return #set_mode


def xtest(self):
#popup
print('xtext pop')
RootBoxLayout()
return

#put text anywhere
self.ids.txt2.background_color=(255,0,0,1) #red
#self.ids.txt2.pos=200,200

self.ids.txti.focus=False
self.ids.txt2.focus=True


#self.draw_recs()

#x=self.get_widget_colors('name')
#print(x)

#self.set_mode() #calls clear_it

#self.ids.disp.text='Test!'
return #xtest
#
def get_widget_colors(self,xname_num):
#xcfile = 'c:/Users/danm9/Crash/colorcodes.txt'
xcfile = os.path.join(os.path.expanduser("~"), "Downloads") + '\\colorcodes.txt'
#read file and set up array
with open(xcfile) as f:
xa=[]; xb=[] ; xc=[] ; xcc=[] ; xnn=[] #setup empty array
for line in f:
x= line.strip('\n').split("*")
xa.append(x[0].rstrip()) ; xb.append(x[1].rstrip()) ; xc.append(x[2].rstrip())

#lookup funtion
xcnt=0
for widget in self.walk():
xcnt=xcnt+1
if hasattr(widget, 'background_color'): # build the color list
xlook = widget.background_color # s/b x,x,x,1
else:
xlook = [255,255,255,1] #default to white (no background_color)
for i in range(len(xa)):
if str(xa[i]) == str(xlook):
xcc.append(xc[i])
xnn.append(xb[i])
break
# else:
# print()
if xname_num=='name':
print(xcc)
return xcc
else:
return xnn

def draw_recs(self):
xxx=self.get_widget_names()
xxx.remove('Main')
xxc=self.get_widget_colors('number')
#xxc.remove('white')
xpos = [self.ids[idx].pos for idx in xxx] #
xsiz = [self.ids[idx].size for idx in xxx] #
print(xxx)
print(xxc)
i=0 #index number from pos, size
for x in xxx: #run through widget names
x1 = xpos[i] #break out x and y values for pos
xx1=x1[0]
xy1=x1[1]
x2 = xsiz[i] #break out x and y values for size
xx2=x2[0]
xy2=x2[1]
with self.canvas:
Color(xxc[i-1])
Line(rectangle=(xx1,xy1,xx2,xy2))
print(i+1,xxx[i],xxc[i+1])
Color(255, 0, 0)
Rectangle(pos=(10, 100), size=(50, 50))

Color(0, 1, 0)
Rectangle(pos=(100, 100), size=(50, 50))

#self.canvas.add(rect)
i+=1 #increment for next widget

def ln(self): #current line number
frameinfo = getframeinfo(currentframe().f_back)
return str(frameinfo.lineno)+':'

def get_widget_names(self):
xxx=[]
for widget in self.walk():
xxx.append(widget.name) #build the name list
return xxx #get_widget_name

def old_get_widget_colors(self,xname_num):
#ooo
xxc=[]
for widget in self.walk():
if hasattr(widget,'background_color'): #build the color list
x4=widget.background_color #s/b x,x,x,1

if xname_num=='name':

for i in range(3): #fix 1,1,0,1 yellow
if x4[i]==1:
x4[i]=255
x5=rgb_to_name(x4)
else:
x5='white'
xxc.append(x5)
return xxc #get_widget_colors

def build_lxx(self,xline_in):
#print(self.ln(),' 1 2 3 4 5 6 7')
#print(self.ln(),'1234567890123456789012345678901234567890123456789012345678901234567890')
xfirst_half=' '+xline_in[:32]
xwname=xline_in[:5]
if xwname=='Outpu' and self.xmode!='W': #clean off Outp as if it's a widget name
xwname=''
xsecond_half=xline_in[33:]
self.lxx= self.lxx+ xline_in+'\n' #entire line
self.lxx1=self.lxx1+xfirst_half+'\n' #first half of line
self.lxx2=self.lxx2+xwname+' '+xsecond_half+'\n' #second half
return

def Listsizes(self,xf): #0=list, 1=file
self.lxx=''
self.lxx1=''
self.lxx2=''
self.ids.disp.font_size = 14
# xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx xxxx
self.build_lxx(' ----pos- --size-- -------- hints -------')
self.build_lxx(' acr up wid hig parent color posx posy sizx sizy')
# 1 2 3 4 5 6
# 12345678901234567890123456789012345678901234567890123456789012
#self.ids.disp.font_size=12
xxx = self.get_widget_names()
xxx.remove('Main')
xxc = self.get_widget_colors('name')
xxc.remove('white')
xparent = [self.ids[idx].parent.name for idx in xxx]
xppos = [self.ids[idx].parent.pos for idx in xxx]
xpsiz = [self.ids[idx].parent.size for idx in xxx]
xpos = [self.ids[idx].pos for idx in xxx]
xsiz = [self.ids[idx].size for idx in xxx]
#main requires seperate approach
self.build_lxx(self.pull_out('main', self.pos, self.size,' ',(0,0),(0,0),''))
i=0
for x in xxx:
# pass in tuples and parent
self.build_lxx(self.pull_out(x,xpos[i],xsiz[i],xparent[i],xppos[i],xpsiz[i],xxc[i]))
i+=1
if xf==1:
file1 = open(self.xxfile,'w+')
self.build_lxx("Output in <downloads> Crash.txt") #+self.xxfile)
file1.write(self.lxx)

self.ids.disp.color='yellow'
if self.xmode!='W': #send output to scroll
self.ids.disp.font_size=12
self.lxx=self.lxx1+'\n'+self.lxx2+'\n'
self.ids.scro.text=self.lxx
else:
self.ids.disp.text = self.lxx

return #listsizes

def pull_out(self,xname,x_pos, x_siz,x_parent,x_ppos,x_psiz,x_color):
#dont need to use unpack as cannot unpack scatter
x1, xx1 = x_pos[0],x_ppos[0]
y1, xy1 = x_pos[1],x_ppos[1]
x2, xx2 = x_siz[0],x_psiz[0]
y2, xy2 = x_siz[1],x_psiz[1]
xr="{0:5.0f}{1:5.0f} {2:5.0f}{3:5.0f}"
xy=xr.format(x1,y1,x2,y2)
xy=xname+xy #add name of widget to beginneing of line
xy=xy+' ' +x_parent
if xname=="main":
#preserve values for calculating hints - nothing to output for main
global xarc,xup,xwid,xhig,m_wid,m_hig
xarc, xup, xwid, xhig = x1,y1,x2,y2
m_wid, m_hig = xwid, xhig
else:
#calculate hints
xy=xy+' '+self.calc_pos_size(x1,y1,x2,y2,xx1,xy1,xx2,xy2,x_color)
#xy=xy+' '+x_color
return xy #pull_out

def calc_pos_size(self,acr,up,wid,hig,p_acr,p_up,p_wid,p_hig,xcolor):
pos_x= 0 if p_wid==0 else acr/p_wid
pos_y= 0 if p_hig==0 else up/p_hig
if pos_y > 1:
pos_y=(up-p_up)/p_hig
siz_x= 0 if p_wid==0 else wid/p_wid
siz_y= 0 if p_hig==0 else hig/p_hig
xr="{0:6}{1:5.2f} {2:5.2f} {2:5.2f} {2:5.2f}"
xy=xr.format(xcolor,pos_x,pos_y,siz_x,siz_y)
return xy #calc_pos_size

def drawit(self):
if self.xmode != 'W':
#self.ids..disabled = True
self.ids.disp.font_size=20
self.ids.disp.text=' Draw only\n available in\n Windows mode\n '\
+str(self.ids.disp.font_size)+' '+str(self.ids.draw.disabled)
self.ids.disp.font_size=20
return
self.ids.drat.color='black'
self.ids.drat.text = "Only 1 Draw \nPer Session" #draw
self.ids.drat.disabled = True

self.Listsizes(0)

self.turtle_it() #call turtle routine

return #drawit

def turtle_it(self):
import turtle
from turtle import Screen

t = turtle.Turtle()
s = Screen()
#s.setup(xx800+150, xx600+150, 1360, 0) # pushes turtle screen to monitor 1
s.setup(xx800+150,xx600+150,0,0) #make turtle screen 150 pixels larger monitor 0
turtle.bgcolor('black')
t.speed(10)
t.turtlesize(.5,.5,.5)
t.penup()
t.pencolor('white') #axis lines are white
t.goto(-xx400,0)
t.pendown()
xz=-xx400 #draw x axis (horizontal)
for i in range(8):
t.fd(100)
t.stamp()
zz=-0 #write horizontal py/turtle tickmarks
for i in range(9):
t.penup()
t.goto(xz,-15)
t.pendown()
t.write(' '+str(zz)+' : '+str(xz))
t.penup()
t.goto(xz,-15)
t.pendown()
xz=xz+100
zz=zz+100

#setup for y axis (vertical)
t.penup()
t.goto(0,-xx300)
t.pendown()
t.lt(90)
xz=-xx300
zz=0
for i in range(6): #draw y axis (vertical)
t.write(' '+str(zz)+' : '+str(xz))
xz=xz+100
zz=zz+100
t.fd(100)
t.stamp()
t.write(' '+str(zz)+' : '+str(xz)) #last tickmark
self.xboxes=2
xxx=self.get_widget_names()
xxx.remove('Main')
xcolor=self.get_widget_colors('name')
xcolor.remove('white')
xpos = [self.ids[idx].pos for idx in xxx] #
xsiz = [self.ids[idx].size for idx in xxx] #
xnam = [self.ids[idx].name for idx in xxx] #

i=0 #index number from pos, size
for x in xxx: #run through widget names
x1 = xpos[i] #break out x and y values for pos
xx1=x1[0]
xy1=x1[1]
x2 = xsiz[i] #break out x and y values for size
xx2=x2[0]
xy2=x2[1]
self.drawbox(xx1,xy1,xx2,xy2,xcolor[i],xnam[i]) #draw the box
i+=1 #increment for next widget
#if i==13:
# turtle.done()
# return

t.penup() #put warning on top of drawing
t.goto(-xx300,xx300+25)
t.write('Close this screen to continue - Output saved in <downloads> turtle.png',font=('Arial', 14, 'normal'))

# Get the turtle canvas
canvas = turtle.getcanvas()

# Get the bounding box of the turtle canvas
x0 = canvas.winfo_rootx()
y0 = canvas.winfo_rooty()
x1 = x0 + canvas.winfo_width()
y1 = y0 + canvas.winfo_height()-50
#quit()
# Grab the image of the turtle canvas
image = ImageGrab.grab(bbox=(x0, y0, x1, y1))

# Save the image as a PNG file
image.save(self.xtfile,"PNG") #<downloads> turtle.png

turtle.done()
self.set_focus()

return #turtle_it

def drawbox(self, x0,y0,wid,hig,color,widg):
if self.xboxes==2:
#convert python to turtle
if x0==0:
xx0=-xx400
else:
if x0>xx400:
xx0=x0-xx400
else:
xx0=x0-xx400

if y0==0:
yy0=-xx300+hig
else:
if y0 > xx300:
yy0=y0-xx300+hig
else:
yy0=y0-xx300+hig

import turtle #draw a box
t=turtle.Turtle()
t.turtlesize(.5,.5,.5)
t.speed(30)
t.penup()
t.goto(xx0,yy0) #put text above box
t.pencolor(color)
xwrite=' '+widg+': py='+str(int(x0))+' '+str(int(y0))+' '+str(int(wid))+' '+str(int(hig))+\
' tu='+str(int(xx0))+' '+str(int(yy0))
#xfont=ImageFont.truetype("arial.ttf",10)
#widx, higx =xfont.getsize(xwrite)
widx=kivy.core.text.Label().get_extents(xwrite)[0] #number of pixels in xwrite
if widx>wid: #text > widget width
if xx0==-xx400 and yy0==-xx300:
if wid==0 and hig==0: #no size
t.goto(xx0+125,yy0)
else:
t.goto(xx0,yy0) #put text above box
else:
t.goto(xx0,yy0-52) #put text inside box
xwrite=' '+widg+': py='+str(int(x0))+' '+str(int(y0))+\
'\n '+str(int(wid))+' '+str(int(hig))+\
'\n tu='+str(int(xx0))+' '+str(int(yy0))
t.write(xwrite,font=('Arial', 10, 'normal'))
t.goto(xx0,yy0)
t.pencolor(color)
t.pendown()
t.fd(wid)
t.rt(90)
t.fd(hig)
t.rt(90)
t.fd(wid)
t.rt(90)
t.fd(hig)

class TutorialApp(App):

def build(self):
return ScatterTextWidget()

def _reposition(self):
#self.root.ids.scat.center = self.root.ids.floa.center
pass

#stuff for popup
class MessagePop(Popup):
print('messagepop')
pass

class RootBoxLayout(BoxLayout):
text = StringProperty('Your message goes here - rbox')
print('rootboxlayout')


class PopApp(App):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.pop = None # used to hold the popup
print('pop popapp')

#def build(self):
# print('build')
# return Builder.load_string(kv)

def on_start(self):
self.pop = MessagePop() # instance the popup after the kv has been processed
print('pop onstart')

if __name__ == "__main__":
TutorialApp().run()

elli...@cox.net

unread,
Sep 6, 2023, 12:26:28 AM9/6/23
to kivy-...@googlegroups.com

You have 2 app classes; you can only have one.  You are only instancing TutorialApp.

 

In the kv file you have a root widget, RootBoxLayout.  But in TutorialApp you are instancing ScatterTextWidget as the root widget.

 

In MessagePop there is an error. You have an on_release statement in the multiline on_release.

 

The color attributes in Color, need to be between 0 and 1, you have 255.  You can divide the color attributes you have by 255, to get them between 0 and 1 if you currently have them represented from 0-255. Or you can use kivy.utils.rgba.  https://kivy.org/doc/stable/api-kivy.utils.html#kivy.utils.rgba

 

There is a dict in kivy.utils called colormap, that maps the names of colors to their value.  It is not documented – but it is in there.  It is also used by the ColorProperty.  Take a look at the source code if you are interested.

 

 

 

<MessagePop>
    title:
'Pop goes the weasel'
   
size_hint: .7, .7
   
on_open: input.focus = True

   
BoxLayout:
       
orientation: 'vertical'
       
AnchorLayout:
           
TextInput:
               
id: input
               
size_hint: None, None
               
size: 200, 40
               
hint_text: 'Please respond'
               
on_text_validate: app.root.text = self.text
       
Button:
           
size_hint_y: None
           
height: dp(48)
           
text: 'Press when finished'
           
on_release:


               
app.root.text = input.text
               
root.dismiss()
                input.
text = ''
                on_release: app.open()

elli...@cox.net

unread,
Sep 6, 2023, 12:39:27 AM9/6/23
to kivy-...@googlegroups.com

If you want to open the popup in xtest, you could use Factory…

#:import Factory kivy.factory.Factory

 


           
Button#xtest
               
id: test
                name:
'test'

#                on_press: root.xtest()
               
on_press: Factory.MessagePop().open()
               
text: 'xtest'
               
background_color: 128,128,128,1     #grey
               
color: 'black'

 

Or to use the xtest method, instance the MesagePop, and then use the open method.

def xtest(self):
   
# popup
   
print('xtext pop')
   
# RootBoxLayout()
   
MessagePop().open()
   
return

elli...@cox.net

unread,
Sep 6, 2023, 12:55:13 AM9/6/23
to kivy-...@googlegroups.com

Another issue:

with self.canvas:
   
print(f'{xxc[i-1]=} {type(xxc[i-1])=}')
    Color(xxc[i -
1])
    Line(
rectangle=(xx1, xy1, xx2, xy2))
   
print(i + 1, xxx[i], xxc[i + 1])
    Color(
1, 0, 0)
    Rectangle(
pos=(10, 100), size=(50, 50))
    Color(
0, 1, 0)
    Rectangle(
pos=(100, 100), size=(50, 50))

xcc is a string, not a tuple.

xxc[i-1]='(0, .5, .5)' type(xxc[i-1])=<class 'str'>

elli...@cox.net

unread,
Sep 6, 2023, 1:00:35 AM9/6/23
to kivy-...@googlegroups.com

I see this is from colorcodes.txt file.  I’d recommend storing the data as json, use the json module to parse and convert back to python data types. Or better yet use the kivy colormap in utils.

Dan Moormann

unread,
Sep 7, 2023, 11:08:35 PM9/7/23
to Kivy users support
I changed xb to a list and converted xxn[i+1] to a tuple and still no color.  it does work if I explicitly type in (255,0,0) or (1,0,0) (see example code).  I also tried to put the tuple into the Color() before Rectangle and it defaulted to white.  It looks like Color() does not accept a variable.  If it does, what am I doing wrong?

xb = [1, 1, 1], [0, 1, 1], [1, 0, 0], [0, 1, 0], [0, 0, 1],\
[.5, 0, .5], [1, 1, 0], [0, 1, 0], [0, .5, .5], [0, 1, 0, 1],\
[.5, .5, .5], [.5, 0, 0], [0, 0, .5], [.5, .5, 0], [.75, .75, .75]


with self.canvas:
print(self.ln(),x,type(x))
x=tuple(x)
print(self.ln(),x,type(x))
Color(x)
Line(rectangle=(xx1,xy1,xx2,xy2))
Color(0, 255, 0)
Line(rectangle=(100,200,200,200))
Color(1,0,0)
Line(rectangle=(200,200,200,200))

Color(0, 1, 0)
Rectangle(pos=(100, 100), size=(50, 50))
Color(x)
Rectangle(pos=(200, 200), size=(50, 50))

elli...@cox.net

unread,
Sep 8, 2023, 12:10:26 AM9/8/23
to kivy-...@googlegroups.com

Here is how I would do it – I would use kv to use the canvas so the bind is taken care of by kv.  You may have an issue where you need to bind on size or pos change.  Or simply move this to kv.

 

from kivy.app import App
from kivy.lang import Builder

from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ColorProperty

kv =
"""
<ColorBox>:
    canvas:
        Color:
            rgb: root.color
        Rectangle:
            size: self.size
            pos: self.pos
           
GridLayout:
    cols: 5
    rows: 3

"""

xb = [1, 1, 1], [0, 1, 1], [1, 0, 0], [0, 1, 0], [0, 0, 1], \
    [
.5, 0, .5], [1, 1, 0], [0, 1, 0], [0, .5, .5], [0, 1, 0, 1], \
    [
.5, .5, .5], [.5, 0, 0], [0, 0, .5], [.5, .5, 0], [.75, .75, .75]


class ColorBox(BoxLayout):
    color = ColorProperty()


class ColorBoxes(App):
   
def build(self):
       
return Builder.load_string(kv)

   
def on_start(self):
       
for color in xb:
            box = ColorBox(
color=color)
           
self.root.add_widget(box)


ColorBoxes().run()

elli...@cox.net

unread,
Sep 8, 2023, 12:29:08 AM9/8/23
to kivy-...@googlegroups.com

Here is the same code but using the canvas in python instead of kv.

from kivy.app import App
from kivy.lang import Builder

from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ColorProperty
from kivy.graphics import Color, Rectangle

kv =
"""
# <ColorBox>:
#     canvas:
#         Color:
#             rgb: root.color
#         Rectangle:
#             size: self.size
#             pos: self.pos


           
GridLayout:
    cols: 5
    rows: 3

"""

xb = [1, 1, 1], [0, 1, 1], [1, 0, 0], [0, 1, 0], [0, 0, 1], \
    [
.5, 0, .5], [1, 1, 0], [0, 1, 0], [0, .5, .5], [0, 1, 0, 1], \
    [
.5, .5, .5], [.5, 0, 0], [0, 0, .5], [.5, .5, 0], [.75, .75, .75]


class ColorBox(BoxLayout):
    color = ColorProperty()

   
def __init__(self, **kwargs):
       
super().__init__(**kwargs)
       
with self.canvas:
           
self.c = Color(self.color)
           
self.rect = Rectangle(pos=self.pos, size=self.size)
       
self.bind(size=self.set_colors)
       
self.bind(pos=self.set_colors)

   
def set_colors(self, *args):
       
self.rect.pos = self.pos
       
self.rect.size = self.size
       
self.c.rgba = self.color


class ColorBoxes(App):
   
def build(self):
       
return Builder.load_string(kv)

   
def on_start(self):
       
for color in xb:
            box = ColorBox(
color=color)
           
self.root.add_widget(box)


ColorBoxes().run()

 

 

From: elli...@cox.net <elli...@cox.net>
Sent: Thursday, September 7, 2023 9:10 PM
To: 'kivy-...@googlegroups.com' <kivy-...@googlegroups.com>
Subject: RE: [kivy-users] Some Questions (unrelated)

 

Here is how I would do it – I would use kv to use the canvas so the bind is taken care of by kv.  You may have an issue where you need to bind on size or pos change.  Or simply move this to kv.

 

from kivy.app import App
from kivy.lang import Builder

from kivy.uix.boxlayout import BoxLayout

from kivy.properties import ColorProperty

kv =
"""
<ColorBox>:
    canvas:
        Color:
            rgb: root.color
        Rectangle:
            size: self.size
            pos: self.pos
           
GridLayout:
    cols: 5
    rows: 3

"""

xb = [1, 1, 1], [0, 1, 1], [1, 0, 0], [0, 1, 0], [0, 0, 1], \
    [
.5, 0, .5], [1, 1, 0], [0, 1, 0], [0, .5, .5], [0, 1, 0, 1], \
    [
.5, .5, .5], [.5, 0, 0], [0, 0, .5], [.5, .5, 0], [.75, .75, .75]


class ColorBox(BoxLayout):
    color = ColorProperty()


class

ColorBoxes(App):
   
def build(self):
       
return Builder.load_string(kv)

   
def on_start(self):
       
for color in xb:
            box = ColorBox(
color=color)
           
self.root.add_widget(box)


ColorBoxes().run()

Dan Moormann

unread,
Sep 9, 2023, 8:49:36 PM9/9/23
to kivy-...@googlegroups.com
Eliot - thanks for the ColorBox app.  It works fine for ColorBox(), but I want it to work for LineRectangle().  Any chance you can help?


elli...@cox.net

unread,
Sep 10, 2023, 1:11:54 AM9/10/23
to kivy-...@googlegroups.com

Like this?

 

 

from kivy.app import App
from kivy.lang import Builder

from kivy.uix.floatlayout import FloatLayout
from kivy.properties import ColorProperty
from kivy.uix.label import Label


kv =
"""


<ColorBox>:
    canvas:
        Color:
            rgb: root.color

        Line:
            rectangle: (*self.pos, *self.size)


FloatLayout:
"""

xb = [1, 1, 1], [0, 1, 1], [1, 0, 0], [0, 1, 0], [0, 0, 1], \
    [
.5, 0, .5], [1, 1, 0], [0, 1, 0], [0, .5, .5], [0, 1, 0, 1], \
    [
.5, .5, .5], [.5, 0, 0], [0, 0, .5], [.5, .5, 0], [.75, .75, .75]


class ColorBox(FloatLayout):
    color = ColorProperty()


class ColorBoxes(App):
   
def build(self):
       
return Builder.load_string(kv)

   
def on_start(self

):
        x =
0;
        y =
0;
        i =
0
       
for color in xb:
            box = ColorBox(
color=color, pos=(x, y), size_hint=(None, None), size=(125, 50))
           
self.root.add_widget(box)
           
self.root.add_widget(Label(text=str(xb[i]), pos=(x, y), size_hint=(None, None), size=(125, 50)))
            x = x +
50;
            y = y +
50
           
print(xb[i], x, y)
            i = i +
1


ColorBoxes().run()

image001.png

elli...@cox.net

unread,
Sep 10, 2023, 1:26:57 AM9/10/23
to kivy-...@googlegroups.com

Or you could use the canvas from the Label…

 

from kivy.app import App
from kivy.lang import Builder

from kivy.properties import ColorProperty
from kivy.uix.label import Label


kv =

"""
<ColorBox>:
    canvas.before:
        Color:
            rgb: root.box_color


        Line:
            rectangle: (*self.pos, *self.size)

FloatLayout:
"""

xb = [1, 1, 1], [0, 1, 1], [1, 0, 0], [0, 1, 0], [0, 0, 1], \
    [
.5, 0, .5], [1, 1, 0], [0, 1, 0], [0, .5, .5], [0, 1, 0, 1], \
    [
.5, .5, .5], [.5, 0, 0], [0, 0, .5], [.5, .5, 0], [.75, .75, .75]


class ColorBox(Label):
    box_color = ColorProperty()


class ColorBoxes(App):
   
def build(self):
       
return Builder.load_string(kv)

   
def on_start(self

):
        x =
0;
        y =
0;
        i =
0
       
for color in xb:
            box = ColorBox(
text=str(xb[i]), box_color=color, pos=(x, y), size_hint=(None, None), size=(125, 50))
           
self.root.add_widget(box)
            x = x +
50;
            y = y +
50
           
print(xb[i], x, y)
            i = i +
1


ColorBoxes().run()

 

image001.png
Reply all
Reply to author
Forward
0 new messages