Restarting Kivy GUI only after closing window - python

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()

Related

How to make kivy app work in background even when it close?

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()

Kivi blackscreens with a textinput

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?

How to make kivy window close automatically after specific amount of time?

I am trying to close the kivy window automatically without pressing any button after a specific amount of time.
Code:
class Tester(App):
def build(self):
return Label(text="Hi")
if __name__ == '__main__':
Tester().run()
Thank you!
You can use Clock to invoke the stop method of the application:
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.label import Label
class Tester(App):
def build(self):
Clock.schedule_once(self.stop, 5)
return Label(text="Hi")
if __name__ == "__main__":
Tester().run()
You can also use App().get_running_app().stop() with the Clock to close the kivy window:
from kivy.app import App
from kivy.clock import Clock
from kivy.uix.label import Label
class Tester(App):
def build(self):
return Label(text="Hi")
Clock.schedule_once(App().get_running_app().stop, 1)
if __name__ == "__main__":
Tester().run()

C# how i can know the error code in kivy?

import kivy
from kivy.app import App
from kivy.uix.label import Label
class Simplekivy(App):
def build(self):
return Label(text="salam")
if __name__ == "__main__":
Simplekivy().run()
this is what i get
"
[CRITICAL] [App ] Unable to get a Window, abort.
"
import kivy
from kivy.app import App
from kivy.uix.label import Label
class Simplekivy(App):
def build(self):
return Label(text="aleykum salam")
if __name__ == "__main__":
Simplekivy().run()
This code works; your installation/dependencies must be at fault.
Please give us more to see what may be the issue.

Kivy window hide/show

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()

Categories