Kivy Clipboard.copy label text - kivy

I want to copy the content of a Label: self.text when I double tap the label, but the following is not working:
main.py
#!/usr/bin/kivy
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
class DoubletapClipboardInterface(BoxLayout):
pass
class DoubletapClipboardApp(App):
#copy_clipboard = ObjectProperty()
def build(self):
self.title = 'DoubletapClipboard'
#self.copy_clipboard = DoubletapClipboardInterface()
return(DoubletapClipboardInterface()) # self.copy_clipboard
if __name__ == '__main__':
DoubletapClipboardApp().run()
doubletapclipboard.kv
#:kivy 1.9.0
#:import Clipboard kivy.core.clipboard.Clipboard
<DoubletapClipboardInterface>:
orientation: 'vertical'
TextInput:
hint_text: 'Try to paste here to see if it works'
Label:
text: 'Can I be copied?'
on_double_tap: Clipboard.copy(self.text) # <-- How do I do this the correct way?
Error
kivy.lang.builder.BuilderException: Parser: File "/home/stef-ubuntu/bitbucket/kanjiorigin_data/test/doubletap_clipboard/doubletapclipboard.kv", line 11:
...
9: Label:
10: text: 'Can I be copied?'
>> 11: on_double_tap: Clipboard.copy(self.text) # <-- How do I do this the correct way?
...
AttributeError: double_tap
File "/usr/lib/python3/dist-packages/kivy/lang/builder.py", line 628, in _apply_rule
raise AttributeError(key)

On suggestion of #MatthiasSchreiber I copied the code from TextInput()
main.py
#!/usr/bin/kivy
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.properties import ObjectProperty
from kivy.utils import platform
Clipboard = None
CutBuffer = None
class TouchLabel(Label):
def __init__(self, **kwargs):
self._touch_count = 0
super(TouchLabel, self).__init__(**kwargs)
self.register_event_type('on_double_tap')
if platform == 'linux':
self._ensure_clipboard()
def _ensure_clipboard(self):
global Clipboard, CutBuffer
if not Clipboard:
from kivy.core.clipboard import Clipboard, CutBuffer
def on_touch_down(self, touch):
print("hello")
if self.disabled:
return
touch_pos = touch.pos
if not self.collide_point(*touch_pos):
return False
if super(TouchLabel, self).on_touch_down(touch):
return True
touch.grab(self)
self._touch_count += 1
if touch.is_double_tap:
self.dispatch('on_double_tap')
def on_double_tap(self, *args):
Clipboard.copy(self.text) # <-- How do I do this the correct way?
print("Copied :D")
class DoubletapClipboardInterface(BoxLayout):
pass
class DoubletapClipboardApp(App):
def build(self):
self.title = 'DoubletapClipboard'
return(DoubletapClipboardInterface())
if __name__ == '__main__':
DoubletapClipboardApp().run()
doubletabclipboard.kv
#:kivy 1.9.0
# #:import Clipboard kivy.core.clipboard.Clipboard
<TouchLabel>
<DoubletapClipboardInterface>:
orientation: 'vertical'
TextInput:
hint_text: 'Try to paste here to see if it works'
TouchLabel:
text: 'Can I be copied?'
#on_double_tap: Clipboard.copy(self.text) # <-- not working

There is no "on_double_tap" for a Label, you need to create that method yourself.
There is one for TexInput though and you could look how it's done there in the code.
Also, you need to import the Clipboard into your kv file.

Related

Python3 KivyMD - MDDialog get ItemConfirm text value

