python kivy - on_press being executed twice - python

Newbie to Python - I am writing a raspberry pi app with touch screen. Manage to make it work but the Increment function is being executed twice when pressing the + button (same for decrement). These buttons are enabled only while another button is pressed but this is not the problem.
Any help please?
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
Window.show_cursor = False
import serial
import time
port = serial.Serial("/dev/ttyS0", baudrate=19200, timeout=3.0)
btnNum = '0'
b1Tmp = 0; b2Tmp = 0; b3Tmp = 0; b4Tmp = 0; b5Tmp = 0; b6Tmp = 0; b7Tmp = 0; b8Tmp = 0
b1Max =50; b2Max =50; b3Max =50; b4Max =50; b5Max =50; b6Max =50; b7Max =50; b8Max =50;
class Container(Widget):
labR1= ObjectProperty(None)
labR2= ObjectProperty(None)
labR3= ObjectProperty(None)
labR4= ObjectProperty(None)
labR5= ObjectProperty(None)
labR6= ObjectProperty(None)
labR7= ObjectProperty(None)
labR8= ObjectProperty(None)
butUP= ObjectProperty(None)
butDN= ObjectProperty(None)
labM1= ObjectProperty(None)
labM2= ObjectProperty(None)
labM3= ObjectProperty(None)
labM4= ObjectProperty(None)
labM5= ObjectProperty(None)
labM6= ObjectProperty(None)
labM7= ObjectProperty(None)
labM8= ObjectProperty(None)
def bpressed(self,instance):
global btnNum
btnNum = instance.text
self.butDN.disabled = False
self.butUP.disabled = False
def breleased(self,instance):
global btnNum
btnNum = '0'
self.butDN.disabled = True
self.butUP.disabled = True
def increment(self,instance):
global btnNum,b1Max,b2Max,b3Max,b4Max,b5Max,b6Max,b7Max,b8Max
if (btnNum == '1'):
if (b1Max<100): b1Max+=2
self.labM1.text = str(b1Max)
if (btnNum == '2'):
if (b2Max<100): b2Max=b2Max+1
self.labM2.text = str(b2Max)
if (btnNum=='3'):
if (b3Max<100): b3Max+=1
self.labM3.text = str(b3Max)
if (btnNum=='4'):
if (b4Max<100): b4Max+=1
self.labM4.text = str(b4Max)
if (btnNum=='5'):
if (b5Max<100): b5Max+=1
self.labM5.text = str(b5Max)
if (btnNum=='6'):
if (b6Max<100): b6Max+=1
self.labM6.text = str(b6Max)
if (btnNum=='7'):
if (b7Max<100): b7Max+=1
self.labM7.text = str(b7Max)
if (btnNum=='8'):
if (b8Max<100): b8Max+=1
self.labM8.text = str(b8Max)
def decrement(self,instance):
global btnNum,b1Max,b2Max,b3Max,b4Max,b5Max,b6Max,b7Max,b8Max
if (btnNum == '1'):
if (b1Max>20): b1Max-=1
self.labM1.text = str(b1Max)
if (btnNum == '2'):
if (b2Max>20): b2Max-=1
self.labM2.text = str(b2Max)
if (btnNum=='3'):
if (b3Max>20): b3Max-=1
self.labM3.text = str(b3Max)
if (btnNum=='4'):
if (b4Max>20): b4Max-=1
self.labM4.text = str(b4Max)
if (btnNum=='5'):
if (b5Max>20): b5Max-=1
self.labM5.text = str(b5Max)
if (btnNum=='6'):
if (b6Max>20): b6Max-=1
self.labM6.text = str(b6Max)
if (btnNum=='7'):
if (b7Max>20): b7Max-=1
self.labM7.text = str(b7Max)
if (btnNum=='8'):
if (b8Max>20): b8Max-=1
self.labM8.text = str(b8Max)
def serialsend(self,instance):
port.write("\r\nSay something:".encode())
def close(self , obj):
App.get_running_app().stop()
Window.close()
class BoilerApp(App):
def build(self):
self.title = 'Boiler Monitor'
return Container()
if __name__ == "__main__":
from kivy.core.window import Window
Window.fullscreen = True
app = BoilerApp()
app.run()
kv file
<MyButton#Button>:
color: .8,.9,0,1
font_size: 30
<MyLabel#Label>:
color: .8,.9,0,1
font_size: 30
<Container>
labR1: labR1
labR2: labR2
labR3: labR3
labR4: labR4
labR5: labR5
labR6: labR6
labR7: labR7
labR8: labR8
butUP: butUP
butDN: butDN
labM1: labM1
labM2: labM2
labM3: labM3
labM4: labM4
labM5: labM5
labM6: labM6
labM7: labM7
labM8: labM8
GridLayout:
cols: 1
size: root.width, root.height
Label:
text: "Boiler Alarm"
size_hint: .5, .2
font_size: 40
background_color: 1,0,0,1
GridLayout:
cols:10
MyLabel:
text: "Boiler"
MyButton:
id: butB1
text: "1"
on_press: root.bpressed(self)
on_release: root.breleased(self)
MyButton:
id: butB2
text: "2"
on_press: root.bpressed(self)
on_release: root.breleased(self)
MyButton:
id: butB3
text: "3"
on_press: root.bpressed(self)
on_release: root.breleased(self)
MyButton:
id: butB4
text: "4"
on_press: root.bpressed(self)
on_release: root.breleased(self)
MyButton:
id: butB5
text: "5"
on_press: root.bpressed(self)
on_release: root.breleased(self)
MyButton:
id: butB6
text: "6"
on_press: root.bpressed(self)
on_release: root.breleased(self)
MyButton:
id: butB7
text: "7"
on_press: root.bpressed(self)
on_release: root.breleased(self)
MyButton:
id: butB8
text: "8"
on_press: root.bpressed(self)
on_release: root.breleased(self)
MyLabel:
text: ""
Label:
text: "Reading"
font_size: 25
MyLabel:
id: labR1
text: "0"
MyLabel:
id: labR2
text: "0"
MyLabel:
id: labR3
text: "0"
MyLabel:
id: labR4
text: "0"
MyLabel:
id: labR5
text: "0"
MyLabel:
id: labR6
text: "0"
MyLabel:
id: labR7
text: "0"
MyLabel:
id: labR8
text: "0"
MyButton:
id: butUP
text: "+"
disabled: True
on_press: root.increment(self)
Label:
text: "Minimum"
font_size: 25
MyLabel:
id: labM1
text: "50"
MyLabel:
id: labM2
text: "50"
MyLabel:
id: labM3
text: "50"
MyLabel:
id: labM4
text: "50"
MyLabel:
id: labM5
text: "50"
MyLabel:
id: labM6
text: "50"
MyLabel:
id: labM7
text: "50"
MyLabel:
id: labM8
text: "50"
MyButton:
id: butDN
text: "-"
disabled: True
on_release: root.decrement(self)
Button:
id: btnexit
text: "Exit"
size_hint: .5, .1
on_press: root.close(self)

