Black screen on kivy how to fix - python

when I try to run the code it just displays a blank screen I tried to find answers everywhere but nothing works
This is my main.py file:
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.graphics import *
from kivy.lang import *
from kivy.uix.screenmanager import *
class MyWidgets(Widget):
pass
kv = Builder.load_file('math.kv')
class Calculator(App):
def build(self):
return kv
if __name__ == '__main__':
Calculator().run()
And this is my math.kv file:
<MyWidgets>
num: num
GridLayout:
cols: 1
TextInput:
id: num
multiline: False
GridLayout:
cols: 3
Button:
text: '1'
font_size: 25
Button:
text: '2'
font_size: 25
Button:
text: '3'
font_size: 25

Looks like you have an indent error in build function. Still, that should have shown some error instead of showing a blank screen. Then in your .kv file, I don't know what that num is. After removing num from .kv and correcting indentation error I run the file and it works fine.
Here's the code I used:
.py
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.graphics import *
from kivy.lang import *
from kivy.uix.screenmanager import *
class MyWidgets(Widget):
pass
class Calculator(App):
def build(self):
kv = Builder.load_file('math.kv')
return kv
if __name__ == '__main__':
Calculator().run()
.kv
<MyWidgets>
GridLayout:
cols: 1
TextInput:
id: num
multiline: False
GridLayout:
cols: 3
Button:
text: '1'
font_size: 25
Button:
text: '2'
font_size: 25
Button:
text: '3'
font_size: 25

Related

since print('hello') is working after pressing enter in Text Input but Why don't these two work?

since print('hello') is working after pressing enter in Text Input but Why don't these two work?
self.grid2.add_widget(Button())
self.grid2.add_widget(TextInput())
Code:
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.lang import Builder
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.graphics import Color, Rectangle
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.uix.button import Button
Builder.load_string("""
<Button>
size_hint: (None, None)
height: 33
width: 100
text:'1'
background_normal: ''
background_down:'1, .3, .4, .85'
background_color: 1, .3, .4, .85
<TextInput>
selection_color:1, .3, .4, .4
on_text_validate:app.enter()
cursor_color: 255/255, 223/255, 5/255, 1
multiline:False
height: 33
width:800
size_hint: (None, None)
background_color: 0,0,0,0
foreground_color: 255/255, 143/255, 5/255, 0.8
<Grid>
t1:t1
b1:b1
grid:grid
grid2:grid2
GridLayout:
cols:1
id:grid
size: root.width - 400,root.height - 400
pos:100,100
GridLayout:
id:grid2
cols:2
Button:
id:b1
TextInput:
id:t1
""")
class Grid(Widget):
t1=ObjectProperty(None)
b1=ObjectProperty(None)
grid=ObjectProperty(None)
grid2=ObjectProperty(None)
def enter(self):
print('hello')
self.grid2.add_widget(Button())
self.grid2.add_widget(TextInput())
gr=Grid()
class foo(App):
def build(self):
Window.clearcolor='#1618388'
return Grid()
def enter(self):
gr.enter()
if __name__ == '__main__':
foo().run()
it worked for me if i returned gr instead for Grid()
class foo(App):
def build(self):
Window.clearcolor = '#1618388'
return gr
def enter(self):
gr.enter()

Outputting the input text in a new screen

Basically i'm trying to take some input from a user, perform some actions, and then output the result in a new screen (i'm thinking as the label of the new screen). I've managed to switch between screens but i cannot figure out how to output the input from the first screen to the second screen. I've tried to make the input data a global variable so i can assign to the text of the label of the output screen, but it didn't work. Here's my python file:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen,ScreenManager
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen,ScreenManager
from kivy.core.window import Window
from kivymd.app import MDApp
from kivy.uix.image import Image
from kivy.animation import Animation
from kivy.clock import Clock
from kivy.properties import ColorProperty
from kivy.uix.popup import Popup
from kivy.uix.floatlayout import FloatLayout
from plyer import filechooser
from kivymd.uix.dialog import MDDialog
Window.clearcolor = (1,1,1,1)
g_data = 'xstring'
class MainWindow(Screen):
def get_data(self):
global g_data
g_data = self.ids.user_input.text
class OutputScreen(Screen):
def ex(self):
self.ids.output_label.text = g_data
class mainApp(MDApp):
def __init__(self):
super().__init__()
def change_screen(self):
screemanager = self.root.ids['screenmanager']
screemanager.current = 'output'
def change(self):
self.change_screen()
if __name__ == '__main__':
mainApp().run()
and my kv file:
#:import utils kivy.utils
GridLayout:
cols:1
ScreenManager:
id: screenmanager
MainWindow:
id: main
name: 'main'
OutputScreen:
id: output
name: 'output'
<MainWindow>:
FloatLayout:
TextInput:
id: user_input
pos_hint:{"x" : 0.05, "top" : 0.9}
size_hint: 0.9, 0.37
Button:
pos_hint:{"top" : 0.51, "x" : 0.05}
size_hint: (None,None)
width : 150
height : 40
font_size : 23
text:'Submit'
on_release: app.change()
<OutputScreen>:
FloatLayout:
Label:
id: output_label
text: root.ex()
color: 0,0,0,1
Thank you very much.
I can't get your example to work, but you would need to do this:
g_data = '' # new global variable defined
class MainWindow(Screen):
def get_data(self):
global g_data # global goes here
g_data = self.ids.user_input.text
class OutputScreen(Screen):
def ex(self):
self.ids.output_label.text = g_data

