October 22, 2024
Chicago 12, Melborne City, USA
python

Floating Window with Python


I am coding an app in kivy python to put on my phone in which – after a certain amount of time – I want a window to ‘pop-up’ (float) over whatever app I’m currently using. I then want to be able to type an input into the window and it disappears. My problem is that I cannot find any guides/tutorials on how to do this floating window in python. I found something to do with Pyjnius/autoclass with accessing WindowManager from java and I tried that but I kept getting ‘JVM EXCEPTION: ClassDefNotFoundError’ and I have no idea how or why that error shows or how to fix it (I’ve put my attempted code below). Put simply, is there any way I can make a floating window appear over other apps using python? Or using python and something else like pyjnius or possibly pyqt?

Let me know if I need to clarify anything here with the language and such, I’m quite out of my depth with this. I’ve included only the app class and build function of my main.py file below, along with my pyjnius floating window test code that’s in another file. I kept getting a formatting error when I went to add the pyjnius log error code but I’ll add that as well as soon as I figure out what I’m doing wrong.
Any help is very appreciated and let me know if more information is needed or if this is the wrong type of question to ask.

PYJNIUS FLOATING WINDOW TEST

 from jnius import autoclass, cast
 from kivy.app import App
 from kivy.uix.widget import Widget
 from kivy.lang.builder import Builder

 class FloatingWindowsareCool(App):
     def build(self):

        PythonActivity = autoclass('org.kivy.android.PythonActivity')
        activity = PythonActivity.mActivity

        WindowManager = autoclass('android.view.WindowManager')
        LayoutParems = autoclass('android.view.WindowManager$LayoutParems')
        Gravity = autoclass('android.view.Gravity')
        Color = autoclass('android.graphics.Color')
        PixelFormat = autoclass('android.graphics.PixelFormat')

        window_manager = cast('android.view.WindowManager',  activity.getSystemService('activity.WINDOW_SERVICE'))
        parems = LayoutParems(LayoutParems.WRAP_CONTENT,
                          LayoutParems.TYPE_APPLICATION_OVERLAY,
                          LayoutParems.FLAG_NOT_FOCUSABLE,
                          PixelFormat.TRANSLUCENT
                          )
        parems.gravity = Gravity.TOP | Gravity.LEFT
        parems.x = 0
        parems.y = 100

        LinearLayout = autoclass('android.widget.LinearLayout')
        layout = LinearLayout(activity)
        layout.setBackgroundColor(Color.argb(1, 0, 1, 1))

        window_manager.addView(layout, parems)

        return Widget()


if __name__ == '__main__':
    FloatingWindowsareCool().run()`

MAIN.PY

`class DemoApp(MDApp):

    def build(self):
        Window.borderless = True
        Window.size = (Window.width * 1.0, Window.height * 0.5)

        #Window.size = (300, 600)
        Window.fullscreen = False
        self.screen_manager = ScreenManager()
        sm = ScreenManager()
        #sm.add_widget(MainScreen(name="main"))
        sm.add_widget(BrainScreen(name="brain"))
        sm.add_widget(FindObjectScreen(name="findhiddenobject"))
        sm.add_widget(PlaySoundScreen(name="sound"))
        sm.add_widget(TryAgainScreen(name="retry"))
        sm.add_widget(CorrectScreen(name="correct"))
        sm.add_widget(WellDoneScreen(name="welldone"))
        return sm


if __name__ == '__main__':
    DemoApp().run()`



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video