Kivy Image Widget not Moving Right - python

If you look under the first GridLayout there is an Image widget. I'm trying to get it to move to the right side of the screen. Any help is appropriated. Here's the code. The id of the widget I need on the right hand side is id=image. I can't seem to move it at all.
I'm giving more details because stackoverflow want's it. I think the above is pretty detailed really stackoverflow, but your in charge so here is more text.
Thank you everyone.
from kivy.lang import Builder
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
Builder.load_string("""
<ClientScreen>:
GridLayout:
id: main_grid_layout
orientation: 'vertical'
cols: 1
Label:
id: name_label
text: '<NO MENU NAME>'
size_hint_y: None
size: self.texture_size
halign: 'left'
valign: 'center'
text_size: self.size
padding_x: 35
canvas.before:
Color:
rgb: .6, .6, .6
Rectangle:
pos: self.pos
size: self.size
Image:
id: image
pos_hint: {'right': 0.5}
ScrollView:
id: text_scroll_view
bar_width: 10
size: self.size
GridLayout:
id: text_grid_layout
orientation: 'vertical'
cols: 1
size_hint_y: None
height: self.minimum_height
ScrollView:
size: self.size
bar_width: 10
size_hint_y: 0.40
GridLayout:
id: action_grid_layout
# padding: [10, 10, 10, 10]
orientation: 'vertical'
cols: 1
size_hint_y: None
# row_default_height: 30
height: self.minimum_height
""")
class LoginScreen(Screen):
pass
class ClientScreen(Screen):
pass
class MyApp(App):
def build(self):
from kivy.core.window import Window
sm = ScreenManager()
sm.transition = NoTransition()
global CLIENT_SCREEN
LOGIN_SCREEN = LoginScreen(name='login')
CLIENT_SCREEN = ClientScreen(name='client')
sm.add_widget(LOGIN_SCREEN)
sm.add_widget(CLIENT_SCREEN)
sm.current = 'client'
Window.size = (300, 120)
self.title = 'xNemesis Client V0'
return sm
MyApp().run()

In the kv file, do the following. Please refer to the example below for details.
Replace GridLayout: with BoxLayout: because GridLayout with cols: 1 has the similar presentation as BoxLayout with orientation: 'vertical'.
Remove cols: 1
At Image:, add size_hint_x: 0.4
Replace pos_hint: {'right': 0.5} with pos_hint: {'right': 1}
Note
GridLayout does not has an attribute called orientation.
Example
main.py
from kivy.lang import Builder
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
Builder.load_string("""
<ClientScreen>:
BoxLayout:
id: main_grid_layout
orientation: 'vertical'
Label:
id: name_label
text: '<NO MENU NAME>'
size_hint_y: None
size: self.texture_size
halign: 'left'
valign: 'center'
text_size: self.size
padding_x: 35
canvas.before:
Color:
rgb: .6, .6, .6
Rectangle:
pos: self.pos
size: self.size
Image:
id: image
source: 'raspberrypi.png'
size_hint_x: 0.4
pos_hint: {'right': 1}
ScrollView:
id: text_scroll_view
bar_width: 10
size: self.size
GridLayout:
id: text_grid_layout
orientation: 'vertical'
cols: 1
size_hint_y: None
height: self.minimum_height
ScrollView:
size: self.size
bar_width: 10
size_hint_y: 0.40
GridLayout:
id: action_grid_layout
# padding: [10, 10, 10, 10]
orientation: 'vertical'
cols: 1
size_hint_y: None
# row_default_height: 30
height: self.minimum_height
""")
class LoginScreen(Screen):
pass
class ClientScreen(Screen):
pass
class MyApp(App):
def build(self):
from kivy.core.window import Window
sm = ScreenManager()
sm.transition = NoTransition()
global CLIENT_SCREEN
LOGIN_SCREEN = LoginScreen(name='login')
CLIENT_SCREEN = ClientScreen(name='client')
sm.add_widget(LOGIN_SCREEN)
sm.add_widget(CLIENT_SCREEN)
sm.current = 'client'
Window.size = (300, 120)
self.title = 'xNemesis Client V0'
return sm
MyApp().run()
Output

Related

Kivy self manager get screen - not working