I had the same problem and found multiple workarounds after fighting this for a while.
The issue seems to be that kivy interprets both the mouse and the touch events simultaneously. This means when you touch the display both a "mouse" click and a "touch" press event is fired, resulting in double presses.
Solution 1 – change the kivy config.ini
The easiest fix is to edit the [input]-section in ~/.kivy/config.ini in su a way:
[input]
%(name)s = probesysfs,provider=hidinput
mouse = mouse
# mtdev_%(name)s = probesysfs,provider=mtdev
hid_%(name)s = probesysfs,provider=hidinput
Just comment out the line starting with mtdev_ and restart the program.
Solution 2 – manually check the event-kind
If you want to use the mouse somewhere still, there is a different way to do this. Let's assume you create a button somewhere like this:
button = ToggleButton(text="Cool Button")
button.bind(on_release=on_toggle)
self.add_widget(button)
You can then check whether our on_toggle-function is activated by a mouse- or a touch-event as follows:
from kivy.input.providers.mouse import MouseMotionEvent
def on_toggle(button_instance):
if isinstance(button_instance.last_touch, MouseMotionEvent):
print(f"Last Touch was from the Mouse {button_instance.last_touch}")

You should not use timeout with kivy. You should instead use the Clock function. The timeout function would halt all your program which may lead to these issues. Whereas Clock schedules specific events without halting the program altogether.

