I'm a newby with python programming and tought, to learn let me create a project. Here is what i'm trying to do. I want to create a program that runs in system tray and fire's a program that is loaded in the background. Loaded in the background so I can reduce the start time of Kivy. After searching here and on google, i could not find the answer.
I have two files
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.config import Config
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window
from kivy.config import Config
#
import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.core.window import Window
Config.set('graphics', 'window_state', 'hidden')
class MyApp(App):
visible = False
def build(self):
Window.bordeless = 'True'
return Button(text='Hello World')
def on_start(self):
if self.visible:
self.root_window.hide()
self.visible = not self.visible
# self.root.focus = True
def do_show(self):
rootWindow = self.root_window
rootWindow.show()
print(self.root_window.focus)
if __name__ in ('__main__'):
ma = MyApp().run()
and
import wx
import someKivyThigy
from kivy.app import App
from someKivyThigy import MyApp
from buttonThing import MyDebugApp
from kivy.core.window import Window
TRAY_TOOLTIP = 'System Tray Demo'
TRAY_ICON = 'icon.png'
testVar = MyApp
def create_menu_item(menu, label, func):
item = wx.MenuItem(menu, -1, label)
menu.Bind(wx.EVT_MENU, func, id=item.GetId())
menu.AppendItem(item)
return item
class TaskBarIcon(wx.TaskBarIcon):
def __init__(self):
super(TaskBarIcon, self).__init__()
self.set_icon(TRAY_ICON)
self.Bind(wx.EVT_TASKBAR_LEFT_DOWN, self.on_left_down)
def CreatePopupMenu(self):
menu = wx.Menu()
create_menu_item(menu, 'Say Hello', self.on_hello)
menu.AppendSeparator()
create_menu_item(menu, 'Exit', self.on_exit)
return menu
def set_icon(self, path):
icon = wx.IconFromBitmap(wx.Bitmap(path))
self.SetIcon(icon, TRAY_TOOLTIP)
def on_left_down(self, event):
print 'Tray icon was left-clicked.'
MyApp().do_show()
def on_hello(self, event):
print 'Hello, world!'
def on_exit(self, event):
wx.CallAfter(self.Destroy)
def main():
app = wx.PySimpleApp()
TaskBarIcon()
testVar = MyApp().run()
app.MainLoop()
if __name__ == '__main__':
main()
When i left click the system tray icon, i get the error: "AttributeError: 'NoneType' object has no attribute 'show'". What am I doing wrong?
Kivy was made specially for Android Platform but it does not mean that it can't be used for desktop application development. But keep in mind that Window manager works with keeping SLD2 provider as backened so, this provider supports hide the window but cannot restore it.
For hiding window you can use : Window.hide() after importing Window from kivy.core.window
And for restoring you can use: Window.restore() or Window.show()
Related
I'm working in simple kivy app for android that play sound every minute, i want to make it work in background even when I close app
from kivy.app import App
from kivy.uix.button import Button
from kivy.core.audio import SoundLoader
import time
class MyApp(App):
def build(self):
self.sound = SoundLoader.load('1.wav')
start_but = Button(text='start',
on_press = self.click
)
return start_but
def click(self, obj):
while True:
self.sound.play()
time.sleep(60)
if __name__ == '__main__':
MyApp().run()
I tried to make the kivy window only restart after that i exit the window but i still can't find a way , any ideas ?
import sys
import kivy, os
from kivy.app import App
from kivy.uix.label import Label
from kivy.clock import Clock
class MyApp(App):
def build(self):
Clock.schedule_once(self.restart, 10)
return Label(text='Hello World One!')
def restart(self, dt):
os.execv(sys.executable, [sys.executable] + sys.argv)
if __name__ == '__main__':
MyApp().run()
I'm using Kivy 1.11.1 on Python 3.7, Ubuntu 20.04.
When I run my program my code shows a window, but the window is completely black and does not respond to any input.
My code is like this:
import kivy
kivy.require('1.11.1')
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.gridlayout import GridLayout
from kivy.core.window import Window
Window.clearcolor = (0.3961, 0.4627, 0.8, 1)
class LoginScreen(GridLayout):
def btn1Click(self, instance):
print('The button was clicked')
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.cols = 2
self.button1 = Button(text='Click Me', font_size=14)
self.button1.bind(on_press=self.btn1Click)
self.add_widget(self.button1)
self.add_widget(TextInput())
class CreateMachineProgram(App):
def build(self):
return LoginScreen()
if __name__ == '__main__':
CreateMachineProgram().run()
When I comment out the line self.add_widget(TextInput()), all of a sudden my code loads properly. What am I doing wrong?
I implemented pywebview on kivy. After clicking the button it will create the window, but after closing the window and click the button again, the window did not created.
How can I solve this problem ?
Below is my code :
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
import threading
import webview
class LoginScreen(BoxLayout):
def __init__(self, **kwargs):
super(LoginScreen, self).__init__(**kwargs)
self.btn1 = self.add_widget(Button(text='Web',on_press=self.on_web))
def on_web(self,instance):
url='http://www.google.com'
print("Im open windows")
webview.create_window('My Web App', url=url,debug=True)
class MyApp(App):
def build(self):
return LoginScreen()
if __name__ == '__main__':
MyApp().run()
In the following lines, remove the debug=True from the webview.create_window. Then add webview.start(debug=True) per https://github.com/r0x0r/pywebview and https://pywebview.flowrl.com/examples/debug.html
webview.create_window('My Web App', url=url)
webview.start(debug=True)
Also, the following are the params for the create_window function, notice that there is no debug per https://pypi.org/project/pywebview/0.5/:
webview.create_window(title, url, width=800, height=600, resizable=True, fullscreen=False)
The above works for me (Python 3, Kivy 1.11, Windows10) after the edit and test of your code.
I would like to prevent the EXIT of a kivy App on Android with the ESCAPE ARROW.
When I write exit_on_escape = 0 in the config.ini file, it does not change anything (however on WINDOWS 8 with the Esc key this works).
I have also tried unsuccessfully to intercept the on_request_close.
import kivy
kivy.require('1.0.8')
from kivy.core.window import WindowBase
from kivy.uix.widget import Widget
from kivy.app import App
from kivy.clock import Clock
class Test(Widget):
def __init__(self, **kwargs):
super(Test,self).__init__(**kwargs)
class TestApp(App):
def build(self):
Clock.schedule_once(self.my_callback, 0)
return Test()
def my_callback(self,*dt):
print("mycallback")
win=self._app_window
win.fullscreen=1 #OK
win.bind(on_request_close=self.alwaystrue)
def alwaystrue(*largs, **kwargs) :
print("alwaystrue")#never happens ...
return True
if __name__ == '__main__':
TestApp().run()
Do something like this in your App class:
from kivy.app import App
from kivy.core.window import Window
class MyApp(App):
def build(self):
self.bind(on_start=self.post_build_init)
# do all your normal stuff as well
return whatever
def post_build_init(self,ev):
if platform() == 'android':
import android
android.map_key(android.KEYCODE_BACK, 1001)
win = Window
win.bind(on_keyboard=self.key_handler)
def key_handler(self, window, keycode1, keycode2, text, modifiers):
if keycode1 == 27 or keycode1 == 1001:
# Do whatever you want here - or nothing at all
# Returning True will eat the keypress
return True
return False
This is probably not all be necessary (using the intermediate post_build_init() method and the android.map_key() thing), but I originally got it from a mailing post and I don't think I have an updated version. Anyway, it works for me.
Add the following code snippet just below the import kivy statement
from kivy.config import Config
Config.set('kivy', 'exit_on_escape', '0')
Here is a working example, how to use on_request_close.
The line:
Config.set('kivy', 'exit_on_escape', '0')
disables the Esc -button.
from kivy.config import Config
Config.set('kivy', 'exit_on_escape', '0')
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.popup import Popup
from kivy.core.window import Window
class ChildApp(App):
def build(self):
Window.bind(on_request_close=self.on_request_close)
return Label(text='Child')
def on_request_close(self, *args):
self.textpopup(title='Exit', text='Are you sure?')
return True
def textpopup(self, title='', text=''):
"""Open the pop-up with the name.
:param title: title of the pop-up to open
:type title: str
:param text: main text of the pop-up to open
:type text: str
:rtype: None
"""
box = BoxLayout(orientation='vertical')
box.add_widget(Label(text=text))
mybutton = Button(text='OK', size_hint=(1, 0.25))
box.add_widget(mybutton)
popup = Popup(title=title, content=box, size_hint=(None, None), size=(600, 300))
mybutton.bind(on_release=self.stop)
popup.open()
if __name__ == '__main__':
ChildApp().run()