CardTransition isn't working in Kivy 1.9.1 - python

I am trying to make a simple app in kivy with multiple screens that makes use of the CardTransition, but when I try to run it I get an Error. I know the app itself works because I have tried it with a different transition and it works fine. I am using Python 3.4.4 with Kivy 1.9.1.
Here's the code:
import kivy
kivy.require("1.9.1")
from kivy.uix.boxlayout import BoxLayout
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, SwapTransition, CardTransition
KivyFile = Builder.load_string("""
<Screen1>:
name: "Screen1"
BoxLayout:
orientation: "vertical"
padding: 40
spacing: 20
Label:
text: "This is screen 1"
Button:
text: "Next Screen"
on_release: app.root.current = "Screen2"
<Screen2>:
name: "Screen2"
BoxLayout:
orientation: "vertical"
padding: 40
spacing: 20
Label:
text: "This is screen 2"
Button:
text: "Next Screen"
on_release: app.root.current = "Screen3"
<Screen3>:
name: "Screen3"
BoxLayout:
orientation: "vertical"
padding: 40
spacing: 20
Label:
text: "This is screen 3"
Button:
text: "Next Screen"
on_release: app.root.current = "Screen1"
""")
class Screen1(Screen):
pass
class Screen2(Screen):
pass
class Screen3(Screen):
pass
sm = ScreenManager(transition=CardTransition())
sm.add_widget(Screen1(name = "Screen1"))
sm.add_widget(Screen2(name = "Screen2"))
sm.add_widget(Screen3(name = "Screen3"))
class MyApp(App):
def build(self):
return sm
if __name__ == "__main__":
MyApp().run()
Here's the error I get:
Traceback (most recent call last):
File "C:/Users/Markus/Desktop/Kivy/TestTransitions.py", line 9, in <module>
from kivy.uix.screenmanager import ScreenManager, Screen, SwapTransition, CardTransition
ImportError: cannot import name 'CardTransition'

CardTransition was only added in kivy 1.10.

Related

Not able to save data instantly after clicking save button- kivy

I am trying to write in a text file after getting an input through TextInput in kivy. The problem is the data gets saved only after i close the app but i want the data to get saved as soon as I leave the specific screen and go to another screen. I am trying to input in add_staff and get it saved in the app through save() function.
import kivymd
import kivy
from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivy.uix.floatlayout import FloatLayout
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
Builder.load_string('''
<MenuScreen>:
nm:nm_input
BoxLayout:
TextInput:
id:nm_input
Button:
text: 'Add New Staff'
on_press: root.manager.current = 'add_staff'
Button:
text: 'View Staff Profile'
Button:
text: 'Salary report'
<Add_new_staff>:
nam: str(name_input)
job: job_input
GridLayout:
cols: 2
Label:
text: 'Name'
TextInput:
id: name_input
multiline: False
Label:
text: 'Job'
TextInput:
id: job_input
Label:
text: 'Salary'
TextInput:
Label:
text: 'Date of Joining'
TextInput:
Button:
text: 'Back to menu'
on_press: root.manager.current = 'menu'
Button:
text: 'Save'
on_press: root.save(name_input.text, job_input.text);root.manager.current = 'menu'
''')
class MenuScreen(Screen):
pass
class Add_new_staff(Screen):
def save(self, name, job):
fob = open('testi.txt','a')
fob.write(name + "\n")
fob.write(job)
fob.close()
class TestApp(App):
def build(self):
sm = ScreenManager()
sm.add_widget(MenuScreen(name='menu'))
sm.add_widget(Add_new_staff(name='add_staff'))
return sm
def lo(self,nm):
f=open('log.txt','a')
f.write(nm)
f.close()
if __name__ == '__main__':
TestApp().run()

KivyMD widgets displaying at the bottom of page in MDBoxLayout

