How to make Scrollable label in kivy using .kv file - python-2.7

I am trying to make a scrollable label which has some text so here is my code
Here is my main.py
from kivy.app import App
import socket, sys, threading, os, time
class ExampleApp(App):
def build(self):
pass
if __name__ == "__main__":
ExampleApp().run()
Here is my example.kv file
ScrollView:
Label:
text:"hello world"*1000
I am facing issues with this....some one please post answer for this

Related

Running a Kivy screen inside a Pyqt app ,

I am trying to make a simple maths quiz game that shows Numbers and when I press space it shows the result , the Application launch with a QTdyalog GUI first and after I finish configuring the gameplay a Kivy screen should show up and the game starts until the user press escape it gets back to the config GUI
here is the Programm skeleton
import kivy
from kivy.app import App
from kivy.uix.label import Label
import sys
from PyQt4 import QtCore, QtGui, uic
qtCreatorFile = "ConfigForm.ui" # Enter file here.
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
class Playy(App): #the Kivy app
def build(self):
l = Label(text='Hello world')
return l
class Conf(QtGui.QDialog, Ui_MainWindow): #the COnfiguration Gui
def __init__(self):
QtGui.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
self.buttonBox.accepted.connect(self.oky)
self.buttonBox.rejected.connect(self.notok)
def oky(self):
print "OK OK OK OK" #creating a Kivy app instance and running it
ins = Playy()
ins.run()
def notok(self):
print "cancel cancel"
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = Conf()
window.show()
sys.exit(app.exec_())
I am only having Issue with making functions inside the Kivy app class and passing arguments Into them and also handling Objects properties such as label text this is my first Kivy experiment tell me what do you think

How to set image from external server to kv file

My code works perfectly for image saved in same directory.
#!/usr/bin/kivy
import kivy
kivy.require('1.7.2')
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
Builder.load_string('''
<MenuScreen>:
GridLayout:
cols: 1
Button:
on_press: root.val1()
Image:
source: "myimage.PNG"
size: self.parent.width, self.parent.height
allow_stretch: True
keep_ratio: False
''')
class MenuScreen(Screen):
def val1(self):
print "i am executed"
sm = ScreenManager()
menu = MenuScreen(name='menu')
sm.add_widget(menu)
class MainApp(App):
def build(self):
return sm
if __name__ == '__main__':
MainApp().run()
What changes should be made in this code if i want to take image from external source ie
Image:
source: "http://example.com/myimage.jpg"
Obviously this does not work. Please help.
Try using AsyncImage instead. From the documentation:
To load an image asynchronously (for example from an external
webserver), use the AsyncImage subclass:
aimg = AsyncImage(source='http://mywebsite.com/logo.png')
This can be useful as it prevents your application from waiting until
the image is loaded. If you want to display large images or retrieve
them from URL’s, using AsyncImage will allow these resources to be
retrieved on a background thread without blocking your application.

calling a python script on button click using python and tkinter

I have a python script which has the functionality of sending an email to a user. I executed this script and it is working fine. In another python script I have only a button, so when I click on this button I want the other python script which sends a email to be executed.I have written the following code:
#!/usr/bin/python
import sys
import os
import Tkinter
import tkMessageBox
top=Tkinter.Tk()
def helloCallBack():
os.system('SendEmail.py')
B=Tkinter.Button(top,text="hello",command= helloCallBack)
B.pack()
top.mainloop()
I get the following error when I click on the button:
sh: 1:SendEmail.py:not found.
Could you let me know what is the reason for this error and how it can be resolved.Thanks.
I was able to figure out a way to call another python script on button click:
instead of using os.system('SendEmail.py') we need to use os.system('python SendEmail.py')
import sys
import os
from tkinter import *
window=Tk()
window.title("Running Python Script")
window.geometry('550x200')
def run():
os.system('opencv_video.py')
btn = Button(window, text="Click Me", bg="black", fg="white",command=run)
btn.grid(column=0, row=0)
window.mainloop()
If your SendEmail.py is in the same location, use os.system('SendEmail.py'). If it's in a different location, use os.system('python SendEmail.py').
#!/usr/bin/python
import sys
import sys
import os
import Tkinter
import tkMessageBox
top=Tkinter.Tk()
def helloCallBack():
os.system('python SendEmail.py')
B=Tkinter.Button(top,text="hello",command= helloCallBack)
B.pack()
top.mainloop()
use the keyword "python" to run the command
As an amateur, I am not really qualified to give advice. This is how I did it.
I want to do this kind of thing too. I have about 16 little python programs which make html, sets of checkboxes, sets of radiobuttons, text input fields, html tables etc.
In another thread here a comment was quite deprecative of using os.system calls. Not sure why, but I thought I would try another approach.
I've just started learning tkinter, so I am making each of my 'makehtml' functions run in a window.
Now I want a master window with buttons. Click a button and another window opens, say the checkboxes window, or any of the other windows for making html.
I made a module: guiHTML.py All my 'makehtml' functions are in there.
Import guiHTML in the master window.
import os, sys
# to import the files we need the paths
path = '/home/pedro/myPython/myModules/'
# append the paths
sys.path.append(path)
import tkinter as tk
from functools import partial
import guiHTML
Then, in the master window make a function like this for each button:
def openCheckboxes():
#call the checkboxes function defined in the guiHTML module
guiHTML.checkboxes()
Then, in the checkboxes button just put this:
btn3 = tk.Button(frame1, text='insert checkboxes', command=openCheckboxes)
btn3.grid(columnspan=2, column=0, row=2, sticky='w', pady=10)
Click btn3 and the checkboxes window opens.
This works for me, but I don't know if it is a good way to do this. I only began with tkinter a month ago.
If there is a better way to do this, I'd be glad to hear it from you experts!
#!/usr/bin/python
import sys
import os
import tkinter as tk
root = tk.Tk()
def helloCallBack():
os.system('call.py')
#Keep_both_files_in_the_same_Folder
b1=tk.Button(root, text="Calendar",bg="white",command=helloCallBack)
b1.pack()
root.mainloop()