I want to pass to my project_selected function the selected item in my MDDialog after pressing the OK button. But I can't figure out how can I get the value and do this.
I could print the value inside my set_icon funtion in ItemConfirm class, but I don't know whats the better way to pass that value to HomeWindow(Screen) class or if it is possible to call it directly from inside of it once that the ItemConfirm is already called from inside a function that is part of HomeWindow class.
main.py
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from kivymd.app import MDApp
from kivymd.uix.button import MDFlatButton
from kivymd.uix.dialog import MDDialog
from kivymd.uix.list import OneLineAvatarIconListItem
class ItemConfirm(OneLineAvatarIconListItem):
divider = None
def set_icon(self, instance_check):
print(self.text)
instance_check.active = True
check_list = instance_check.get_widgets(instance_check.group)
for check in check_list:
if check != instance_check:
check.active = False
class HomeWindow(Screen):
dialog = None
def show_confirmation_dialog(self):
projects = [{id: 0, name: "example1"},{id: 1, name: "example2"}]
if not self.dialog:
self.dialog = MDDialog(
title="Projects",
type="confirmation",
auto_dismiss=False,
items=[ItemConfirm(text=f"{project['name']}") for project in projects],
buttons=[
MDFlatButton(text="CANCEL", theme_text_color="Custom"),
MDFlatButton(
text="OK",
theme_text_color="Custom",
on_release=self.project_selected,
),
],
)
self.dialog.open()
def project_selected(self, *args, **kwargs):
self.ids.project_selection.text = self.dialog.text
class WindowManager(ScreenManager):
pass
class RlogTimer(MDApp):
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_palette = "BlueGray"
self.root = Builder.load_file("templates/main.kv")
return self.root
if __name__ == "__main__":
RlogTimer().run()
templates/main.kv
#: import NoTransition kivy.uix.screenmanager.NoTransition
WindowManager:
HomeWindow:
<ScreenManager>:
transition: NoTransition()
<ItemConfirm>:
on_release: root.set_icon(check)
CheckboxLeftWidget:
id: check
group: "check"
<HomeWindow>:
name: "home"
MDBoxLayout:
orientation: "vertical"
MDToolbar:
id: title
title: "Redmine logTimer"
right_action_items: [["clock", lambda x: app.callback_2()]]
MDFlatButton:
id: project_selection
text: "Select Project"
pos_hint: {'center_x': .5, 'center_y': .5}
on_release: root.show_confirmation_dialog()
You can achieve that as follows,
First create a prop. say, selected_project in HomeWindow as,
class HomeWindow(Screen):
dialog = None
selected_project = StringProperty()
Now set its value in method set_icon as,
def set_icon(self, instance_check):
...
app = MDApp.get_running_app() # Access the running app instance.
home_screen = app.root.get_screen("home") # Access required screen.
home_screen.selected_project = self.text # Now set value.
...
Now it's time to set this value in method project_selected,
def project_selected(self, *args, **kwargs):
self.ids.project_selection.text = self.selected_project

Add KivyMD Expansion Panel inside RecyleView kivy