KivyMD widgets are displayed at the bottom when using MDBoxLayout. I remember two MDTextField widgets and one MDRaisedButton, but they are located at the bottom. When using Kivy without KivyMD, there is no such problem. Here is my code:
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager
KV = """
ScreenManager:
MDScreen:
name: "main"
MDBoxLayout:
orientation: "vertical"
MDTextField:
hint_text: "User ID"
hint_size: 36
font_size: 36
MDTextField:
hint_text: "Message"
hint_size: 36
font_size: 36
MDRaisedButton:
text: "START"
font_size: 36
increment_width: 100
"""
class MainApp(MDApp):
def build(self):
return Builder.load_string(KV)
def on_start(self):
self.root.current = "main"
if __name__ == "__main__":
MainApp().run()
Result:
Result of run this code
I tried to replace MDBoxLayout to BoxLayout and MDScreen to Screen, but this not helped.
I solved a problem. I was need to add this to MDBoxLayout:
pos_hint: {"top": 1}
adaptive_height: True
And result code is:
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager
KV = """
ScreenManager:
MDScreen:
name: "main"
MDBoxLayout:
orientation: "vertical"
pos_hint: {"top": 1}
adaptive_height: True
MDTextField:
hint_text: "User ID"
hint_size: 36
font_size: 36
MDTextField:
hint_text: "Message"
hint_size: 36
font_size: 36
MDRaisedButton:
text: "START"
font_size: 36
increment_width: 100
"""
class MainApp(MDApp):
def build(self):
return Builder.load_string(KV)
def on_start(self):
self.root.current = "main"
if __name__ == "__main__":
MainApp().run()
Thanks KivyMD community in KivyMD's Discord!

i was trying to make an app using python and kivy although code is fine and not throwing any error i am getting black screen

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from kivy.core.window import Window
from kivy.uix.button import Button
Window.size = (500,700)
Builder.load_file('calc.kv')
class MyLayout(Widget):
pass
class App(App):
def build(self):
return MyLayout()
if __name__ == "__main__":
App().run()
this is my calc.py please help me to solve the problem
#:kivy 2.0.0
BoxLayout:
orientation:"vertical"
size: root.width, root.height
TextInput:
id: clac_input
text: "0"
halign: "right"
font_size : 65
size_hint: (1, .15)
GridLayout:
cols:4
rows:5
Button:
size_hint: (.2, .2)
font_size: 32
text: "%"
this is my calc.kv
From what I'm seeing the app is not returning any screens for your widgets to show on. I've changed a bit of your code to what I believe is correct that solves the issue.
.py
from kivy.app import App
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 kivy.uix.button import Button
Window.size = (500,700)
Builder.load_file('calc.kv')
class MyLayout(Widget):
pass
class Screen1(Screen):
pass
class WindowManager(ScreenManager):
pass
class App(App):
def build(self):
return WindowManager()
if __name__ == "__main__":
App().run()
calc.kv
<WindowManager>:
Screen1
<Screen1>:
BoxLayout:
orientation:"vertical"
size: root.width, root.height
GridLayout:
cols:4
rows:5
TextInput:
id: clac_input
text: "0"
halign: "right"
font_size : 65
size_hint: (1, .15)
Button:
size_hint: (.2, .2)
font_size: 32
text: "%"

Kivy Python IDs problems

I was using my main class as widget and was passing IDs from kv file to python without problems.
Now changed it to a screen as I need to switch between 2 screens but now I cant access the IDs declared in kv file. I get error - 'MainScreen'object has no attribute 'labINFO' - (this is the first label id I am trying to modify.)
Here are parts of py and kv files
from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.core.window import Window
from kivy.uix.popup import Popup
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.clock import Clock
class GSMNosScreen(Screen):
def gsmclose(self, obj):
Clock.schedule(self.startgetTemps,dataInterval)
BoilerApp.sm.current = 'main'
class MainScreen(Screen):
....
....
def __init__(self, **kwargs):
super(MainScreen, self).__init__(**kwargs)
self.labINFO.text = "Waiting for Pic..."
self.labM1.text = str(b1Min)
self.labM2.text = str(b2Min)
sm = ScreenManager() #transition=NoTransition()
sm.add_widget(MainScreen(name='main'))
sm.add_widget(GSMNosScreen(name='gsmnos'))
class BoilerApp(App):
def build(self):
self.title = 'Boiler Monitor'
return sm
if __name__ == "__main__":
from kivy.core.window import Window
Window.fullscreen = True
app = BoilerApp()
app.run()
KV file
<GSMNosScreen>:
labN1: labN1
size_hint: .5, .5
auto_dismiss: False
title: "GSM Numbers" # <<<<<<<<
separator_height: 0 # <<<<<<<<
GridLayout:
cols: 1
size: root.width, root.height
MyTLab:
id:labN1
font_size: 20
text: "Number 1 : ..."
MyBut:
text: "Close"
font_size: 20
size_hint: .2, .2
on_press: gsmclose()
<MainScreen>
labM1: labM1
labM2: labM2
labM3: labM3
labM4: labM4
labM5: labM5
labINFO: labINFO
labDT: labDT
GridLayout:
cols: 1
size: root.width, root.height
MyTLab:
text: "Monitor"
underline: True
size_hint: .5, .2
BoxLayout:
orientation: "horizontal"
size_hint_y: .2
MySTLab:
color: 0.8,.2,.2,0.7
text: "Min °C"
size_hint_x: 1.5
font_size: 25
MyMLab:
id: labM1
text: "50"
MyMLab:
id: labM2
text: "50"
MyMLab:
id: labM3
text: "50"
MyMLab:
id: labM4
text: "50"
BoxLayout:
orientation: "horizontal"
#cols:2
size_hint_y: .1
Label:
id: labDT
color: 0,1,0.5,0.8
text: "Starting ...."
font_size: sp(20)
size_hint_x: .3
Label:
id: labINFO
color: 0,1,0.5,0.8
text: "...."
font_size: sp(20)
size_hint_x: .7
I believe the problem is that the kv file has not been loaded yet when the lines:
sm = ScreenManager() #transition=NoTransition()
sm.add_widget(MainScreen(name='main'))
sm.add_widget(GSMNosScreen(name='gsmnos'))
are executed.
Try moving the above lines into the build() method of the App:
class BoilerApp(App):
def build(self):
self.title = 'Boiler Monitor'
sm = ScreenManager() # transition=NoTransition()
sm.add_widget(MainScreen(name='main'))
sm.add_widget(GSMNosScreen(name='gsmnos'))
return sm
The kv file (if it is named boiler.kv) is loaded before the build() mehod is called.