How to use button behaviour inside of scrollview in kivy?

I am trying to figure out how I could make a label have the properties of a button, while the label itself is scrollable.
I have tried a couple of different things but haven't managed to get it to work, here's my current code, my end goal is to have every number as a separate "clickable" entity, but for now if I could figure out how to make the whole lable have the properties of a button, that would be good enough.
My code:
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty
from kivy.clock import Clock
from kivy.properties import ListProperty
from kivy.properties import StringProperty
from kivy.uix.scrollview import ScrollView
from kivy.uix.button import ButtonBehavior
from kivy.uix.label import Label
class Testime(Widget):
pass
class loendtest(App):
tulemus = NumericProperty()
loend = ListProperty()
loend2 = StringProperty()
loend3 = StringProperty()
def build(self):
return Testime()
if __name__ == "__main__":
loendtest().run()
<Testime>:
GridLayout:
cols:3
size: root.size
Button:
text: "add a result"
on_press:
app.tulemus += 1
app.loend.append(app.tulemus)
print(app.tulemus)
print(app.loend)
app.loend2 = " " + str(app.loend).strip('[]').replace(',', '\n')
print(app.loend2.split())
app.loend3 = " " + str(app.loend2.split()).strip('[]').replace(',', '\n')
print(app.loend3)
ScrollView:
Label:
size_hint_y: 2
font_size: 75
text_size: None, self.height
valign: "top"
text:
app.loend3
Just create a custom Label that acts like a Button:
class MyButtonLabel(ButtonBehavior, Label):
pass
... and replace your Label with it...
OR, you can do that on the kv side, like this:
<MyButtonLabel#ButtonBehavior+Label>

Kivy Adding image as button to another page

i have an app, that in the future, will be a cards game counter, but right now its nowhere near that stage, anyways, i have 3 files, main.py, screens.kv,screens2.kv(which doesnt have anything in it) now i also got a picture, and its a button, each time its pressed, for test it writes down "pressed", i've been on this for at least 2 hours and cant figure out how to make the button appear when i press "Start Pasmar" (Pasmar is the name of the game in future), all it brings up is a blank screen after i press the button, but i did replace them and it only showed the button, nothing else.
This my current code:
#-*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.properties import ObjectProperty
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.tabbedpanel import TabbedPanelHeader
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from functools import partial
from kivy.base import runTouchApp
from kivy.uix.image import Image
from kivy.uix.widget import Widget
from kivy.uix.popup import Popup
import sys
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.image import Image
from kivy.uix.behaviors import ButtonBehavior
from kivy.lang import Builder
from kivy.base import runTouchApp
import random
import time
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
from kivy.properties import ObjectProperty, NumericProperty
import time
import threading
class ScreensApp(App):
def build(self):
return RootWidget()
class ScreenOne(Screen):
pass
class ScreenTwo(Screen):
def __init__(self, **kwargs):
super(ScreenTwo, self).__init__(**kwargs)
class RootWidget(FloatLayout):
pass
class ImageButton(ButtonBehavior, Image):
def on_press(self):
print ('pressed')
def displayScreenThenLeave(self):
self.changeScreen()
def changeScreen(self):
if self.manager.current == 'screen1':
self.manager.current = 'screen2'
else:
self.manager.current = 'screen1'
class Manager(ScreenManager):
screen_one = ObjectProperty(None)
screen_two = ObjectProperty(None)
class ScreensApp(App):
def build(self):
m = Manager(transition=NoTransition())
return m
if __name__ == "__main__":
ScreensApp().run()
class The_AssignmentApp(App):
def build(self):
return RootWidget()
if __name__ == "__main__":
The_AssignmentApp().run()
This is the kv file:
#:kivy 1.10.1
<ScreenOne>:
BoxLayout:
orientation: "vertical"
size: root.size
spacing: 15
padding: 200
Label:
font_size: 34
text: "Pasmar"
Button:
background_color: 0, 255, 0, .255
text: "Start Pasmar"
on_release: root.manager.current = "screen2"
<ScreenTwo>:
BoxLayout:
orientation: "vertical"
size: root.size
spacing: 20
padding: 20
<Manager>:
id: screen_manager
screen_one: screen_one
screen_two: screen_two
ScreenOne:
id: screen_one
name: "screen1"
manager: screen_manager
ScreenTwo:
id: screen_two
name: "screen2"
manager: screen_manager

Using value from textinput in python

I want to make an app which would print text entered in TextInput when I press Print.
After a couple of hours of searching the web I still can't understand how to assing value of TextInput to variable in python script.
This is Kivy code:
<SimpleRoot>:
orientation:"vertical"
padding: root.width * .02, root.height * .02
spacing: "10dp"
TextInput:
id: txt
Button:
text: 'Print'
on_press: root.printTxt(txt.text)
Python script:
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.boxlayout import BoxLayout
class SimpleRoot(BoxLayout): # 2
def printTxt(text):
print txt.text
pass
class SimpleApp(App): # 1
def build(self):
# Return root widget
return SimpleRoot()
if __name__ == "__main__":
SimpleApp().run()
Try changing this:
class SimpleRoot(BoxLayout): # 2
def printTxt(text):
print txt.text
To this
class SimpleRoot(BoxLayout): # 2
def printTxt(self, text):
print text

Categories

Resources