Kivi blackscreens with a textinput - python

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?

Related

can't print a string after m.run() in python in kivy framework

I want to print('hello') after running the m.run() but IDLE just shows me the result of m.run and it does not print the hello. How can I print a string after m.run()?
This is an example of my problem:
import socket
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.gridlayout import GridLayout
class me (App):
def __init__(self):
super().__init__()
self.b=Button(text='button')
self.g=GridLayout(cols=4)
self.l=Label(text='label')
self.t=TextInput(text='Type here your message: ')
self.g.add_widget(self.b)
self.g.add_widget(self.t)
self.g.add_widget(self.l)
def build(self):
return self.g
m=me()
m.run()
print('hello')

Kivy ScrollView is not able to scroll

This is the .py file:
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.uix.stacklayout import StackLayout
class Stack(StackLayout):
def __init__(self,**kwargs):
super().__init__(**kwargs)
for i in range(0,100):
b1 = Button(text=str(i+1),size_hint=(.1,.1))
self.add_widget(b1)
class ScrollView(ScrollView):
pass
class GameApp(App):
def build(self):
return Stack()
GameApp().run()
And this is the .kV file:
<ScrollView>:
Stack:
size_hint:1,None
height:4000
In the output I am getting the buttons but I am unable to scroll.
Hello if you are new please check this link for beginners guide.
The ScrollView only works if you put 1 widget on it with specific adjustments to size ofcourse.
Instead of this line
return stack()
You must return the scrollview widget and add the stack() layout on top. Even better you can modify your code like so
For .py
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.scrollview import ScrollView
from kivy.uix.stacklayout import StackLayout
from kivy.uix.screenmanager import Screen
class MainScreen(Screen):
pass
class Stack(StackLayout):
def __init__(self,**kwargs):
super().__init__(**kwargs)
for i in range(0,100):
b1 = Button(text=str(i+1),size_hint=(.1,.1))
self.add_widget(b1)
class GameApp(App):
def build(self):
return MainScreen()
GameApp().run()
.kv
<MainScreen>:
ScrollView:
do_scroll_y: True
do_scroll_y: False
Stack:
size_hint:1,None
height:4000
I did not test the code but thats the logic of how the code can be. The rest you can check here

Unable to use ColorWheel without loading kv (AttributeError)

Can't understand why I get
AttributeError: 'ColorWheel' object has no attribute 'sv_s'
When I run
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.colorpicker import ColorWheel
from kivy.uix.boxlayout import BoxLayout
class MainApp(App):
def build(self):
box = BoxLayout()
box.add_widget(ColorWheel())
self.root = box
MainApp().run()
and I haven't any errors when use kv, for example:
from kivy.app import App
from kivy.lang import Builder
KV = '''
BoxLayout
ColorWheel
'''
class MainApp(App):
def build(self):
self.root = Builder.load_string(KV)
MainApp().run()
What did I miss?
You missed the warning in the ColorPicker documentation. That is a bug in the ColorWheel code. You should report it as a bug at Kivy Issues.

Implement pywebview on kivy

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.

Prevent the EXIT of a kivy App on Android with the ESCAPE ARROW

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

Categories