How does one add a sub widget from a python file to a super widget defined in a kivy file?

I have a BoxLayout defined in a kivy file, and I am trying to add a label to this BoxLayout from a python file. Below are my python and kivy scripts. When I try to run the program as is, it fails, and I get an error message stating:
"properties.pyx", line 654, in kivy.properties.ObservableDict.__getattr__ (kivy\properties.c:9590)
KeyError: 'box'
How can I add a label (defined in dummy.py) to the BoxLayout (defined in dummy.kv) from dummy.py?
dummy.py shown below:
from kivy.app import App
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.lang import Builder
Builder.load_file('dummy.kv')
class test(BoxLayout):
box = ObjectProperty()
def __init__(self):
label = Label(text='Hello')
self.ids.box.add_widget(label)
class Test(App):
def build(self):
return test()
if __name__ == '__main__':
Test().run()
dummy.kv shown below:
#:kivy 1.8
<test>:
box: box
BoxLayout:
orientation: 'vertical'
id: box
You didn't initialize your test class properly:
from kivy.app import App
from kivy.properties import ObjectProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.lang import Builder
Builder.load_file('dummy.kv')
class test(BoxLayout):
box = ObjectProperty()
def __init__(self):
label = Label(text='Hello')
super(test,self).__init__() #-------ADD THIS LINE
print(self.ids)
self.ids.box.add_widget(label)
class Test(App):
def build(self):
return test()
if __name__ == '__main__':
Test().run()
If those are really your filenames, the kv file is never loaded, so the BoxLayout with id 'box' does not exist. Rename the kv file to test.kv or use kivy.lang.Builder.load_file('dummy.kv').

Kivy : Relative size of text displaying in a popup

Is there any way to make the message/title in the label fits into the popup ?
'message' or 'title' can be a long text that goes beyond the borders of the popup .
def popup_display(self, title, message):
btnclose = Button(text='Close me', size_hint_y=None, height=50)
content = BoxLayout(orientation='vertical')
content.add_widget(Label(text=message))
content.add_widget(btnclose)
popup = Popup(content=content, title=title,
size_hint=(None, None),
size=(300, 300),
auto_dismiss=False)
btnclose.bind(on_release=popup.dismiss)
popup.open()
Label has a text_size property, which allows to constraint its size to given bounding box. You can bind it to available size:
#!/usr/bin/python
# -*- coding: utf-8 -*-
from kivy.uix.popup import Popup
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.app import App
from kivy.lang import Builder
from functools import partial
class TestApp(App):
def build(self):
return Button(on_press=partial(self.popup_display, "title", "bar "*80))
def popup_display(self, title, message, widget):
btnclose = Button(text='Close me', size_hint_y=None, height=50)
l = Label(text=message)
l.bind(size=lambda s, w: s.setter('text_size')(s, w))
content = BoxLayout(orientation='vertical')
content.add_widget(l)
content.add_widget(btnclose)
popup = Popup(content=content, title=title, size_hint=(None, None), size=(300, 300), auto_dismiss=False)
btnclose.bind(on_release=popup.dismiss)
popup.open()
if __name__ == '__main__':
TestApp().run()
Text that won't fit will get removed. If you want to have it all you should use ScrollView inside of your popup.
As for title, it's not a Label but a string, so the best you can do is to add newlines:
return Button(on_press=partial(self.popup_display, "multiline\ntitle", "bar "*80))
Here'a a simpler version without BoxLayout and Button:
#!/usr/bin/python
# -*- coding: utf-8 -*-
from functools import partial
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.popup import Popup
class TestApp(App):
def build(self):
return Button(on_press=partial(self.popup_display, "title", "bar "*80))
def popup_display(self, title, message, widget):
l = Label(text=message)
l.bind(size=lambda s, w: s.setter('text_size')(s, w))
popup = Popup(content=l, title=title, size_hint=(None, None), size=(300, 200), auto_dismiss=True)
popup.open()
if __name__ == '__main__':
TestApp().run()
Simply click outside the popup to close it.