Extremely oversized/large buttons inside Kivy Screen class

For the sake of brevity I'll explain my problem before posing the code. Here it goes:
I have a Screen class in Kivy that holds two widgets, a GridLayout and an Image. The latter is fine, but the buttons are extremely oversized:
And here's my main.py:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.factory import Factory
from kivy.uix.gridlayout import GridLayout
from kivy.uix.image import Image
from kivy.config import Config
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
import configparser
Builder.load_file('kv\MainMain.kv')
#read configurations
config = configparser.RawConfigParser()
config.read('config.ini')
#read config values
width = config.getint('Default', 'MAX_WINDOW_WIDTH')
height = config.getint('Default', 'MAX_WINDOW_HEIGHT')
border = config.getint('Default', 'BORDERLESS')
#apply config values
Config.set('graphics','width', width)
Config.set('graphics', 'height', height)
Config.set('graphics', 'borderless', border)
Config.set('input', 'mouse', 'mouse,multitouch_on_demand')
#create screen manager
class ScreenManager(ScreenManager):
pass
#create background widget
class BackGround(Image):
pass
#image buttons
class MainButtons(GridLayout):
pass
#create main screen:
class MainScreen(Screen):
pass
#create main app
class MainMainApp(App):
def build(self):
return MainScreen()
#register class
Factory.register(MainScreen, cls=MainScreen)
#run
if __name__ == '__main__':
MainMainApp().run()
And here's my kv file:
<ScreenManager>:
id: screen_manager
MainScreen:
id: main_screen
name: 'MainScreen'
manager: screen_manager
ReadScreen:
id: read_screen
name: 'ReadScreen'
manager: screen_manager
<MainScreen>:
BackGround:
id: back_ground
source: 'images\\app_background.jpg'
size: root.width, root.height
MainButtons:
cols: 1
pos: root.width / 2 - 100, root.height / 4
size: 20, 10
Button:
id: button_read
text: "Read"
on_press: root.callback_read()
Button:
id: button_add
text: "Add"
Button:
id: button_manage
text: "Manage"
I'm really swamped on this one. Thanks for your help.
You can use size_hint for this.
Here is an example
The pyhton file:
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen,ScreenManager
Builder.load_file("my.kv")
class MainButtons(GridLayout):
pass
class MainScreen(Screen):
pass
class MainMainApp(App):
def build(self):
self.sm = ScreenManager()
self.sm.add_widget(MainScreen(name="main"))
return self.sm
if __name__ == '__main__':
MainMainApp().run()
and kivy file:
<MainScreen>:
MainButtons:
cols: 1
rows: 4
Label:
font_size: "40sp"
text: "Something"
Button:
size_hint: (1,0.1)
text: "Read"
Button:
size_hint: (1,0.1)
text: "Add"
Button:
size_hint: (1,0.1)
text: "Manage"
And output will be:
And if you want the buttons to be smaller in width, you add ´size_hint_x: None´ to your kv file, like this.
<MainScreen>:
MainButtons:
cols: 1
rows: 4
Label:
font_size: "40sp"
text: "Something"
Button:
size_hint: (1,0.1)
size_hint_x:None
text: "Read"
Button:
size_hint: (1,0.1)
size_hint_x:None
text: "Add"
Button:
size_hint: (1,0.1)
size_hint_x:None
text: "Manage"
Output will now be:

Categories