recently I try to make that app with python and kivy. After tons of hours googling everything together i more or less have everything i want. Only one thing is missing:
I have several screens. At the beginning of the App I have a menu. After that i have lots of questions; all of them have the same text and buttons in the bottom of the screen. I managed to make that the following way:
<firstquestion>:
name: "firstquestion"
GridLayout:
cols: 1
size_hint_y: 1
GridLayout:
cols: 1
NeuLabelinBox:
text: "Here the Question"
GridLayout:
cols: 1
size_hint_y: 0.1
UnteresMenue:
Please notice "UnteresMenue" which refer another Class in the kv language. I think I somehow mixed 2 Screens together in one. The kv code of "UnteresMenue"
<UnteresMenue>:
name: "UnteresMenue"
#id: UnteresMenue
GridLayout:
cols: 1
NeuButton:
text: root.labeltext
Now for the python part:
class UnteresMenue(Screen):
labeltext = StringProperty("Answer")
That works fine. But now I want to change the text of the label in this "UnteresMenue", when I press a certain button at the beginning of the app (Start Questions). The label in the "Untermenue" should change to a certain text. So to do this:
text: "Exam"
self.manager.get_screen('UnteresMenue').labeltext = text
For all other classes this method works fine. But not for that certain class "UnteresMenue". Is it because it is implemented in the question and therefor kv does not recognize its properties?
For any small hints I would be more than grateful!
Here an "Mini" Example:
In the Main Menu you get to the "Exam" Section (Press Here in the example). While entering this Section (Prüfungsmodus) the property of the questions should change. "Press Here" again to get to the questions.
Normally it says in the Top of the questions "This should change" And THIS is supposed to change the label into "Zeit" while entering the Prüfungsmodus Screen (Class def on_enter, in the python file), but it doesn't...
Python File:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, TransitionBase
from kivy.properties import ObjectProperty, NumericProperty
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.core.text import LabelBase
from kivy.core.window import Window
from kivy.graphics import Color, InstructionGroup, Line, Rectangle
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.properties import ListProperty
from kivy.uix.textinput import TextInput
from kivy.uix.checkbox import CheckBox
from kivy.uix.widget import Widget
from kivy.config import Config
from kivy.properties import StringProperty
from kivy.clock import Clock
from functools import partial
from kivy.uix.image import Image
from kivy.garden.navigationdrawer import NavigationDrawer as ND
from kivy.uix.scatter import Scatter
import random
import time
Window.clearcolor = (0.2,0.2, 0.2,1)
Window.size = (480, 800)
#--------------------------------- Hauptmenü -------------------------------------------
class hauptmenue(Screen, Widget):
pass
class Pruefungsmodus(Screen):
labeltext2 = StringProperty("hi")
def on_enter(self):
text = "Zeit"
self.manager.get_screen('OberesMenue').labeltext = text #DAS Klappt nicht
def StartPruefung(self):
sm.current = "ersteFrage"
class UnteresMenue(Screen):
background_color_Kappa = ListProperty([0.2,0.2,0.2, 1])
class OberesMenue(Screen):
BildLabeltext = StringProperty("Bilder\Loesung_Bild_pressed.png")
BildLabeltext2 = StringProperty("Bilder\Loesung_Bild.png")
labeltext = StringProperty("This should change")
class ersteFrage(Screen):
pass
class WindowManager(ScreenManager):
pass
kv = Builder.load_file("my.kv")
sm = WindowManager()
sm.add_widget(hauptmenue(name="hauptmenue"))
sm.add_widget(Pruefungsmodus(name="Pruefungsmodus"))
sm.add_widget(OberesMenue(name="OberesMenue"))
sm.add_widget(UnteresMenue(name="UnteresMenue"))
sm.add_widget(ersteFrage(name="ersteFrage"))
sm.current = "hauptmenue"
class Vorbrereitung(App):
pruefung = ObjectProperty(None)
def build(self):
return sm
if __name__ == "__main__":
Vorbrereitung().run()
my.kv file :
#:include alleFragen.kv
<NeuLabel2#Label>:
halign: "center"
color:1,1,1,1 # <-----------
canvas.before:
Color:
rgba: 0.2,0.2, 0.2,1
Rectangle:
pos: self.pos
size: self.size
<NeuLabelinBox#Label>:
font_size: "22sp"
color:0,0,0,1 # <-----------
canvas.before:
Color:
rgba: 0.949019608, 0.949019608, 0.949019608, 1
Rectangle:
pos: self.pos
size: self.size
<NeuCheckBox#CheckBox>:
color: 0, 0, 0, 1
canvas.before:
Color:
rgba: 0.949019608, 0.949019608, 0.949019608, 1
Rectangle:
pos: self.pos
size: self.size
<NeuButton#Button>:
font_size: "22sp"
background_normal: ''
#background_normal: "background.png"
background_color: 0.92549,0.92549,0.92549, 1
color:0,0,0,1 # <-----------
canvas.before:
Color:
rgba: 0.2,0.2, 0.2,1
Rectangle:
pos: self.pos
size: self.size
<NeuButtonKappa#Button>:
font_size: "40sp"
background_normal: ''
background_color: 0.2,0.2,0.2, 1
color:1,1,1,1 # <-----------
canvas.before:
Color:
rgba: 0.2,0.2, 0.2,1
Rectangle:
pos: self.pos
size: self.size
#---------------------------------- Hauptmenue ---------------------------------------------------------------------------------
<hauptmenue>:
name: "Hauptmenue"
GridLayout:
cols:1
#spacing: 20
GridLayout:
cols:1
padding: 20
size_hint_y: 0.2
NeuLabel2:
size_hint_x: 0.6
text: ""
font_size: (root.width**2 + root.height**2) / 13**4
halign: 'center'
valign: 'middle'
GridLayout:
cols:2
padding: 30
spacing: 20
size_hint_y: 0.5
BoxLayout:
NeuButton:
text: ''
BoxLayout:
NeuButton:
text: 'PRESS HERE'
on_release:
app.root.current = "Pruefungsmodus"
BoxLayout:
NeuButton:
text: ""
halign: 'center'
valign: 'middle'
BoxLayout:
NeuButton:
text: ""
BoxLayout:
padding: 10
size_hint_y: 0.15
NeuButton:
text: ''
#---------------------------------- Pruefungsmodus ---------------------------------------------------------------------------------
<Pruefungsmodus>:
name: "Pruefungsmodus"
#background_color: 1, 1, 1, 1
id: pruefungsmudos
GridLayout:
cols:1
#spacing: 20
GridLayout:
cols:3
padding: 20
size_hint_y: 0.2
Image:
size_hint_x: 0.2
source:"Bilder\Logo.png"
NeuLabel2:
size_hint_x: 0.6
text: 'Pr\u00FCfungs\nmodus'
font_size: (root.width**2 + root.height**2) / 12**4
halign: 'center'
valign: 'middle'
Image:
size_hint_x: 0.2
source:"Bilder\Logo2.png"
GridLayout:
cols:1
padding: 30
spacing: 10
size_hint_y: 0.4
NeuLabelinBox:
id: text2
text: root.labeltext2
color: 0,0,0,1 # <-----------
canvas.before:
Color:
rgba: 0.949019608, 0.949019608, 0.949019608, 1
Rectangle:
pos: self.pos
size: self.size
GridLayout:
cols:2
padding: 30
size_hint_y: 0.2
BoxLayout:
padding: 30
spacing: 10
NeuButton:
size_hint_x: 0.5
text: "Press Here"
on_release:
app.root.current = "ersteFrage"
NeuButton:
size_hint_x: 0.5
text: ""
on_release:
app.root.current = "hauptmenue"
#---------------------------------- Erste Frage ---------------------------------------------------------------------------------
<ersteFrage>:
name: "ersteFrage"
GridLayout:
cols: 1
size_hint_y: 1
GridLayout:
cols: 1
size_hint_y: 0.08
OberesMenue:
GridLayout:
cols: 1
size_hint_y: 0.1
NeuLabel2:
font_size: "18sp"
text: "Wie lautet......."
GridLayout:
cols: 1
size_hint_y: 0.7
padding: 20
background_color: 0.92549,0.92549,0.92549, 1
rows: 2
BoxLayout:
orientation:'horizontal'
NeuLabelinBox:
text: "h+u=ZU"
NeuCheckBox:
NeuLabelinBox:
text: "h+u=ZU"
NeuCheckBox:
BoxLayout:
orientation:'horizontal'
NeuLabelinBox:
text: "h+u=ZU"
NeuCheckBox:
NeuLabelinBox:
text: "h+u=ZU"
NeuCheckBox:
GridLayout:
cols: 1
size_hint_y: 0.1
UnteresMenue:
and the outsourced Top and Button Menu:
alleFragen.kv
<OberesMenue>:
id: OberesMenueee
name: "OberesMenue"
GridLayout:
cols:1
GridLayout:
#size_hint_y: 0.05
spacing: 20
cols: 3
NeuButton:
text: ""
size_hint_x: 0.16
background_color: 1,1,1, 1
background_down: root.BildLabeltext
background_normal: root.BildLabeltext2
color:1,1,1,1 # <-----------
canvas.before:
Color:
rgba: 0.2,0.2, 0.2,1
Rectangle:
pos: self.pos
size: self.size
NeuLabel2:
size_hint_x: 0.8
text: root.labeltext
NeuButtonKappa:
size_hint_x: 0.1
text: "MENU"
background_down: "Bilder\HintergurndFarbe_app.png"
on_release:
app.root.current = "hauptmenue"
<UnteresMenue>:
name: "UnteresMenue"
id: UnteresMenuee
GridLayout:
cols: 5
spacing: 20
padding: 20
NeuButton:
text: "<=="
NeuButton:
text: "<"
NeuButtonKappa:
text: 'k'
#background_color: root.background_color_Kappa
background_down: "Bilder\HintergurndFarbe_app.png"
NeuButton:
text: ">"
NeuButton:
text: "==>"
The problem is that you have two different instances of the OberesMenue Screen.
There is one created in your python:
sm.add_widget(OberesMenue(name="OberesMenue"))
And you have another created in your kv:
<ersteFrage>:
name: "ersteFrage"
GridLayout:
cols: 1
size_hint_y: 1
GridLayout:
cols: 1
size_hint_y: 0.08
OberesMenue:
In order to acces the second instance of OberesMenue, you can add an id:
<ersteFrage>:
name: "ersteFrage"
GridLayout:
cols: 1
size_hint_y: 1
GridLayout:
cols: 1
size_hint_y: 0.08
OberesMenue:
id: in_ersteFrage
Then in your on_enter() method of Pruefungsmodus, you can change that instance by using:
def on_enter(self):
text = "Zeit"
self.manager.get_screen('ersteFrage').ids.in_ersteFrage.labeltext = text #DAS Klappt nicht
Note that this changes the Label in the second instance of OberesMenue, but has no effect on the first. Your original code was changing the first instance, but not the second. And you can't put the same instance in both places at the same time, since a Widget can have only one parent at a time.
So, if those instances are intended to be identical, then you need to change the Label in both.
If you really want to share the same instance, you can keep your own record of where it is and use remove_widget() and add_widget() to move it from one parent to another.

