close webview in kivy - python

iam trying to close or destroy the webview and show other layout
here is my code for webview or if that won't possible i want to get the link of the current page and if current page link is equal to some link i want to exit the webview pls help me out this
from android.runnable import run_on_ui_thread as run_thread
from jnius import autoclass
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.app import runTouchApp
from kivy.clock import Clock, mainthread
#mainthread
def quit_screen():
app = App.get_running_app()
e=GridLayout()
d1=button(text="hi end")
e.add_widget(e)
app.root.switch_screen(e)
#run_thread
def key_back_handler():
webview.loadUrl("about:blank")
webview.clearHistory()
webview.clearCache(True)
webview.clearFormData()
webview.freeMemory()
Clock.schedule_once(quit_screen, 0)
#run_thread
def WebView(link,*args):
WebV = autoclass('android.webkit.WebView')
WebViewClient = autoclass('android.webkit.WebViewClient')
activity = autoclass('org.kivy.android.PythonActivity').mActivity
webview = WebV(activity)
settings = webview.getSettings()
settings.setJavaScriptEnabled(True)
settings.setUseWideViewPort(True)
settings.setLoadWithOverviewMode(True)
settings.setSupportZoom(True)
settings.setBuiltInZoomControls(True)
wvc = WebViewClient()
webview.setWebViewClient(wvc)
activity.setContentView(webview)
webview.loadUrl(link)
m=GridLayout(cols=1,rows=1)
d=GridLayout(cols=1,rows=1)
m.add_widget(d)
def Push(butoon):
WebView("https://www.google.com")
b=Button(text="hi")
b.bind(on_press=Push)
d.add_widget(b)
runTouchApp(m).run()
like this but its not closing on back pressed on android
Thanks for help in advance

The trick is to put the Webview in a Kivy ModalView and catch the back button/gesture from Java to exit the ModalView , for example:
https://github.com/RobertFlatt/Android-for-Python/tree/main/webview

Related

Kivy not opening new window on button press? (Subprocess/Popen, Python)

I am trying to open a new window upon a button press with kivy.
However, when I try to open the file with the separate window in it, it simply doesn't do anything, however I do know it runs the function the button calls.
Can any help with why this is happening?
Thank you
# Banned Book Project (Name pending) for digitech subject, Y13
from multiprocessing import Manager
from tkinter import Grid
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
import selenium
from selenium import webdriver
import subprocess
from kivy.uix.screenmanager import Screen,ScreenManager
def bannedBooksMain(self):
from subprocess import Popen, PIPE
process = Popen(['python3', 'seearch_parameters.py'], stdout=PIPE, stderr=PIPE)
class mainApp(GridLayout):
def __init__(self, **kwargs):
super(mainApp, self).__init__(**kwargs)
self.cols = 2
btn1 = Button(text='Database (Search)')
btn1.bind(on_release=bannedBooksMain)
self.add_widget(btn1)
class MyApp(App):
def build(self):
return mainApp()
MyApp().run()

Kivy(framework): blank window after running code

After running this code python idle gave me a blank window without any kivy widgets and python idle does not show any error.
what is the problem in this code?
screen shot of blank window
code:
import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
class me(App):
def __init__(self,b,g,l,t):
super(me, self).__init__()
self.b=Button(text='start')
self.g=GridLayout(cols=4)
self.l=Label(text='label')
self.t=TextInput()
self.g.add_widget(self.b)
self.g.add_widget(self.t)
self.g.add_widget(self.l)
m=me('b','g','l','t')
m.run()
Building the App should be done in a build() method of the App, and that method should return the root widget for the App. Like this:
class me(App):
def __init__(self,b,g,l,t):
super(me, self).__init__()
self.b=Button(text='start')
self.g=GridLayout(cols=4)
self.l=Label(text='label')
self.t=TextInput()
self.g.add_widget(self.b)
self.g.add_widget(self.t)
self.g.add_widget(self.l)
def build(self):
return self.g

filechooser in kivy without class