anyone can help me about how to add KivyMD Expansion Panel in RecycleView kivy? Because I have issue with slow perfomance while add MDExpansionPanel with "add_widget" on kivy (run in old android).
Then i have idea to put Expansion into RecycleView, but the expansionpanel can't open with true index.
fyi, i have make custom expansion.py file
expansion.py
from kivy.animation import Animation
from kivy.lang import Builder
from kivy.metrics import dp
from kivy.properties import StringProperty, ObjectProperty, NumericProperty
from kivy.uix.boxlayout import BoxLayout
from kivymd.uix.button import MDIconButton
from kivymd.uix.list import (IRightBody, ILeftBody, TwoLineAvatarIconListItem)
from kivymd.uix.selectioncontrol import MDCheckbox
Builder.load_string(
"""
#:import images_path kivymd.images_path
#:import md_icons kivymd.icon_definitions.md_icons
<CustomExpansionPanel>
text: root.title
secondary_text: root.desc
_no_ripple_effect: True
IconRightSampleWidget:
id: check
disabled_color: [.2, .3, .6, .9]
disabled: True
ChevronRight:
id: chevron
icon: 'chevron-right'
disabled: True
canvas.before:
PushMatrix
Rotate:
angle: self.angle
axis: (0, 0, 1)
origin: self.center
canvas.after:
PopMatrix
<CustomMDExpansionPanel>
size_hint_y: None
height: dp(68)
padding:dp(0)
BoxLayout:
id: box_item
size_hint_y: None
height: root.height
orientation: 'vertical'
CustomExpansionPanel:
id: item_anim
title: root.title
desc: root.desc
on_press: root.check_open_box(self)
"""
)
class IconRightSampleWidget(ILeftBody, MDCheckbox):
pass
class ChevronRight(IRightBody, MDIconButton):
angle = NumericProperty(0)
class CustomExpansionPanel(TwoLineAvatarIconListItem):
title = StringProperty()
desc = StringProperty()
class CustomMDExpansionPanel(BoxLayout):
content = ObjectProperty()
title = StringProperty()
desc = StringProperty()
def __init__(self, **kwargs):
super(CustomMDExpansionPanel, self).__init__(**kwargs)
self.register_event_type("on_open")
self.register_event_type("on_close")
def on_open(self, *args):
pass
def on_close(self, *args):
pass
def check_open_box(self, instance,CloseAll=False):
press_current_item = False
for box in self.parent.children:
if isinstance(box, CustomMDExpansionPanel):
if len(box.ids.box_item.children) == 2:
if instance is box.ids.item_anim:
press_current_item = True
box.ids.box_item.remove_widget(box.ids.box_item.children[0])
chevron = box.ids.box_item.children[0].ids.chevron
self.anim_chevron_up(chevron)
self.anim_resize_close(box)
self.dispatch("on_close")
break
if not press_current_item and not CloseAll:
self.anim_chevron_down()
def anim_chevron_down(self):
chevron = self.ids.item_anim.ids.chevron
angle = -90
Animation(angle=angle, d=0.2).start(chevron)
self.anim_resize_open_item()
self.dispatch("on_open")
def anim_chevron_up(self, instance):
angle = 0
Animation(angle=angle, d=0.2).start(instance)
def anim_resize_close(self, box):
Animation(height=dp(68), d=0.1, t="in_cubic").start(box)
def anim_resize_open_item(self, *args):
self.content.name_item = self.title
anim = Animation(
height=self.content.height + dp(70), d=0.2, t="in_cubic"
)
anim.bind(on_complete=self.add_content)
anim.start(self)
def add_content(self, *args):
if self.content and len(self.ids.box_item.children) == 1:
self.ids.box_item.add_widget(self.content)
main.py
from kivymd.app import MDApp as App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.clock import Clock
from expansion import CustomMDExpansionPanel
from time import time
from kivy.uix.boxlayout import BoxLayout
Builder.load_string(
"""
<ListScreen>:
recycle_view: recycle_view
RecycleView:
id: recycle_view
size_hint: 1, 0.9
viewclass: "CustomMDExpansionPanel"
RecycleGridLayout:
id: items_box
cols:1
default_size_hint: 1, None
size_hint: 1, None
height:self.minimum_height
"""
)
class Container(BoxLayout):
def __init__(self,**kwargs):
super(Container,self).__init__()
class ListScreen(Screen):
recycle_view = ObjectProperty(None)
items_box = ObjectProperty(None)
def on_enter(self):
start = time()
container = []
for i in range(0,50):
container.append(Container(item='strrr'+str(i), index=i))
self.recycle_view.data.append(
{
'content':container[i],
'title':'title'+str(i),
'desc':'desc'+str(i)
}
)
def on_leave(self):
self.recycle_view.data = []
class ListApp(App):
sm = ScreenManager()
screens = {}
def build(self):
self.__create_screens()
ListApp.sm.add_widget(ListApp.screens['list1'])
return ListApp.sm
def __create_screens(self):
ListApp.screens['list1'] = ListScreen(name='list1')
if __name__ == '__main__':
ListApp().run()
Thank You
From my understanding , if you are trying to add your expansion panel inside the RecycleView.
Here is an example code of how to do that
from kivy.lang import Builder
from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.expansionpanel import MDExpansionPanel, MDExpansionPanelThreeLine
from kivymd import images_path
KV = """
<Content>
adaptive_height: True
TwoLineIconListItem:
text: "(050)-123-45-67"
secondary_text: "Mobile"
RecycleView:
MDGridLayout:
id: box
cols: 1
adaptive_height: True
"""
class Content(MDBoxLayout):
"""Custom content."""
class Test(MDApp):
def build(self):
return Builder.load_string(KV)
def on_start(self):
for i in range(10):
self.root.ids.box.add_widget(
MDExpansionPanel(
icon=f"{images_path}kivymd.png",
content=Content(),
panel_cls=MDExpansionPanelThreeLine(
text="Text",
secondary_text="Secondary text",
tertiary_text="Tertiary text",
),
)
)
Test().run()
Please Refer Documentations Before Asking Questions :) As It Solves 90 % Of Your Problems <3
Also Edit Your Question And Specify Your Problem Clearly :)

Kivy, Label text and multithreading