Python/Kivy : Can i call function on double click on Label

Can someone tell me how to call function def demo() on double click of Label Item1,Item2 ?
test.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen
def convert_data(data):
l = []
for item in data:
for key, value in item.items():
l.append({'text': key, 'value': value})
return l
class test():
def demo(self):
print('Demo')
class Invoice(Screen):
def abc(self):
arr = ({'Item1': ''},{'Item2': 1000})
self.rv.data = convert_data(arr)
class MyApp(App):
def build(self):
return Builder.load_file('test.kv')
if __name__ == '__main__':
MyApp().run()
test.kv
<Button#Button>:
font_size: 15
size_hint_y:None
height: 30
<Label#Label>:
font_size: 15
size_hint_y:None
height: 30
<Item#GridLayout>:
cols: 2
text: ""
value: 0
padding : 5, 0
spacing: 10, 0
Label:
size_hint_x: .35
text: root.text
halign: 'left'
valign: 'middle'
canvas.before:
Color:
rgb: .6, .6, .6
Rectangle:
pos: self.pos
size: self.size
Label:
size_hint_x: .15
text: str(root.value)
halign: 'right'
valign: 'middle'
canvas.before:
Color:
rgb: .6, .6, .6
Rectangle:
pos: self.pos
size: self.size
Invoice:
rv: rv
BoxLayout:
orientation: "vertical"
padding : 15, 15
BoxLayout:
orientation: "vertical"
padding : 5, 5
size_hint: .6, None
pos_hint: {'x': .18,}
BoxLayout:
orientation: "horizontal"
padding : 5, 5
spacing: 10, 10
size: 800, 40
size_hint: 1, None
Button:
text: "Show"
size_hint_x: .05
spacing_x: 30
on_press:root.abc()
BoxLayout:
orientation: "horizontal"
size_hint: 1, 1
BoxLayout:
orientation: "vertical"
size_hint: .5, 1
padding : 0, 15
spacing: 10, 10
size: 500, 30
BoxLayout:
RecycleView:
id: rv
viewclass: 'Item'
RecycleBoxLayout:
default_size: None, dp(30)
default_size_hint: 1, None
size_hint_y: None
height: self.minimum_height
orientation: 'vertical'
you can create a subclass of your Label and attach the double_tap event to it
class MyLabel(Label):
def on_touch_down(self, touch):
if touch.is_double_tap:
demo()

