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.
Related
When I run my kivy script,script runs without errors but I see no open window only a kivy icon in the task bar and it is not possible to open window.
This is a screenshot
This my code
from kivy.app import App
from kivy.uix.label import Label
class Test(App):
def build(self):
return Label(text='Hello
World',halign='center')
if __name__=='__main__':
Test().run()
I am a beginner in Python and Kivy. I want to create a simple program that should have an exit button on a window that must exit the app when pressed. Hi there, please help me. It's a request, please keep the code simple for a beginner. ^_^
Okay, I don't know how your code is, but this code could be copy-pasted as is and it will work (just import the necessary modules from kivy though):
.py
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
class yourscreen(FloatLayout):
def __init__(self, **kwargs):
#NEEDED
super(yourscreen, self).__init__(**kwargs)
self.button = Button(
text='exit',
size_hint=(0.5, 0.2)
)
self.button.bind(on_release= lambda x:self.exit())
self.add_widget(self.button)
def exit(self):
return MyApp().stop()
class MyApp(App):
def build(self):
return yourscreen()
if __name__ == '__main__':
MyApp().run()
You could pull out what you need which is MyApp().stop()
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 have simple code for kivy, on W10 runs without problem. It falls down during loading in kivy launcher. Problem is without message.
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
class View(BoxLayout):
def __init__(self):
super().__init__()
self.text = "No text"
but = Button(text = "Press",on_press = self.show)
self.add_widget(but)
self.lbl = Label()
self.add_widget(self.lbl)
def show(self,obj):
self.lbl.text = self.text
pass
class MyPaintApp(App):
def build(self):
return View()
if __name__ == '__main__':
MyPaintApp().run()
It does not run because you call super wrong.
As kivy launcher uses python 2, you need to pass your class (View) and the instance (self) to super.
You need to edit your class like this:
class View(BoxLayout):
def __init__(self,**kwargs):
super(View,self).__init__(**kwargs)
in every failure in kivy launcher, there is a '.kivy/log' directory inside the project directory that has a full log. you could find all the problem there.
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()