I've put a loop into thread and I want my label to show the given thext. The issue is that it doesn't show most of texts I see in the terminal. It shows only:
>Text 0
>nothing
>Text 4
>Text 5
>nothing
>text 14
>text 15
>etc.
I've no idea why is that. I tried to use Clock.schedule_once to call prompt function but the resoult is the same.
CODE
from kivy.uix.boxlayout import BoxLayout
import threading
from kivy.app import App
from kivy.lang.builder import Builder
from time import sleep
kv = '''
<MainWindow>
Button:
text: 'Go!'
on_press: root.go()
Label:
id: label
MainWindow:
'''
class MainWindow(BoxLayout):
def __init__(self):
super(MainWindow, self).__init__()
def go(self):
thread = Prompter()
thread.start()
def prompt(self, text):
self.ids.label.text = text
print(self.ids.label.text)
class MyThread(threading.Thread):
def __init__(self):
super(MyThread, self).__init__()
def run(self):
for i in range(100):
text = 'TEXT {}'.format(i)
App.get_running_app().root.prompt(text)
sleep(0.5)
class Setup(App):
def build(self):
return Builder.load_string(kv)
if __name__ == '__main__':
Setup().run()
Try adding the #mainthread decorator to the prompt() method. Changes to the GUI must be done on the min thread.

Recycleview AttributeError: 'super' object has no attribute '__getattr__'

please check why the below program is giving an
AttributeError: 'super' object has no attribute '__getattr__'
.py:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ListProperty, NumericProperty, ObjectProperty
from kivy.uix.recycleview.views import RecycleDataViewBehavior
from kivy.uix.textinput import TextInput
# from kivy.effects.scroll.ScrollEffect import ScrollEffect
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.gridlayout import GridLayout
from kivy.uix.recycleboxlayout import RecycleBoxLayout
Builder.load_file('so_extractTIC.kv')
class RecycleItem(ScreenManager,RecycleDataViewBehavior, TextInput):
index = NumericProperty(0)
def refresh_view_attrs(self, rv, index, data):
self.index = index
return super(RecycleItem, self).refresh_view_attrs(rv, index, data)
class DataView(Screen):
DataList = ListProperty()
TextInputNum = NumericProperty(10)
def __init__(self,*args,**kwargs):
super(DataView, self).__init__(*args,**kwargs)
# for key, val in self.ids.items():
# print("key={0}, val={1}".format(key, val))
data12= []
for x in range(self.TextInputNum):
data12.append({'text': '', 'height': 50})
self.ids.rv.data = data12
def extract_data(self,rv):
print(self.parent.parent.parent)
self.DataList.clear()
for x in range(self.TextInputNum):
self.DataList.append(self.ids.rv.data[x]['text'])
print(self.DataList)
class RootWidget(ScreenManager):
pass
class MainApp(App):
def build(self):
# self.root = Builder.load_string(APP_KV)
return RootWidget()
if __name__ == '__main__':
MainApp().run()
.kv:
<DataView>:
BoxLayout:
orientation: 'vertical'
RecycleView:
size_hint_y: 0.9
viewclass: 'RecycleItem'
id: rv
key_size: 'size'
# effect_cls: ScrollEffect
cols: 1
RecycleBoxLayout:
id: rvbox
cols: rv.cols
orientation: 'vertical'
size_hint_y: None
height: self.minimum_height
default_size_hint: 1, None
Button:
text: 'Submit'
size_hint_y: 0.1
on_release: root.extract_data()
<RecycleItem>:
on_text: self.parent.parent.data[self.index]['text'] = self.text
<RootWidget>:
DataView:
name:"DataView_screen"
I have been searching for extract data from recycleview using textInput boxes. Please find the link for the query:
Retrieve Data from Kivy Recycleview
I am trying to inherit from ScreenManager, but it is giving the 'super' attribute error. Tried passing id as argument in the .kv and tried to find real parent but nothing works.
Also, please suggest how to use the above code for recycle GridLaout, with 2d rows and columns, i tried using for loops, but getting key related errors.
like:
for z in range(12):
for y in range(8):
self.table_data12.append(self.ids.idname.data[y][z]['text'])
thanks!
In your DataView class, the __init__() method references self.ids, but the ids are not yet available at that point. You can delay that referencing by using Clock.schedule_once() like this:
def __init__(self, *args, **kwargs):
super(DataView, self).__init__(*args, **kwargs)
# for key, val in self.ids.items():
# print("key={0}, val={1}".format(key, val))
Clock.schedule_once(self.setup_data)
def setup_data(self, dt):
data12 = []
for x in range(self.TextInputNum):
data12.append({'text': '', 'height': 50})
self.ids.rv.data = data12
I see no reason for making the RecycleItem a subclass of ScreenManager.
In your 'kv, the on_textline forRecycleItem` can be:
<RecycleItem>:
on_text: app.root.get_screen('DataView_screen').ids.rv.data[self.index]['text'] = self.text
And the submit Button can be:
Button:
text: 'Submit'
size_hint_y: 0.1
on_release: root.extract_data(rv)