KIVY: adding buttons in a sub layout when a button is pressed (on_release)

The screen has multiple layouts all child of a box layout. I am trying to add a button in the last sub layout (grid-layout) when a button in other layout is clicked. The code does not crash but nothing happens too. I want to add buttons to the layout with id drinkslayout when a button is clicked eg pepsi,sprite
Python:
class MainScreen(Screen):
pass
class AnotherScreen(Screen):
pass
class ScreenManagement(ScreenManager):
pass
class Stacks(StackLayout):
pass
present=Builder.load_file('hellokivy2.kv') #Specifying location of kv file
class MainApp(App):
def build(self):
return present
def drinksSelect(self,value): #creating a button by referring the id of the layout in which to create button
print(value)
yolo = AnotherScreen()
yolo2 = yolo.ids.newButtonLayout
button = Button(text="value",size_hint= (0.2,0.4))
yolo2.add_widget(button)
if __name__ == "__main__":
MainApp().run()
KV
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManagement:
transition: FadeTransition()
MainScreen:
AnotherScreen:
<MainScreen>:
name: "main"
canvas.before:
Color:
rgba: 1, 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
orientation: 'vertical'
size:root.size
Button:
text: "Slideshow"
GridLayout:
cols: 2
Button:
text: "Burger"
Button:
text: "Drinks"
on_release: app.root.current = "other"
Button:
text: "Fries"
Button:
text: "Others"
Label:
text: "Your Cart"
font_size: 40
color:(0,0,0,1)
size_hint_y: None
canvas.before:
Color:
rgba:150,10,200,0.5
Rectangle:
pos: self.pos
size: self.size
<AnotherScreen>:
name: "other"
canvas.before:
Color:
rgba: 1, 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
size:root.size
orientation: 'vertical'
GridLayout:
size_hint_y:0.1
orientation: 'vertical'
rows: 1
Button:
color: 1,1,1,1
font_size: 25
size_hint_y:0.1
text: "Back Home"
on_release: app.root.current = "main"
Label:
text: "Project BAM"
canvas.before:
Color:
rgba: 0,0,0,1
Rectangle:
pos: self.pos
size: self.size
Button:
color: 1,1,1,1
font_size: 25
size_hint_y:0.1
text: "Profile"
on_release: app.root.current = "main"
Button:
color: 1,1,1,1
font_size: 25
size_hint_y:0.9
text: "Whats New"
on_release: app.root.current = "main"
GridLayout:
rows: 2
Button:
text: "Pepsi"
on_release: app.drinksSelect(1)
Button:
text: "7up"
on_release: app.drinksSelect(2)
Button:
text: "Fanta"
on_release: app.drinksSelect(3)
Button:
text: "Mountain Dew"
on_release: app.drinksSelect(4)
Button:
text: "Diet Pepsi"
on_release: app.drinksSelect(5)
Button:
text: "Sprite"
on_release: app.drinksSelect(6)
GridLayout:
id: drinksLayout
size_hint_y: 0.3
orientation: 'horizontal'
rows: 1
Button:
id: label1
size_hint: 0.2,0.4
background_normal:'1.jpg'
text: 'B1'
Button:
id: label2
size_hint: 0.2,0.4
background_normal:'1.jpg'
text: 'B1'
Button:
id: label3
size_hint: 0.2,0.4
background_normal:'1.jpg'
text: 'B1'
Ok the problem was your drinksSelect method, also you need to keep the value of your screens
Try this:
py:
from kivy.app import App
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.lang.builder import Builder
from kivy.uix.stacklayout import StackLayout
from kivy.uix.button import Button
from kivy.properties import ObjectProperty
class MainScreen(Screen):
pass
class AnotherScreen(Screen):
dl = ObjectProperty()
l = 0
limit = 3 # Fix your limit here 3 is an example
class ScreenManagement(ScreenManager):
m_s = ObjectProperty() # Here I will keep the value of the MainScreen
a_s = ObjectProperty() # Here the AnotherScreen so I can use them later
class Stacks(StackLayout):
pass
present = Builder.load_file('hellokivy2.kv') # Specifying location of kv file
class MainApp(App):
def build(self):
return present
def drinksSelect(self,value): # creating a button by referring the id of the layout in which to create button
print(value)
if self.root.a_s.l < self.root.a_s.limit: # You know what I mean
button = Button(text=str(value), size_hint= (0.2,0.4))
self.root.a_s.ids['place_remaining'].add_widget(button)
self.root.a_s.l += 1
if __name__ == "__main__":
MainApp().run()
kv:
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
ScreenManagement:
m_s: m_s # so I can use it in the .py file
a_s:a_s # same here
transition: FadeTransition()
MainScreen:
id: m_s
AnotherScreen:
id: a_s
<MainScreen>:
name: "main"
canvas.before:
Color:
rgba: 1, 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
orientation: 'vertical'
size:root.size
Button:
text: "Slideshow"
GridLayout:
cols: 2
Button:
text: "Burger"
Button:
text: "Drinks"
on_release: app.root.current = "other"
Button:
text: "Fries"
Button:
text: "Others"
Label:
text: "Your Cart"
font_size: 40
color:(0,0,0,1)
size_hint_y: None
canvas.before:
Color:
rgba:150,10,200,0.5
Rectangle:
pos: self.pos
size: self.size
<AnotherScreen>:
name: "other"
dl : drinksLayout
canvas.before:
Color:
rgba: 1, 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
size:root.size
orientation: 'vertical'
GridLayout:
size_hint_y:0.1
orientation: 'vertical'
rows: 1
Button:
color: 1,1,1,1
font_size: 25
size_hint_y:0.1
text: "Back Home"
on_release: app.root.current = "main"
Label:
text: "Project BAM"
canvas.before:
Color:
rgba: 0,0,0,1
Rectangle:
pos: self.pos
size: self.size
Button:
color: 1,1,1,1
font_size: 25
size_hint_y:0.1
text: "Profile"
on_release: app.root.current = "main"
Button:
color: 1,1,1,1
font_size: 25
size_hint_y:0.9
text: "Whats New"
on_release: app.root.current = "main"
GridLayout:
rows: 2
Button:
text: "Pepsi"
on_release: app.drinksSelect(1)
Button:
text: "7up"
on_release: app.drinksSelect(2)
Button:
text: "Fanta"
on_release: app.drinksSelect(3)
Button:
text: "Mountain Dew"
on_release: app.drinksSelect(4)
Button:
text: "Diet Pepsi"
on_release: app.drinksSelect(5)
Button:
text: "Sprite"
on_release: app.drinksSelect(6)
GridLayout:
id: drinksLayout
size_hint_y: 0.3
orientation: 'horizontal'
rows: 1
GridLayout:
id: place_remaining
rows: 1
size_hint_x: 80
Button:
id: label1
width: 40 # Set the size_hint_x to None and then set the width if you want a fixed dimension
size_hint: None,0.4
background_normal:'1.jpg'
text: 'B1'
Button:
id: label2
width: 40
size_hint: None,0.4
background_normal:'1.jpg'
text: 'B1'
Button:
id: label3
width: 40
size_hint: None,0.4
background_normal:'1.jpg'
text: 'B1'
Outputs:
I hope that the code is a bit clear now