hi there i new in kivy iam learning without classes i want filechooser but any one tell me kivy filechooserIconView without class and not with kivy file only python file
from kivy.uix.button import Button
from kivy.app import runTouchApp
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
from kivy.uix.gridlayout import GridLayout
from kivy.clock import Clock
from kivy.core.window import Window
from kivy.uix.scrollview import ScrollView
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.filechooser import FileChooserIconView
a=GridLayout(cols=1)
def a1():
global b
global c
c.text=str(b.path)
b=FileChooserIconView(path='/storage/emulated/0/')
from functools import partial
c=Button(text='ok')
a.add_widget(c)
b.bind(on_release=a1)
a.add_widget(b)
runTouchApp(a)
Your code almost works, just a couple issues. First, you are binding on_release to the FileChooser, but I think you want that bound to the Button. Second, our a1() method must accept an argument (the button). Here is a modified version of your code with those changes:
from kivy.uix.button import Button
from kivy.app import runTouchApp
from kivy.uix.gridlayout import GridLayout
from kivy.uix.filechooser import FileChooserIconView
a=GridLayout(cols=1)
def a1(button): # handle required arg
global b
global c
c.text=str(b.path)
b=FileChooserIconView(path='/home/jra')
c=Button(text='ok')
a.add_widget(c)
c.bind(on_release=a1) # bind to button, not FileChooser
a.add_widget(b)
runTouchApp(a)

Fps bar on Kivy apps not disabling on mac os

I used Config.set('modules', 'monitor', '') on one of my Kivy apps to get the fps bar, but I can't get it disabled now.
Kivy version -- 1.10.1
I've tried everything and checked every line of code but can't get it disabled. Also if I write a basic code with just
from kivy.app import App
from kivy.uix.button import Button
imports I still getting the fps bar. I even reinstalled the whole Kivy environment but still I have the fps bar.
These are all the imports that I used in my file which created all the problem.
import kivy
kivy.require("1.10.1")
# from kivy.config import Config
# Config.set('graphics', 'resizable', 0)
# Config.set('graphics', 'width', 500)
# Config.set('graphics', 'height', 300)
# Config.set('modules', 'monitor', '')
from kivy.metrics import *
from kivy.core.window import Window
Window.size = (sp(500), sp(300))
from kivy.app import App
from kivy.uix.label import Label
from kivy.graphics import Line, InstructionGroup, Color
from kivy.properties import ObjectProperty, Property
from kivy.core.audio import SoundLoader
from kivy.clock import Clock
from kivy.uix.screenmanager import Screen, ScreenManager, FadeTransition
from kivy.lang import Builder
import random as rnd
import time
This is the basic code.
from kivy.app import App
from kivy.uix.button import Button
class TestApp(App):
def build(self):
return Button(text='Hello World')
TestApp().run()
Output Image
From eyllanesc's comment I've found a fix to this problem in case anyone else gets into the same problem.
Note: Always create a backup of the file.
Go to this directory.
Windows: C:\Users\tito\.kivy\config.ini
macOS: /Users/tito/.kivy/config.ini
Linux: /home/tito/.kivy/config.ini
Open config.ini
Find
[modules]
monitor =
Delete monitor = and Save it.
If anything happens just delete the config.ini and Kivy will create a new one but all your configurations will reset.

Kivy Text Editor Input Not Showing Up

I have unfortuanlly have encourtered an error in kivy and Python 3. I have not found a soultion via Google. I wanted to get text input at the very least but it does not show up. Just the text itself. Thank you for your time!
import kivy
kivy.require('1.10.1') # replace with your current kivy version !
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
class ColdKivyApp(App):
def build(self):
f = FloatLayout()
label = Label(text="Cold") #I acutally orginally called it Zone unitil I changed it into Cold cause it's really cold now
f.add_widget(label)
txt = TextInput(text='', focus=True, multiline=True, cursor_blink=True, background_color=(1,1,1,1))
f.add_widget(txt)
return f
if __name__ == '__main__':
ColdKivyApp().run()
It seems that there is a bug in TextInput when setting the focus in the constructor, a workaround is to set the focus an instant after the window is shown through Clock:
import kivy
kivy.require('1.10.1') # replace with your current kivy version !
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.clock import Clock
class ColdKivyApp(App):
def build(self):
f = FloatLayout()
label = Label(text="Cold")
f.add_widget(label)
txt = TextInput(multiline=True, cursor_blink=True, background_color=(1,1,1,1))
f.add_widget(txt)
Clock.schedule_once(lambda *args: setattr(txt, "focus", True))
return f
if __name__ == '__main__':
ColdKivyApp().run()

Categories

Resources