Related

How do i add a signature/paint method to a screen in my app in either kv lang or normal kivy?

how do i add buttons to my paintScreen class without the on_touch_down/move interfering and making it so that i draw within a BoxLayout canvas and separate the buttons in a different box so that it wont draw over the buttons? This is the current problem i am having. Also if that problem is fixed then would i be able to save that drawing as an img if i use export_to_png
from kivy.app import App
from kivy.graphics.context_instructions import Color
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.properties import ObjectProperty
from kivy.uix.textinput import TextInput
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.dropdown import DropDown
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.spinner import Spinner
from kivy.uix.checkbox import CheckBox
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang.builder import Builder
from kivy.uix.recycleview import RecycleView
from kivy.uix.scrollview import ScrollView
from kivy.graphics import Line, Rectangle, Ellipse, Triangle
Builder.load_string("""
<firstScreen>
BoxLayout:
orientation: "vertical"
ScrollView:
size: root.size
GridLayout:
orientation: "vertical"
size_hint_y: None
height: self.minimum_height #<<<<<<<<<<<<<<<<<<<<
row_default_height: 90
cols: 2
Label:
text: "Name: "
TextInput:
multiline: False
Label:
text: "Address: "
TextInput:
multiline: False
Label:
text: "Contact: "
TextInput:
multiline: False
Label:
text: "Telephone: "
TextInput:
multiline: False
Label:
text: "Order Number: "
TextInput:
multiline: False
Label:
text: "Transporter's Name: "
TextInput:
multiline: False
Label:
text: "Vehicle Reg. No.: "
TextInput:
multiline: False
Label:
text: "Driver's Name: "
TextInput:
multiline: False
Label:
text:"Hazchem Requirements: "
Spinner:
id: spinner_1
text: "< Select >"
values: root.pick_opt
Spinner:
id: spinner_2
text: "Waste Details"
values: root.pick_classification
GridLayout:
size_hint: 1, None
rows:2
Label:
text: "Non-Hazardous"
Label:
text: "Hazardous"
CheckBox:
on_active: root.chk_tick
CheckBox:
on_active: root.chk_tick
Spinner:
id: spinner_3
text: "Service Details"
values: root.sog
TextInput:
multiline: False
hint_text: "Enter Volume/Weight"
Spinner:
id: spinner_4
text: "Service Details"
values: root.sog
TextInput:
multiline: False
hint_text: "Enter Volume/Weight"
Spinner:
id: spinner_5
text: "Service Details"
values: root.sog
TextInput:
multiline: False
hint_text: "Enter Volume/Weight"
Spinner:
id: spinner_6
text: "Service Details"
values: root.sog
TextInput:
multiline: False
hint_text: "Enter Volume/Weight"
Button:
text: "Next Screen"
size_hint: 1,.1
on_press: root.manager.current = "screen_2"
<secondScreen>
BoxLayout:
orientation: "horizontal"
ScrollView:
size: root.size
GridLayout:
orientation: "vertical"
size_hint_y: None
height: self.minimum_height #<<<<<<<<<<<<<<<<<<<<
row_default_height: 120
cols: 1
BoxLayout:
orientation: 'vertical'
Spinner:
id: spinner_7
text: "details"
width: self.parent.width * 2
values: root.pick_dets
Spinner:
id: spinner_8
text: "details"
values: root.pick_dets
Spinner:
id: spinner_9
text: "details"
values: root.pick_dets
GridLayout:
cols: 2
rows: 3
Label:
text: "Start Time"
Label:
text: "End Time"
TextInput:
multiline: False
TextInput:
multiline: False
GridLayout:
cols: 2
Label:
text: 'Special Instructions/Remarks'
TextInput:
hint_text: 'Enter Remarks'
Button:
id: signature
text: 'Signature'
on_press: root.manager.current = "screen_signature"
BoxLayout:
orientation: 'vertical'
Button:
text: "Previous Screen"
size_hint: 1,.1
on_press: root.manager.current = "screen_1"
Button:
text: "Next Screen"
size_hint: 1,.1
on_press: root.manager.current = "screen_2"
<paintScreen>
""")
class paintScreen(Screen):
# On mouse press how Paint_brush behave
def on_touch_down(self, touch):
color = (1, 1, 1)
with self.canvas:
Color(*color, mode='hsv')
d = 30.
Line(pos=(touch.x - d / 2, touch.y - d / 2), size=(d, d))
touch.ud['line'] = Line(points=(touch.x, touch.y))
# On mouse movement how Paint_brush behave
def on_touch_move(self, touch):
touch.ud['line'].points += [touch.x, touch.y]
pass
class firstScreen(Screen):
def __init__(self, **kwargs):
self.pick_opt = ["yes", "no"]
self.pick_classification = ["Classification (SANAS 10228)", "HR (1,2,3,4 - Minimum Requirements)",
"Emergency Response Guide Reference"]
self.sog = ['Oil', "Effluent", "Sludge", "Other"]
super(firstScreen, self).__init__(**kwargs)
def chk_tick(self, instance, value):
if value:
print("ticked")
else:
print('unticked')
pass
class secondScreen(Screen):
def __init__(self, **kwargs):
self.pick_dets = ["HP Cleaning", "Chemicals", "Soil Treatment",
"Minor Civils & Plumbing", "Effluent Treatment", "Other"]
super(secondScreen, self).__init__(**kwargs)
pass
class MyScreenManager(ScreenManager, RecycleView):
pass
screen_manager: ScreenManager = ScreenManager()
screen_manager.add_widget(firstScreen(name="screen_1"))
screen_manager.add_widget(secondScreen(name="screen_2"))
screen_manager.add_widget(paintScreen(name="screen_signature"))
class Myapp(App):
def build(self):
return screen_manager
def clear_canvas(self, obj):
self.painter.canvas.clear()
if __name__ == "__main__":
Myapp().run()