Python - Kivy - Screen Manager - Changing a Button Text with Text Input issue

I'm currently working with Kivy for GUI Design I was looking for a way to change a button text with a TextInput from another screen.
My Screen 1 has a button that will work as a "label", there I have another button to go to screen 2.
Screen 2 is a Keypad with a Textinput on it, there I put the numbers that I want to set in the button "label" from screen 1.
With a button called "ENTER" I want to go back to the screen 1 and update the new text in the button "label". But I can't figure out how to do it properly.
Here is a little piece of the project code main.py :
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.graphics import Line
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
class Main_Screen(Screen):
pass
class Input_Number_Inch_Screen(Screen):
pass
class Input_Screen(Screen):
pass
class Screen_Management(ScreenManager):
pass
presentation = Builder.load_file("screen3.kv")
class Screen3App(App):
def build(self):
return presentation
Screen3App().run()
the screen3.kv file:
Screen_Management:
id: screen_management
transition: FadeTransition()
Main_Screen:
id: main_screen
name: "main_screen_name"
manager: screen_management
Input_Screen:
id: tire_setup_screen_id
name: "tire_setup_screen_name"
manager: screen_management
Input_Number_Inch_Screen:
name: "inch_screen"
manager: screen_management
<Main_Screen>:
canvas:
Color:
rgb: [.30, .30, .30]
Rectangle:
pos: self.pos
size: self.size
Button:
background_color: .52, .52, .52, 1
bold: 1
color: .0078,.67,.69,1
size_hint: .2, 1
pos_hint: {"x": 0, "center_y": .5}
on_release: app.root.current = "tire_setup_screen_name"
text: " INPUTS "
font_size: 30
# Screen 1: Input Screen
<Input_Screen>:
canvas:
Color:
rgb: [.30, .30, .30]
Rectangle:
pos: self.pos
size: self.size
GridLayout:
cols: 2
pos: (160,150)
size_hint: (.8, .8)
Button:
background_color: .52, .52, .52, 1
bold: 1
color: .0078,.67,.69,1
font_size: 30
text: "INPUT\n(Inch)"
size_hint_x: None
width: 150
on_release: app.root.current = "inch_screen"
# This button will go to the screen2
Button:
id: inch_input
background_color: .52, .52, .52, 1
bold: 1
color: .0078,.67,.69,1
font_size: 100
text: "THIS IS THE TEXT THAT I WANT TO UPDATE"
# Screen 2: Input Inch Screen Data
<Input_Number_Inch_Screen>:
canvas:
Color:
rgb: [.30, .30, .30]
Rectangle:
pos: self.pos
size: self.size
GridLayout:
orientation: 'vertical'
display: entry
rows: 6
padding: 10
spacing: 10
# This is the TextInput
BoxLayout:
TextInput:
id: entry
font_size: 75
multiline: False
# This will be the button that would go back to the screen and update
# the button text with the new text entered in the TextInput
BoxLayout:
spacing: 10
Button:
background_color: .52, .52, .52, 1
bold: 1
color: .0078,.67,.69,1
font_size: 40
text:"ENTER"
on_release: app.root.current = "tire_setup_screen_name"
on_press: app.root.inch_input.text = entry.text
Any comment to help it would be great, thanks for your time.
Please replace the following in screen3.kv:
on_press: app.root.inch_input.text = entry.text
with:
on_press: root.manager.ids.tire_setup_screen_id.ids.inch_input.text = entry.text
Example
main.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
from kivy.uix.label import Label
from kivy.uix.widget import Widget
from kivy.graphics import Line
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
class Main_Screen(Screen):
pass
class Input_Number_Inch_Screen(Screen):
pass
class Input_Screen(Screen):
pass
class Screen_Management(ScreenManager):
pass
presentation = Builder.load_file("screen3.kv")
class Screen3App(App):
def build(self):
return presentation
if __name__ == "__main__":
Screen3App().run()
screen3.kv
#:kivy 1.10.0
#:import FadeTransition kivy.uix.screenmanager.FadeTransition
Screen_Management:
id: screen_management
transition: FadeTransition()
Main_Screen:
id: main_screen
name: "main_screen_name"
manager: screen_management
Input_Screen:
id: tire_setup_screen_id
name: "tire_setup_screen_name"
manager: screen_management
Input_Number_Inch_Screen:
name: "inch_screen"
manager: screen_management
<Main_Screen>:
canvas:
Color:
rgb: [.30, .30, .30]
Rectangle:
pos: self.pos
size: self.size
Button:
background_color: .52, .52, .52, 1
bold: 1
color: .0078,.67,.69,1
size_hint: .2, 1
pos_hint: {"x": 0, "center_y": .5}
on_release: root.manager.current = "tire_setup_screen_name"
text: " INPUTS "
font_size: 30
# Screen 1: Input Screen
<Input_Screen>:
canvas:
Color:
rgb: [.30, .30, .30]
Rectangle:
pos: self.pos
size: self.size
GridLayout:
cols: 2
pos: (160,150)
size_hint: (.8, .8)
Button:
background_color: .52, .52, .52, 1
bold: 1
color: .0078,.67,.69,1
font_size: 30
text: "INPUT\n(Inch)"
size_hint_x: None
width: 150
on_release: root.manager.current = "inch_screen"
# This button will go to the screen2
Button:
id: inch_input
background_color: .52, .52, .52, 1
bold: 1
color: .0078,.67,.69,1
font_size: 100
text: "THIS IS THE TEXT THAT I WANT TO UPDATE"
# Screen 2: Input Inch Screen Data
<Input_Number_Inch_Screen>:
canvas:
Color:
rgb: [.30, .30, .30]
Rectangle:
pos: self.pos
size: self.size
GridLayout:
orientation: 'vertical'
display: entry
rows: 6
padding: 10
spacing: 10
# This is the TextInput
BoxLayout:
TextInput:
id: entry
font_size: 75
multiline: False
# This will be the button that would go back to the screen and update
# the button text with the new text entered in the TextInput
BoxLayout:
spacing: 10
Button:
background_color: .52, .52, .52, 1
bold: 1
color: .0078,.67,.69,1
font_size: 40
text:"ENTER"
on_release: root.manager.current = "tire_setup_screen_name"
on_press: root.manager.ids.tire_setup_screen_id.ids.inch_input.text = entry.text
Outpu

