In Kivy, I am trying to build an interface where the user can drag and drop a file into a widget (text input) and then my code would retrieve the file system path of that file (/path/to/users.file). That seems like a simpler approach than using the FileChooser widget, but how would I do it?
Thanks!
Use on_dropfile event handler. Here is an working example:
from kivy.app import App
from kivy.core.window import Window
class WindowFileDropExampleApp(App):
def build(self):
Window.bind(on_dropfile=self._on_file_drop)
return
def _on_file_drop(self, window, file_path):
print(file_path)
return
if __name__ == '__main__':
WindowFileDropExampleApp().run()
Related
My simple label in kivy is very blurry, I just wanted to know how to make it look sharper?
Here's the code:
import kivy
from kivy.app import App
from kivy.uix.label import Label
class MyApp(App):
def build(self):
return Label(text="Hello World")
if __name__ == '__main__':
MyApp().run()
Thanks.
The issue seems to be coming from you. On Windows 10, click the windows button/search, type ClearType. One of the options should say Adjust ClearType Text. Open that, and follow the wizard
I'm trying to use kivy for my project but I can't deal with it well..
I made a button, but I want that when I press him it will create another (new) button. Thanks a lot!
from kivy.app import App
from kivy.lang import builder
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.widget import Widget
from kivy.uix.button import Button
def createButton():
b = Button(pos=(0, 90), size_hint=(.2, .2), on_press=lambda a:ad())
return b
def ad():
"crate new button here!"
class NoobApp(App):
def build(self):
return createButton()
if __name__ == '__main__':
NoobApp().run()
In your ad() method add a line to create a Button and add it to the root of the app:
def ad():
print("crate new button here!")
App.get_running_app().root.add_widget(Button(text='hi'))
Note that this is adding a Button to a Button, (the root of the app is the original Button). Not a recommended approach. You should probably return some kind of Layout from your build() method instead.
Here is my code:
from kivy.app import App
from kivy.uix.button import Button
from kivy.core.image import Image as CoreImage
class WeaselApp(App):
def __init__(self, image):
self.image = image
def coreimage(self, load_image):
self.load_image = load_image
load_image = CoreImage("psychTREE.jpg")
#return load_image
def built(self):
return coreimage(self, load_image)
if __name__== "__main__":
WeaselApp(App).run()
When I run it, it says " WeaselApp has no attribute 'root' ". Why is this the case? Any hints or suggestions are greatly appreciated.
You don't return any widget from your build method, for two reasons:
As Joran Beasley said, you need a build method, not built.
CoreImage is not a widget that can be displayed on screen, it's a low level tool to load image data. You should use a kivy.uix.image.Image widget.
Edit: Following the comments, replace your code with this:
from kivy.app import App
from kivy.uix.image import Image
class YourApp(App):
def build(self):
return Image(source='psychTREE.jpg')
YourApp().run() # edited this in after
I would like my Kivy application to be able to spawn multiple apps (i.e. new windows) on a Windows machine that can communicate with each other.
ScreenManager and Popup options will not cut it because they live in the same window..I need to be able to drag new screens across multiple monitors and therefore need multiple windows.
Kivy docs explicitly state that "Kivy supports only one window
per application: please don't try to create more than one."
A google search produces this simple approach of simple spawning a new app from within another app, like so:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
class ChildApp(App):
def build(self):
return Label(text='Child')
class MainApp(App):
def build(self):
b = Button(text='Launch Child App')
b.bind(on_press=self.launchChild)
return b
def launchChild(self, button):
ChildApp().run()
if __name__ == '__main__':
MainApp().run()
However, when I do this, it launches the app within the same window and crashes, and my terminal spits out like crazy:
Original exception was:
Error in sys.exceptionhook:
I get the same result if instead of ChildApp().run() I do multiprocessing.Process(target=ChildApp().run()).start()
Using the subprocess library gets me closer to what I want:
# filename: test2.py
from kivy.app import App
from kivy.uix.label import Label
class ChildApp(App):
def build(self):
return Label(text='Child')
if __name__ == '__main__':
ChildApp().run()
# filename: test.py
from kivy.app import App
from kivy.uix.button import Button
import subprocess
class MainApp(App):
def build(self):
b = Button(text='Launch Child App')
b.bind(on_press=self.launchChild)
return b
def launchChild(self, button):
subprocess.call('ipython test2.py', shell=True)
if __name__ == '__main__':
MainApp().run()
This spawns the child window without error, however now the main window is locked (white canvas) and if I close the child window, it just gets reopened.
They need to be able pass data between one another. Any ideas on how to do this correctly in Windows? This post seems to suggest that this is possible but I'm not sure where to start.
I tried baconwichsand's code and can confirm with Python 3.6 and Windows 10 it does not work. Apparently only top level object classes can be pickled, and since both apps inherit from the App class python throws an error. However a top level definition that simply executes the ChildApp().run() command can be pickled and works. Here is my working code.
import multiprocessing
from kivy.app import App
from kivy.uix.label import Label
class MainApp(App):
def build(self):
return Label(text='Main App Window')
class OtherApp(App):
def build(self):
return Label(text='Other App Window')
def open_parent():
MainApp().run()
def open_child():
OtherApp().run()
if __name__ == '__main__':
a = multiprocessing.Process(target=open_parent)
b = multiprocessing.Process(target=open_child)
a.start()
b.start()
And here is the code I am using, including the Builder to use a shared .kv file for both windows.
import multiprocessing
from kivy.lang import Builder
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.widget import Widget
class MainRoot(Widget):
pass
class OtherRoot(Widget):
pass
class MainApp(App):
def build(self):
Builder.load_file('B:\Python_Codes\Testing Grounds\shared.kv')
main = MainRoot()
return main
class OtherApp(App):
def build(self):
Builder.load_file('B:\Python_Codes\Testing Grounds\shared.kv')
other = OtherRoot()
return other
def open_parent():
MainApp().run()
def open_child():
OtherApp().run()
if __name__ == '__main__':
a = multiprocessing.Process(target=open_parent)
b = multiprocessing.Process(target=open_child)
a.start()
b.start()
I'm not sure why it doesn't work with multiprocessing (I've never tried it), but it should at least work with subprocess. The reason your main window is locked is because subprocess.call blocks the thread that calls it while it waits for the subprocess to finish and return a result.
You want to use subprocess.Popen instead, which does not block.
bj0's answer regarding subprocess was correct.
Even better, I figured out how to do this via multiprocessing, which allows better communication and passing of information between apps. It wasn't working before because I did multiprocessing.Process(target=ChildApp().run()).start() when it should be multiprocessing.Process(target=ChildApp().run).start(). The following works
# filename: test.py
from kivy.app import App
from kivy.uix.button import Button
from test2 import ChildApp
import multiprocessing
class MainApp(App):
def build(self):
b = Button(text='Launch Child App')
b.bind(on_press=self.launchChild)
return b
def launchChild(self, button):
app = ChildApp()
p = multiprocessing.Process(target=app.run)
p.start()
if __name__ == '__main__':
MainApp().run()
# filename: test2.py
from kivy.app import App
from kivy.uix.label import Label
class ChildApp(App):
def build(self):
return Label(text='Child')
if __name__ == '__main__':
ChildApp().run()
Working on Ubuntu Python 3.10
Calling a subprocess in a multiprocess.
main.py
import multiprocessing
import subprocess
import shlex
#my kivy code..
def sub_window():
subprocess.call(shlex.split('python3 test.py'))
if __name__ == '__main__':
b = multiprocessing.Process(target=sub_window)
b.start()
MyApp().run()
test.py
#another kivy app script..
OtherApp().run()
I have this sample program written in Python and Kivy:
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
import time
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.image import AsyncImage
from kivy.uix.label import Label
class Application(App):
def build(self):
keyboard = Window.request_keyboard(None, self)
keyboard.bind(on_key_down=self.__on_key_down)
box_layout = BoxLayout(orientation='vertical')
self.__label = Label(text='Ready')
box_layout.add_widget(self.__label)
self.__img = AsyncImage()
box_layout.add_widget(self.__img)
return box_layout
def __on_key_down(self, keyboard, keycode, text, modifiers):
self.__set_status('Loading, please wait...')
print(self.__label.text)
time.sleep(3) # simulates long image download time
self.__img.source = 'http://pl.python.org/forum/Smileys/default/cheesy.gif'
def __set_status(self, text):
self.__label.text = text
# self.__label.canvas.ask_update()
app = Application()
app.run()
Basically what I want to do is label which displays status of application like: Ready, loading, done, etc. but my widget is not updating text when it should.
What can I do to immediately update text in self.__label widget?
I've tried ask_update() method but it seems to not work for this case.
You're blocking Kivy from doing anything when you call time.sleep. Kivy can't update the screen or process input until your function returns control to the main loop.
When you set the source of the AsyncImage, the image will be downloaded in the background without blocking the main loop. This widget is designed to just work automatically. It will display a loading animation until the image is downloaded, so you don't need to show any text.
If you want to show your own loading text, however, you can do so by using Loader directly. It will return a ProxyImage which you can use to determine when the image has loaded:
def __on_key_down(self, keyboard, keycode, text, modifiers):
self.__set_status('Loading, please wait...')
proxy = Loader.image('http://pl.python.org/forum/Smileys/default/cheesy.gif')
proxy.bind(on_load=self.__image_loaded)
def __image_loaded(self, proxy):
if proxy.image.texture:
self.__img.texture = proxy.image.texture
self.__set_status('Done')