adding two Values in two kivy screens and getting the result

I'm trying to build an app that can calculate the sum of two values. I have a screen called Therdwindow that has three text input widgets.
from kivy.app import App
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.scatter import Scatter
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.textinput import TextInput
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen`
class MainWindow(Screen):
pass
class SecondWindow(Screen):
pass
class Therdwindow(Screen):
pass
class Fourwindow(Screen):
pass
class FunfthWindow(Screen):
def calculate3(self,bibi,mimi):
kiki = str(float(bibi) + float(mimi))
if kiki:
try :
self.result_1.text = str(eval(kiki))
except Exception:
self.result_1.text = 'Error'
class Sixwindow(Screen):
pass
class WindowManager(ScreenManager):
psss
kv = Builder.load_file("layout.kv")
class MyMainApp(App):
def build(self):
return kv
if __name__ == "__main__":
MyMainApp().run()
A person is supposed to enter the Password 'gin' than click on a button 'NEXT' to go to the next screen named SecondWindow , then click on a button 'NEXT' to go to the next screen named TherdWindow ,
Then enter a First Value in the Box then click on a button 'NEXT' to go to the next screen named fourWindow ,
than enter the second Value and click on a button 'NEXT' to go to the next screen named funfthWindow .
there should have the 'Result' if he click on a button result. In this screen there is a Label that should print the volume of the sum that the person specified.
layout.kv
<CustButton#Button>:
font_size: 40
WindowManager:
MainWindow:
SecondWindow:
Therdwindow:
Fourwindow:
FunfthWindow:
Sixwindow:
<MainWindow>:
name: "main"
<MainWindow>:
name: "main"
GridLayout:
cols:1
GridLayout:
cols: 2
orientation: 'vertical'
Label:
text: "Password: "
font_size: "40sp"
background_color: (1,1,0,1)
font_name: 'RobotoMono-Regular.ttf'
TextInput:
id: passw
multiline: False
font_size: "40sp"
CustButton:
text: "Submit"
background_color: (0.8,0.8,0,1)
on_press:
app.root.current = "second" if passw.text == "gin" else "six"
root.manager.transition.duration = 1
root.manager.transition.direction = "left"
<SecondWindow>:
name: "second"
GridLayout:
cols: 1
spacing: 10
CustButton:
text: "Go Back"
on_press:
app.root.current = "main"
root.manager.transition.direction = "right"
CustButton:
text: "next"
on_press:
app.root.current = "therd"
root.manager.transition.direction = "left"
<Therdwindow>:
id:lulu
name: "therd"
nani:feras1
rows: 20
padding: 0
spacing: 2
GridLayout:
spacing: 10
cols:2
Label:
text: "Enter The First Value : "
font_size: "30sp"
TextInput:
id:first_Value
font_size: 40
multiline: True
CustButton:
text: "go back"
on_press:
app.root.current = "second"
root.manager.transition.direction = "right"
CustButton:
text: "Next"
on_press:
app.root.current = "four"
root.manager.transition.direction = "left"
<Fourwindow>:
id:lala
name: "four"
nani21:feras2
rows: 20
padding: 0
spacing: 2
GridLayout:
spacing: 10
cols:2
Label:
text: "Enter The second Value : "
font_size: "30sp"
TextInput:
id: second_Value
font_size: 40
multiline: True
CustButton:
text: "go back"
on_press:
app.root.current = "therd"
root.manager.transition.direction = "right"
CustButton:
text: "NEXT"
on_press:
app.root.current = "funfth"
root.manager.transition.direction = "left"
<FunfthWindow>:
id:CalcmGridLayout
name: "funfth"
result_1:label_id
rows: 20
padding: 0
spacing: 2
GridLayout:
spacing: 10
cols:2
CustButton:
text: "Result : "
font_size: "30sp"
on_press:CalcmGridLayout.calculate3(first_Value.text,second_Value.text)
Label:
id: label_id
font_size: 40
multiline: True
CustButton:
text: "go back"
on_press:
app.root.current = "four"
root.manager.transition.direction = "right"
CustButton:
text: "NEXT"
on_press:
app.root.current = "main"
root.manager.transition.direction = "left"
<Sixwindow>:
name: "six"
GridLayout:
cols: 1
spacing: 10
Label:
text: 'das Password ist falsch'
font_size: "40sp"
CustButton:
text: "nochmal"
on_press:
app.root.current = "main"
root.manager.transition.direction = "right"
When I click of 'Result' I get this Error NameError : first_Value is not defined
please help. I would really appreciate any advice.
Well, there's a lot of ways to do that, for the simplicity I'll do all operations in the MyMainApp class:
from kivy.properties import ObjectProperty
from Kivy.clock import Clock
...
class Therdwindow(Screen):
first_Value = ObjectProperty(None)
def getvalue(self):
return self.first_Value
class Fourwindow(Screen):
second_Value = ObjectProperty(None)
def getvalue(self):
return self.second_Value
class FunfthWindow(Screen):
label_id = ObjectProperty(None)
def getlabel(self):
return self.label_id
class WindowManager(ScreenManager):
pass
class MyMainApp(App):
def build(self):
with open('layout.kv', encoding='utf-8', errors='ignore') as f:
Builder.load_string(f.read())
# creating objects of screens
self.mainwindow = MainWindow()
self.secondwindow = SecondWindow()
self.therdwindow = Therdwindow()
self.fourwindow = Fourwindow()
self.funfthwindow = FunthWindow()
# creating object of screen manager
self.sm = WindowManager()
# getting all object properties
self.first_Value = self.therdwindow.getvalue()
self.second_Value = self.fourwindow.getvalue()
self.label_id = self.funfthwindow.getlabel()
# connecting object properties to widgets from kv file
# sometimes we need to delay that, because the app can't load so quickly, so let's make a method for it
Clock.schedule_once(self.getids)
# adding screens to screen manager
self.sm.add_widget(self.mainwindow)
self.sm.add_widget(self.secondwindow)
self.sm.add_widget(self.therdwindow)
self.sm.add_widget(self.fourwindow)
self.sm.add_widget(self.funfthwindow)
# in case you want screen manager be able to work you should return it
return self.sm
def getids(self):
self.first_Value = self.therdwindow.ids.first_Value
self.second_Value = self.fourwindow.ids.second_Value
self.label_id = self.funfthwindow.ids.label_id
# method that does what you want
def count(self):
self.label_id.text = str(int(self.first_Value.text) + int(self.second_Value.text))
if __name__ == "__main__":
MyMainApp().run()
And you need to edit your kv file, change all that
on_press:
app.root.current = "second"
to that:
on_press:
app.sm.current = "second"
And change this:
CustButton:
text: "Result : "
font_size: "30sp"
on_press:CalcmGridLayout.calculate3(first_Value.text,second_Value.text)
to this:
CustButton:
text: "Result : "
font_size: "30sp"
on_press: app.count
Also you need to add that lines in classes:
<Therdwindow>:
...
first_Value: first_Value.__self__
<Fourwindow>:
...
second_Value: second_Value.__self__
<FunfthWindow>:
...
label_id: label_id.__self__
And delete this:
WindowManager:
MainWindow:
SecondWindow:
Therdwindow:
Fourwindow:
FunfthWindow:
Sixwindow:
You are trying to use ids from one rule inside another rule, but ids are only available for the current rule. So, this line in your kv will not work:
on_press:CalcmGridLayout.calculate3(first_Value.text,second_Value.text)
A way to fix this, is to access the ids by first getting the Screen where that id is defined, and that is more easily done outside of kv. So, I would recommend replacing the above line with:
on_press:root.calculate3()
And then rewrite your calculate3() method slightly to access the other values:
class FunfthWindow(Screen):
def calculate3(self):
bibi = App.get_running_app().root.get_screen('therd').ids.first_Value.text
mimi = App.get_running_app().root.get_screen('four').ids.second_Value.text
kiki = str(float(bibi) + float(mimi))
if kiki:
try :
self.ids.label_id.text = str(eval(kiki))
except Exception:
self.ids.label_id.text = 'Error'

errors in results after launching program

this is my 1st time posting here and am so amazed how genius people here are.
i have written a python code using kivy and it works perfectly on my computer even on kivy launcher for android, but the problem is that the result on my phone is always ignoring the float numbers and show just the 1s one.
thanks in advance
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.config import Config
import time
Builder.load_string("""
<Main>:
GridLayout:
cols:2
Button:
text: "Engine Consumption"
on_press: root.manager.current = "test1"
Button:
text: "Apu Consumption"
on_press: root.manager.current = "test2"
<Test1>
GridLayout:
cols: 2
Label:
text: "Flight Hours: "
TextInput:
id: FH
multiline: False
Label:
text: "Flight Minutes:"
TextInput:
id: FM
multiline: False
Label:
text: "Added Quantity:"
TextInput:
id: AQ
multiline: False
Button:
text: "get result"
on_press: root.get_result()
Label:
id: result
Button:
text: "To Main Page"
on_release: root.manager.current = "main"
Button:
text: "Apu Consumption:"
on_release: root.manager.current = "test2"
<Test2>
GridLayout:
cols: 2
Label:
text: "Apu hours In Last Refill: "
TextInput:
id: FH
multiline: False
Label:
text: "Current Apu Hours:"
TextInput:
id: FM
multiline: False
Label:
text: "Added Quantity:"
TextInput:
id: AQ
multiline: False
Button:
text: "get result"
on_press: root.get_result()
Label:
id: result
Button:
text: "To main Page"
on_release: root.manager.current = "main"
Button:
text: "Engine Consumption"
on_release: root.manager.current = "test1"
""")
class Main(Screen):
pass
class Test1(Screen):
def get_result(self):
fh = self.ids.FH.text
fm = self.ids.FM.text
ad = self.ids.AQ.text
result = int(ad) / (int(fh) + int(fm) / 60)
self.ids.result.text = str(result)
self.ids.result.color = 1,0,1,1
class Test2(Screen):
def get_result(self):
fh = self.ids.FH.text
fm = self.ids.FM.text
ad = self.ids.AQ.text
result = int(ad) / (int(fm) - int(fh))
self.ids.result.text = "the result is: " + str(float(result))
class Test(App):
def build(self):
sm = ScreenManager()
sm.add_widget(Main(name = "main"))
sm.add_widget(Test1(name = "test1"))
sm.add_widget(Test2(name = "test2"))
return sm
Config.set("graphics", "height", "150")
if __name__ == "__main__":
Test().run()
on computer result is 0,4444
and on phone 0

(Python,Kivy) when i convert app to exe, popup doesn't work

Pop up and EXE issue
Hi guys I've got problem, whenever I open my Kivy Program via Python, a popup is actually showing up
But when I convert it via Pyinstaller, it gives me an error:
How can I possibly fix this?
Python Code:
import os
os.environ["KIVY_IMAGE"] = "pil, sdl2"
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.popup import Popup
Builder.load_file("kivy.kv")
class Rusure(Popup):
pass
class Menu(Screen):
def quit(self):
Rusure().open()
class Settings(Screen):
def bright (self,*args):
self.brightness.text = "Brightness: {}".format(str(int(args[1])))
class Play(Screen):
pass
class Sm(ScreenManager):
def __init__(self):
super().__init__()
self.add_widget(Menu(name="menu"))
self.add_widget(Settings(name="settings"))
self.add_widget(Play(name="play"))
class GoodApp(App):
def build(self):
return Sm()
GoodApp().run()
Kivy Code:
#: import rit kivy.uix.screenmanager.RiseInTransition
#: import st kivy.uix.screenmanager.SlideTransition
#: import fot kivy.uix.screenmanager.FallOutTransition
#: import Factory kivy.factory.Factory
<Menu>:
BoxLayout:
padding: 100,100,100,100
orientation: "vertical"
Label:
text: "Menu"
BoxLayout:
orientation: "horizontal"
Button:
text: "Play"
background_normal: "Image.jpg"
background_down: "ImageRel.jpg"
color: 1,1,1,1
on_press:
root.manager.transition = rit()
root.manager.current = "play"
Button:
text: "Settings"
background_color: 0.9,1,1,1
on_press:
root.manager.transition = st()
root.manager.current = "settings"
root.manager.transition.direction = "up"
Button:
text: "Quit"
background_color: 1,0.7,0,1
on_press:
root.quit()
<Settings>:
brightness: brght
BoxLayout:
padding: 100,100,100,100
orientation: "vertical"
Label:
text: "Settings"
GridLayout:
cols: 3
Label:
text: "Brightness"
Slider:
min: 0
max: 10
value: 5
on_value: root.bright(*args)
Label:
id: brght
text: "Change Value"
Button:
text: "Back"
on_press:
root.manager.transition = st()
root.manager.current = "menu"
root.manager.transition.direction = "down"
<Play>:
BoxLayout:
Label:
text: "Welcome to the game!"
Button:
text: "Back"
on_press:
root.manager.transition = fot()
root.manager.current = "menu"
<Rusure>:
title: "Are you sure?"
size_hint: 0.5,0.5
auto_dismiss: False
BoxLayout:
orientation: "vertical"
Label:
text: "Your progress will be lost"
GridLayout:
cols: 2
Button:
text: "Oh shit man!"
on_press: root.dismiss()
Button:
text: "Cancel"
on_press: root.dismiss()
I tried searching solution on the internet, but after 3 hours of searching I couldn't come up whith any answer.

How should I set a global variable in kivy Python?

My application is simple, the ScreenTwo is showing a form 'Parametreur' with various options to be entered.
What I'm trying to do is to set a 'save' button at the end of this form that will register into a list called 'resultat' all the options furfilled so far leaving '0' in the empty inputs.
The global variable 'resultat' would look like this ['blabla','15/06/2018','31/12/1999','6'].
File.py
from kivy.app import App
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager, Screen, WipeTransition
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.properties import ObjectProperty
from kivy.lang import Builder
Builder.load_file("HadrianRunningApp.kv")
class ScreenMenu(Screen):
pass
class ScreenOne(Screen):
pass
class ScreenTwo(Screen):
pass
class Parametreur(FloatLayout):
def build(self):
pass
def submit_input(self):
# Get the student name from the TextInputs
label_name = self.label_text_input.text
resultat.insert(0,label_name)
def submit_input2(self):
date_debut_name = self.date_debut_input.text
resultat.insert(1,date_debut_name)
def submit_input3(self):
date_fin_name = self.date_fin_input.text
resultat.insert(2,date_fin_name)
def submit_input4(self):
var_duree_estimation = self.duree_estimation_input.text
resultat.insert(3,var_duree_estimation)
def save_input(self):
print(resultat)
class Manager(ScreenManager):
screen_menu = ObjectProperty(None)
screen_one = ObjectProperty(None)
screen_two = ObjectProperty(None)
class ScreensApp(App):
def build(self):
m = Manager(transition=WipeTransition())
return m
if __name__=='__main__':
ScreensApp().run()
The Kivy file .kv:
#:kivy 1.9.0
<ScreenMenu>:
BoxLayout:
orientation:'vertical'
size: root.size
padding: "20dp"
Button:
text: "go to Screen 1"
on_press: root.manager.current = 'screen1'
Button:
text: "go to Screen 2"
on_press: root.manager.current = 'Parametreur'
<ScreenOne>:
BoxLayout:
spacing: 20
padding: 20
Button:
text: "go to Screen 2"
on_press: root.manager.current = 'Parametreur'
Button:
text: "go to Menu"
on_press: root.manager.current = 'screen0'
<ScreenTwo>:
Parametreur
<Parametreur>:
label_text_input: label_text
date_debut_input: date_debut
date_fin_input: date_fin
duree_estimation_input: duree_estimation
resultat: resultat
RelativeLayout:
orientation: 'vertical'
GridLayout:
size_hint: (1., 0.11)
pos_hint: {'right': 1, 'center_y': 0.91}
padding: 6
spacing: "4dp"
cols:2
rows:1
Button:
text: "go to Screen 1"
on_press: root.manager.current = 'screen1'
Button:
text: "go to Menu"
on_press: root.manager.current = 'screen0'
GridLayout:
size_hint: (1., 0.8)
pos_hint: {'right': 1, 'center_y': 0.45}
padding: "7dp"
spacing: 5
cols:2
rows:7
Button:
text: "Liste Actif"
Label:
text: " "
BoxLayout:
Label:
text: "Date du début"
BoxLayout:
orientation: "vertical"
Button:
text: "Submit"
on_press: root.submit_input2()
TextInput:
id: date_debut
text: 'dd/mm/YYYY'
BoxLayout:
Label:
text: "Date de fin"
BoxLayout:
orientation: "vertical"
Button:
text: "Submit"
on_press: root.submit_input3()
TextInput:
id: date_fin
text: 'dd/mm/YYYY'
Label:
text: "Pourcentage de séparation \n de la base (validation/test)"
Button:
text: "Open to Close"
Button:
text: "Close to Close"
Button:
text: "les 3 (6 en tout) frontières des VSs"
BoxLayout:
Label:
text: "Durée pour la réestimation \n des modèles (en jours)"
BoxLayout:
orientation: "vertical"
Button:
text: "Submit"
on_press: root.submit_input4()
TextInput:
id: duree_estimation
text: 'dd/mm/YYYY'
Label:
text: "variable selection NMF/Entropy"
Label:
text: "Kernel/damiers/buntcher/\n neurone/XGBoost/Gradient boosting"
# We create the widgets
BoxLayout:
spacing: "0dp"
Label:
id: label_text
text: "0"
font_size: "30dp"
BoxLayout:
orientation: "vertical"
spacing: "3dp"
Button:
text: "Add"
on_release: label_text.text = str(int(label_text.text) + 1)
Button:
text: "Subtract"
on_release: label_text.text = str(int(label_text.text) - 1)
Button:
text: "Submit"
size_hint_x: None
on_press: root.submit_input()
BoxLayout:
padding: "2dp"
spacing: "2dp"
size_hint: (1., 0.50)
pos_hint: {'right': 1, 'center_y': 0.09}
Button:
id: resultat
text:"Save"
size_hint_x: None
on_press: root.save_input()
<Manager>:
id: screen_manager
screen_menu: screen_menu
screen_one: screen_one
screen_two: screen_two
ScreenMenu:
id: screen_menu
name: 'screen0'
manager: screen_manager
ScreenOne:
id: screen_one
name: 'screen1'
manager: screen_manager
ScreenTwo:
id: screen_two
name: 'Parametreur'
manager: screen_manager
Where and How should I set my global variable 'resultat' (which is a list)?
I've solved my problem but I still don't understand how does it work. Setting a variable is Kivy doesn't need to set it in the Python code. But how do Kivy and Python know about its type? Right now, it works as a list without me defining it such as (I intended it to be a list anyway).
How could I set a variable as tuple for example?

Categories