Positioning multiple widgets in a BoxLayout in Kivy.

I have two widgets in a horizontal boxlayout. Afaik they will both stretch to fill half of the boxlayout. How do I go about resizing the widgets and repositioning within the boxlayout? Is it worth putting each widget in its own boxlayout so that it isn't affected by the widget beside it? Here is my code so far:
from kivy.app import App
from kivy.uix.label import Label
from kivy.lang import Builder
from kivy.properties import ListProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
class ShoppingListScreen(Screen):
pass
class theScreenManager(ScreenManager):
pass
root_widget = Builder.load_string('''
#:import FadeTransition kivy.uix.screenmanager.FadeTransition
theScreenManager:
ShoppingListScreen:
<ShoppingListScreen>:
BoxLayout:
orientation: 'vertical'
BoxLayout:
orientation: 'horizontal'
size_hint: 1, .3
Button:
text: '<'
size_hint: .1, 1
font_size: 75
background_normal: ""
background_color: 0.18, .5, .92, 1
on_release: app.root.current = 'main'
Label:
text: 'Login'
halign: 'left'
font_size: 50
canvas.before:
Color:
rgb: 0.18, .5, .92
Rectangle:
pos: self.pos
size: self.size
Widget:
size_hint: .1, 1
canvas.before:
Color:
rgb: 0.18, .5, .92
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
orientation: 'vertical'
canvas.before:
Color:
rgb: 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
BoxLayout:
orientation: 'horizontal'
padding_left: 250
Label:
text: 'Username:'
font_size: 25
color: 0.18, .5, .92, 1
size_hint: .2, .5
pos_hint: {'top': 1}
TextInput:
size_hint: .3, .1
Button:
text: 'Register'
font_size: 35
size_hint: 1, .3
background_normal: ""
background_color: 0.18, .5, .92, 1
#on_release: app.root.current = 'main'
Label:
text: 'Support'
color: 0.18, .5, .92, 1
halign: 'left'
font_size: 25
size_hint: 1, .3
canvas.before:
Color:
rgb: 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
''')
class temprecipeapp(App):
def build(self):
return root_widget
if __name__ == "__main__":
temprecipeapp().run()
If you run this file you will see the username and textinput box are in skewered positions. I am trying to center them and put them both beside each other. How would I go about doing so?
Maybe this will help:
I just import GridLayout and change part with Label:Usename and TextInput from BoxLayout to GridLayout.
from kivy.app import App
from kivy.uix.label import Label
from kivy.lang import Builder
from kivy.properties import ListProperty
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.dropdown import DropDown
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.screenmanager import ScreenManager, Screen, FadeTransition
class ShoppingListScreen(Screen):
pass
class theScreenManager(ScreenManager):
pass
root_widget = Builder.load_string('''
#:import FadeTransition kivy.uix.screenmanager.FadeTransition
theScreenManager:
ShoppingListScreen:
<ShoppingListScreen>:
BoxLayout:
orientation: 'vertical'
BoxLayout:
orientation: 'horizontal'
size_hint: 1, .3
Button:
text: '<'
size_hint: .1, 1
font_size: 75
background_normal: ""
background_color: 0.18, .5, .92, 1
on_release: app.root.current = 'main'
Label:
text: 'Login'
halign: 'left'
font_size: 50
canvas.before:
Color:
rgb: 0.18, .5, .92
Rectangle:
pos: self.pos
size: self.size
Widget:
size_hint: .1, 1
canvas.before:
Color:
rgb: 0.18, .5, .92
Rectangle:
pos: self.pos
size: self.size
GridLayout:
cols: 2
rows: 1
size_hint: 1, .1
canvas.before:
Color:
rgb: 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
Label:
text: 'Username:'
font_size: 25
size_hint: .3, 1
color: 0.18, .5, .92, 1
pos_hint: {'center': 1}
TextInput:
size_hint: .7, 1
font_size: 25
Button:
text: 'Register'
font_size: 35
size_hint: 1, .3
background_normal: ""
background_color: 0.18, .5, .92, 1
#on_release: app.root.current = 'main'
Label:
text: 'Support'
color: 0.18, .5, .92, 1
halign: 'left'
font_size: 25
size_hint: 1, .3
canvas.before:
Color:
rgb: 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
''')
class temprecipeapp(App):
def build(self):
return root_widget
if __name__ == "__main__":
temprecipeapp().run()

Categories