sockets with kivy-recv line don't work

I made an app working with kivy that sends "name" to the server(with socket), the server inserts the "name" to the data base and sends back the "name" to the client(the app) and the client prints it.
I don't get any error but the app doesn't work. it is not open.
My client(the app):
# kivy.require("1.8.0")
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.widget import Widget
from kivy.uix.textinput import TextInput
from kivy.graphics import Line
from kivy.uix.button import Button
import socket
Builder.load_string("""
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
<MenuScreen>:
BoxLayout:
Button:
text: 'Sign Up'
on_press:
root.manager.transition.direction = 'left'
root.manager.current = 'settings'
Button:
text: 'Login'
on_press:
root.manager.transition.direction = 'left'
root.manager.current = 'settings'
<Searchi>:
BoxLayout:
Button:
text: 'send massage'
font_size: '20sp'
size_hint: (0.4,0.111)
pos_hint: {'y': 0,'x':0.6}
on_press:
root.manager.transition.direction = 'right'
root.manager.current = 'settings'
<SettingsScreen>:
BoxLayout:
FloatLayout:
LoginScreen
Button:
font_size: '20sp'
size_hint: (0.4,0.111)
pos_hint: {'y': 0,'x':0.6}
text: 'Back to menu'
on_press:
root.manager.transition.direction = 'right'
root.manager.current = 'menu'
""")
class LoginScreen(GridLayout):
def __init__(self, **kwargs):
super(LoginScreen, self).__init__(**kwargs)
self.cols = 2
self.my_socket = socket.socket()
#host = socket.gethostname()
#port = 8585
self.my_socket.connect(('127.0.0.1', 8081))
self.add_widget(Label(text='username'))
self.username = TextInput(multiline=False)
self.add_widget(self.username)
self.add_widget(Label(text='Password'))
self.password = TextInput(multiline=False, password=True)
self.add_widget(self.password)
self.submit_button = Button(text='sumbit',size_hint=(.5,
.25),font_size=20)
self.submit_button.bind(on_press=self.submit_username)
self.add_widget(self.submit_button)
def submit_username(self, *args):
# Make sure to validate the input before submitting to the server
self.my_socket.send(self.username.text.encode('utf-8'))
sm.current = 'searchi'
sm.transition.direction = 'left'
# Declare both screens
class MenuScreen(Screen):
pass
class SettingsScreen(Screen):
pass
class Searchi(Screen):
def __init__(self, **kwargs):
super(Searchi, self).__init__(**kwargs)
self.my_socket = socket.socket()
self.my_socket.connect(('127.0.0.1', 8081))
self.msg=self.my_socket.recv(1024)
self.name=self.msg.decode('utf-8')
self.add_widget(Label(text="hello"+self.name,size_hint=(1,
1.7),font_size=20))
# Create the screen manager
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(Searchi(name='searchi'))
sm.add_widget(SettingsScreen(name='settings'))
class TestApp(App):
def build(self):
return sm
if __name__ == '__main__':
TestApp().run()
My server:
import socket
import db # import my db
import time
server_socket = socket.socket()
server_socket.bind(('127.0.0.1',8081))
server_socket.listen(5)
(client_socket, client_address) = server_socket.accept()
while(True):
client_username = client_socket.recv(1024)
msg=client_username.decode('utf-8')
db.data_entry_login(msg,"dsfsds","dsfsfsdd","2") #insert to my db
msg=msg+"\r\n"
client_socket.send(msg.text.encode('utf-8'))
Can you help me?
The problem is as you mentioned the recv method.
That method put the whole main thread on hold, so the app wont run.
Whenever something is waiting or looping in the mainthread, the app will stop updating. And recv is waiting for a message.
So you need to put the recv in a thread.
You could do like this:
import threading
from kivy.clock import mainthread
class Searchi(Screen):
def __init__(self, **kwargs):
super(Searchi, self).__init__(**kwargs)
self.hello_label = Label(text="Connecting...",size_hint=(1,1.7),font_size=20)
threading.Thread(target=self.recv_msg).start()
# make sure you execute UI updates in mainthread
#mainthread
def recv_msg(self):
self.my_socket = socket.socket()
self.my_socket.connect(('127.0.0.1', 8081))
self.msg=self.my_socket.recv(1024)
self.name=self.msg.decode('utf-8')
self.hello_label.text = "hello"+self.name

Resources