Handling keyboard input in regard to its source in Kivy - python

I am currently experimenting a bit with Kivy and added a TextInput to my GUI.
I now want to keep features such as deleting the last character when pressing backspace and so on but also want Kivy to execute a method when a new letter is entered to my textInput.
How would I do that? I tried the following code which works in terms of calling said function but doesn't keep the backspace and delete functionality.
Code:
KV
#:kivy 1.9.0
<PageLayout>:
GridLayout:
searchterm: searchterm
cols: 2
rows: 2
spacing: 5
padding: 5
Button:
text: "maybe a listview of latest traces here"
size_hint: .2,.5
BoxLayout:
orientation:'vertical'
TextInput:
id: searchterm
multiLine: False
hint_text: "Search for specific processes here"
keyboard_on_key_down: root.on_search
Button:
text: "room for imagination"
size_hint: 0.2, .5
Button:
text: "out of ideas"
BoxLayout:
orientation: "vertical"
Button:
text: "this is a new page for additional information"
Label:
halign: 'center'
valign: 'center'
size_hint: None, .025
width: self.parent.width
text: "About:"
color: 0,0,0,1 #textcolor
canvas.before:
Color:
rgba: 0.5,0.5,0.5,1
Rectangle:
pos: self.pos
size: self.size
Label:
halign: 'center'
valign: 'center'
size_hint: None, .025
width: self.parent.width
text: "This tool was written by me"
color: 0,0,0,1 #textcolor
canvas.before:
Color:
rgba: 0.5,0.5,0.5,1
Rectangle:
pos: self.pos
size: self.size
Label:
halign: 'center'
valign: 'center'
size_hint: None, .025
width: self.parent.width
text: "It is only meant for evaluation and testing purposes."
color: 0,0,0,1 #textcolor
canvas.before:
Color:
rgba: 0.5,0.5,0.5,1
Rectangle:
pos: self.pos
size: self.size
Python
import kivy
from kivy.app import App
from kivy.uix.pagelayout import PageLayout
from kivy.config import Config
from kivy.properties import ObjectProperty
from kivy.uix.widget import Widget
#WINDOW ATTRIBUTES
Config.set('graphics','resizable','1')
Config.set('graphics','width','1000')
Config.set('graphics','height','800')
class Visualizer(PageLayout):
searchterm = ObjectProperty(None)
def on_search(self,*args,**kwargs):
print("Test")
print(*args)
print(**kwargs)
class VisualizerApp(App):
def build(self):
self.title = "MQTT IDS Visualizer. Author: Daniel"
return Visualizer()
GLAPP = VisualizerApp()
GLAPP.run()

You need to call the super for keyboard_on_key_down. I believe the easiest way to do that is to define a subclass of TextInput like so:
class MyTextInput(TextInput):
def keyboard_on_key_down(self, window, keycode, text, modifiers):
App.get_running_app().root.on_search( window, keycode, text, modifiers) # calls your `on_search()` method
return super(MyTextInput, self).keyboard_on_key_down(window, keycode, text, modifiers)
Then replace TextInput with MyTextInput in your kv file.

Related

Kivy how to have one class and method for multiple dropdowns?

I am making an app where I will have many different labels for dropdown lists. These dropdown lists will be activated by a button that will show the dropdown.
My problem is that I do not know how to have multiple dropdowns using only one class in my python code. Currently, I have to make multiple classes and multiple methods to activate different dropdown menus and to have those dropdown menus come from different classes.
Trying to modify the show_dropDown() didn't work since i couldn't get the labels to show and the button to link the dropdown menu from.
main.py
from kivy.app import App
from kivy.core.window import Window
from kivy.lang import Builder
from kivy.uix.behaviors import ButtonBehavior
from kivy.uix.dropdown import DropDown
from kivy.uix.gridlayout import GridLayout
from kivy.uix.screenmanager import Screen
Window.size = (286.110236, 570.33070866)
class HomeScreen(Screen):
pass
class CustomDropDown(DropDown):
pass
class CustomDropDown2(DropDown):
pass
class ButtonGrid(ButtonBehavior, GridLayout):
def show_dropDown(self):
dropdown = CustomDropDown()
dropdown.open(self)
def show_dropDown2(self):
dropdown = CustomDropDown2()
dropdown.open(self)
GUI = Builder.load_file("main.kv")
class MainApp(App):
def build(self):
return GUI
MainApp().run()
kv-file:
#:import utils kivy.utils
#:import Factory kivy.factory.Factory
<ButtonGrid>:
<CustomDropdown>:
id: dropdown
Label:
id: label1
text: 'Dropdown 1'
size_hint_y: None
height: 400
background_color: (0, 1, 0, .9)
canvas.before:
Color:
rgba: self.background_color
Rectangle:
size: self.size
pos: self.pos
<CustomDropdown2>:
id: dropdown2
Label:
id: label2
text: 'Dropdown 2'
size_hint_y: None
height: 400
background_color: (1, 0, 1, .9)
canvas.before:
Color:
rgba: self.background_color
Rectangle:
size: self.size
pos: self.pos
<HomeScreen>:
FloatLayout:
canvas:
Color:
rgb: utils.get_color_from_hex("#FFFFFF")
Rectangle:
size: self.size
pos: self.pos
GridLayout:
rows: 8
cols: 1
spacing: 10
size_hint_y: None
height: 1200
width: self.minimum_width
ButtonGrid:
id: button1
cols:1
rows:1
size_hint: 1, None
height: 185.71
on_press:
self.show_dropDown()
background_color: (1,0,0,1)
canvas.before:
Color:
rgba: self.background_color
Rectangle:
size: self.size
pos: self.pos
Label:
text: "Button 1"
ButtonGrid:
id: button2
cols:1
rows:1
size_hint: 1, None
height: 185.71
on_press:
self.show_dropDown2()
background_color: (0,0,1,1)
canvas.before:
Color:
rgba: self.background_color
Rectangle:
size: self.size
pos: self.pos
Label:
text: "Button 2"
You can add property (or properties) to your CustomDropDown class (or may be to ButtonGrid) and link it to associated widget as follows:
First in .py
class CustomDropDown(DropDown):
text = StringProperty("")
Then in .kv
<CustomDropdown>:
id: dropdown
Label:
id: label1
text: root.text
size_hint_y: None
...
Now call them as you need with some added args/kwargs,
...
GridLayout:
rows: 8
cols: 1
spacing: 10
size_hint_y: None
height: 1200
width: self.minimum_width
ButtonGrid:
id: button1
cols:1
rows:1
size_hint: 1, None
height: 185.71
on_press:
self.show_dropDown('Dropdown 1')
background_color: (1,0,0,1)
canvas.before:
Color:
rgba: self.background_color
Rectangle:
size: self.size
pos: self.pos
Label:
text: "Button 1"
ButtonGrid:
id: button2
cols:1
rows:1
size_hint: 1, None
height: 185.71
on_press:
self.show_dropDown(value='Dropdown 2')
background_color: (0,0,1,1)
And handle those calls in one go,
class ButtonGrid(ButtonBehavior, GridLayout):
def show_dropDown(self, value = ""):
dropdown = CustomDropDown(text = value)
dropdown.open(self)

Kivy - Get InputText From Other Screen

I am trying to type in text from one screen. Press a button and move to another screen and have that text be shown in a label. I've seen a few questions that are similar to mine, but have not been able to figure out how to use the posted solutions and have been stuck for hours (Link One, Link Two, Link Three). I believe that I need to use the __init__ method somewhere because this is an instance? I tried using the first link, but the label ends up blank (the code does run). Any Advice?
main.py
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.app import App
from kivy.lang.builder import Builder
class SecondWindow(Screen):
def get_unique_text(self):
x = self.manager.get_screen("first")
y = x.ids.unique.text
return str(y)
class FirstWindow(Screen):
pass
class MainWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
kv_main = Builder.load_file('main.kv')
class MyApp(App):
def build(self):
return kv_main
if __name__ == '__main__':
MyApp().run()
main.kv
#:include First.kv
#:include Second.kv
WindowManager:
MainWindow:
FirstWindow:
SecondWindow:
<MainWindow>
name: "main"
BoxLayout:
Button:
text: "Press"
on_release:
app.root.current = "first"
First.kv
<FirstWindow#Screen>:
name: "first"
BoxLayout:
orientation: "vertical"
Label:
text: "Enter Unique Text for Saving"
font_size: 20
text_size: self.width, None
halign: 'center'
TextInput:
id: unique
hint_text: 'example: Stand25'
Button:
text: "Press"
on_release:
app.root.current = "second"
Second.kv
<SecondWindow#Screen>:
name: "second"
BoxLayout:
orientation: "vertical"
Label:
text: "Unique Text"
font_size: 20
text_size: self.width, None
halign: 'center'
Label:
text: root.get_unique_text()
font_size: 16
canvas.before:
Color:
rgba: 1,1,1,1
Rectangle:
pos: self.pos
size: self.size
color: 0,0,0,1
Button:
text: "Go Back"
on_release:
app.root.current = "first"
Another approach is to use the on_enter() method of a Screen in order to fetch the text. This also requires an id for the unique Label:
<SecondWindow#Screen>:
name: "second"
BoxLayout:
orientation: "vertical"
Label:
text: "Unique Text"
font_size: 20
text_size: self.width, None
halign: 'center'
Label:
id: unique # added id
# text: root.get_unique_text()
font_size: 16
canvas.before:
Color:
rgba: 1,1,1,1
Rectangle:
pos: self.pos
size: self.size
color: 0,0,0,1
Button:
text: "Go Back"
on_release:
app.root.current = "first"
Just add an on_enter() method to the SecondWindow class:
class SecondWindow(Screen):
def on_enter(self, *args):
self.ids.unique.text = self.get_unique_text()
def get_unique_text(self):
x = self.manager.get_screen("first")
y = x.ids.unique.text
return str(y)
In your Second.kv you can reference the text of the TextInput in the First.kv by making a couple changes to the kv files. First, in the main.kv, add an id for the FirstWindow (and eliminate the SecondWindow for now):
WindowManager:
MainWindow:
FirstWindow:
id: first # added id
# SecondWindow: # this gets added later
<MainWindow>
name: "main"
BoxLayout:
Button:
text: "Press"
on_release:
app.root.current = "first"
Then, in the Second.kv, set up the reference to the text of the TextInput:
<SecondWindow#Screen>:
name: "second"
BoxLayout:
orientation: "vertical"
Label:
text: "Unique Text"
font_size: 20
text_size: self.width, None
halign: 'center'
Label:
text: app.root.ids.first.ids.unique.text # reference to unique text
font_size: 16
canvas.before:
Color:
rgba: 1,1,1,1
Rectangle:
pos: self.pos
size: self.size
color: 0,0,0,1
Button:
text: "Go Back"
on_release:
app.root.current = "first"
Since the kv for SecondWindow uses app.root, it will cause an error if SecondWindow is instantiated before the root widget of the App is assigned. To avoid that, add the SecondWindow after a slight delay:
class MyApp(App):
def build(self):
Clock.schedule_once(self.add_second_screen)
return kv_main
def add_second_screen(self, dt):
self.root.add_widget(SecondWindow())

How to reference other class methods in kivy

I'm new to stack overflow but I've been programming in python for a couple years. One thing I havent done much of is object oriented programming. I just started learning kivy and I think I might have screwed up the root of my program by organizing it wrong. I used classes to define each label, button, layout...etc. now I'm wondering how I can reference the attributes of each text input to use what's filled in for other methods I want to define.
Example: have a button when pressed gets the input from two different text inputs and adds them together and a label displays it.
You can see what I've tried in my code but I'm having a hell of a time figuring out how to reference all these things from different classes.
I hope my code is understandable...sorry its lengthy but I want you to see the whole scope of what I did. I'm really hoping I can get a solution to where I can keep my organization and have certain things only show up under their own files but I understand if I have to change a lot of things...
Main.py
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
from kivy.uix.gridlayout import GridLayout
from kivy.uix.anchorlayout import AnchorLayout
from kivy.uix.togglebutton import ToggleButton
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.screenmanager import ScreenManager, Screen
from os import listdir
from kivy.core.window import Window
#load all files with 'kv' in folder
kv_path = './kv/'
for kv in listdir(kv_path):
Builder.load_file(kv_path+kv)
#move the keyboard below text input
Window.softinput_mode = 'below_target'
#classes for savings and loan screens
class SaveButton(Button):
pass
class LoanButton(Button):
pass
class Calculate(Button):
def add(self):
total = SaveDepositInput.inideposit +GoalAmountInput.amount
return total
pass
class TotalsLabel(Label):
pass
#layout classes
class MainBoxLayout(BoxLayout):
pass
class InsideAnchorLayout(AnchorLayout):
pass
class OneColGridlayout(GridLayout):
pass
class AColGridLayout(GridLayout):
pass
class TwoColGridLayout(GridLayout):
pass
class WidgetRelativeLayout(RelativeLayout):
pass
#Toggle Buttons
class DailyToggle(ToggleButton):
pass
class WeeklyToggle(ToggleButton):
pass
class BiWeeklyToggle(ToggleButton):
pass
class MonthlyToggle(ToggleButton):
pass
class YearlyToggle(ToggleButton):
pass
class NoneToggle(ToggleButton):
pass
class Monthly2Toggle(ToggleButton):
pass
class Yearly2Toggle(ToggleButton):
pass
#classes for screen change
class SaveScreen(Screen):
pass
class LoanScreen(Screen):
pass
class SaveLoanTabs(TabbedPanel):
pass
#classes for savings screen
class OutputLabel(Label):
pass
class GoalOutputLabel(Label):
pass
class NoReinvestLabel(Label):
pass
class SaveInstructLabel(Label):
pass
class SaveDepositLabel(Label):
pass
class SaveDepositInput(TextInput):
def inideposit(self):
initial = root.TextInput.text
deposit = int(initial)
return deposit
pass
class SaveYearsLabel(Label):
pass
class SaveYearsInput(TextInput):
pass
class SaveMonthsInput(TextInput):
pass
class ChooseCompoundLabel(Label):
pass
class SaveInterestLabel(Label):
pass
class SaveInterestInput(TextInput):
pass
class RepeatDepositLabel(Label):
pass
class RepeatDeposit2Label(Label):
pass
class RepeatDepositInput(TextInput):
pass
class YearsLabel(Label):
pass
class MonthsLabel(Label):
pass
class GoalAmount(Label):
pass
class GoalAmountInput(TextInput):
def amount(self):
initial = root.TextInput.text
goal = int(initial)
return goal
pass
#classes for loan screen
class LoanOutputLabel(Label):
pass
class LoanInstructLabel(Label):
pass
class LoanAmountLabel(Label):
pass
class LoanAmountInput(TextInput):
pass
# Create the screen manager
sm = ScreenManager()
sm.add_widget(SaveScreen(name='save'))
sm.add_widget(LoanScreen(name='loan'))
#class to run app
class InterestApp(App):
def build(self):
self.title = "Compound Interest and Loan Application"
return sm
#run app
if __name__ == "__main__":
InterestApp().run()
Main.kv file(mostly used for layout formatting)
<SaveScreen>:
MainBoxLayout:
GridLayout:
cols: 2
SaveButton:
on_press: root.manager.current = 'save'
LoanButton:
on_press: root.manager.current = 'loan'
InsideAnchorLayout:
GridLayout:
cols: 1
canvas:
Color:
rgba: .7,1,.7,1
Rectangle:
size: self.size
pos: self.pos
SaveInstructLabel:
InsideAnchorLayout:
TwoColGridLayout:
SaveDepositLabel:
SaveDepositInput:
InsideAnchorLayout:
AColGridLayout:
ChooseCompoundLabel:
InsideAnchorLayout:
AColGridLayout:
cols: 3
DailyToggle:
MonthlyToggle:
YearlyToggle:
InsideAnchorLayout:
TwoColGridLayout:
SaveInterestLabel:
SaveInterestInput:
InsideAnchorLayout:
AColGridLayout:
RepeatDepositLabel:
InsideAnchorLayout:
AColGridLayout:
cols: 5
NoneToggle:
WeeklyToggle:
BiWeeklyToggle:
Monthly2Toggle:
Yearly2Toggle:
InsideAnchorLayout:
TwoColGridLayout:
RepeatDeposit2Label:
RepeatDepositInput:
InsideAnchorLayout:
TwoColGridLayout:
size_hint_y: None
height: dp(50)
SaveYearsLabel:
TwoColGridLayout:
YearsLabel:
SaveYearsInput:
MonthsLabel:
SaveMonthsInput:
InsideAnchorLayout:
TwoColGridLayout:
GoalAmount:
GoalAmountInput:
InsideAnchorLayout:
GridLayout:
cols: 1
canvas:
Color:
rgba: .5,1,1,1
Rectangle:
size: self.size
pos: self.pos
size_hint: None, None
height: dp(80)
width: self.parent.width - dp(15)
TotalsLabel:
OutputLabel:
GoalOutputLabel:
NoReinvestLabel:
InsideAnchorLayout:
AColGridLayout:
Calculate:
<LoanScreen>:
MainBoxLayout:
TwoColGridLayout:
SaveButton:
on_press: root.manager.current = 'save'
LoanButton:
on_press: root.manager.current = 'loan'
Buttons.kv
<SaveButton>:
id: 'save'
text: "[u]Save[/u]"
color: 1, .9, 0, 1
background_normal: ''
background_color: (.5, .5, .5, 1) if self.state == 'normal' else (0, .75, 1, 1)
group: 'menu'
markup: True
<LoanButton>:
text: "[u]Loan[/u]"
markup: True
color: 1, .9, 0, 1
background_normal: ''
background_color: (.5, .5, .5, 1) if self.state == 'normal' else (0, .75, 1, 1)
group: 'menu'
<DailyToggle>:
text: 'Daily'
group: 'compound'
<MonthlyToggle>:
text: 'Monthly'
group: 'compound'
<YearlyToggle>:
text: 'Yearly'
group: 'compound'
<WeeklyToggle>:
text: 'Weekly'
group: 'repeat'
<Yearly2Toggle>:
text: 'Yearly'
group: 'repeat'
<Monthly2Toggle>:
text: 'Monthly'
group: 'repeat'
<BiWeeklyToggle>:
text: 'Bi-Weekly'
group: 'repeat'
<NoneToggle>:
text: 'None'
group: 'repeat'
<Calculate>:
text: '[u][b]Calculate[/b][/u]'
markup: True
on_release: self.add
Labels.kv
<SaveInstructLabel>:
text: "[b]This is the Savings screen. It will calculate compounded interest over a period of time and tell you the result. Follow each prompt to calculate.[/b]"
text_size: root.width, None
size: self.texture_size
valign: 'middle'
halign: 'center'
color: 0,0,0,1
markup: True
<SaveDepositLabel>:
text: "Enter initial deposit amount:"
text_size: root.width, None
size: self.texture_size
valign: 'middle'
halign: 'center'
color: 0,0,0,1
<ChooseCompoundLabel>:
text: "Choose frequency of compounding interest:"
text_size: root.width, None
size: self.texture_size
valign: 'middle'
halign: 'center'
color: 0,0,0,1
<SaveInterestLabel>:
text: 'Enter the interest APY:'
text_size: root.width, None
size: self.texture_size
valign: 'middle'
halign: 'center'
color: 0,0,0,1
<RepeatDepositLabel>:
text: 'How often will you make a deposit:'
text_size: root.width, None
size: self.texture_size
valign: 'middle'
halign: 'center'
color: 0,0,0,1
<SaveYearsLabel>:
text: 'Enter the amount of years and months you will have this account build:'
text_size: root.width, None
size: self.texture_size
valign: 'middle'
halign: 'center'
color: 0,0,0,1
font_size: dp(15)
<YearsLabel>:
text: 'Years:'
text_size: root.width, None
size: self.texture_size
valign: 'middle'
halign: 'right'
color: 0,0,0,1
<MonthsLabel>:
text: 'Months:'
text_size: root.width, None
size: self.texture_size
valign: 'middle'
halign: 'right'
color: 0,0,0,1
<GoalAmount>:
text: '(Optional)Enter an amount you would like to reach:'
text_size: root.width, None
size: self.texture_size
valign: 'middle'
halign: 'center'
color: 0,0,0,1
<OutputLabel>:
text: app.Calculate.add
text_size: root.width, None
size: self.texture_size
valign: 'middle'
halign: 'center'
color: 0,0,0,1
<GoalOutputLabel>:
text: 'total years to reach goal'
text_size: root.width, None
size: self.texture_size
valign: 'middle'
halign: 'center'
color: 0,0,0,1
<NoReinvestLabel>:
text: 'if you didnt reinvest'
text_size: root.width, None
size: self.texture_size
valign: 'middle'
halign: 'center'
color: 0,0,0,1
<TotalsLabel>:
text: '[u][b]TOTALS:[/b][/u]'
markup: True
valign: 'middle'
halign: 'center'
color: 0,0,0,1
<RepeatDeposit2Label>:
text: 'Enter recurring deposit amount:'
text_size: root.width, None
size: self.texture_size
valign: 'middle'
halign: 'center'
color: 0,0,0,1
Textboxes.kv
<SaveDepositInput>:
multiline: False
hint_text: '$0.00'
input_filter: 'float'
size_hint_y: None
height: dp(25)
<SaveInterestInput>:
multiline: False
hint_text: '0.0'
input_filter: 'float'
size_hint_y: None
height: dp(25)
<SaveYearsInput>:
multiline: False
hint_text: '0'
input_filter: 'int'
size_hint_y: None
height: dp(25)
<SaveMonthsInput>:
multiline: False
hint_text: '0'
input_filter: 'int'
size_hint_y: None
height: dp(25)
<GoalAmountInput>:
multiline: False
hint_text: '$0.00'
input_filter: 'float'
size_hint_y: None
height: dp(25)
<RepeatDepositInput>:
multiline: False
hint_text: '$0.00'
input_filter: 'float'
size_hint_y: None
height: dp(25)
And Layouts.kv
<MainBoxLayout>:
spacing: dp(2)
orientation: 'vertical'
canvas.before:
Color:
rgba: .7,1,.7,1
Rectangle:
size: self.size
pos: self.pos
<TwoColGridLayout>:
cols: 2
size_hint: .95, .70
canvas:
Color:
rgba: .5,1,1,1
Rectangle:
size: self.size
pos: self.pos
<OneColGridLayout>:
cols: 1
<AColGridLayout>:
cols: 1
size_hint: .95,.70
canvas:
Color:
rgba: .5,1,1,1
Rectangle:
size: self.size
pos: self.pos
<WidgetRelativeLayout>:
<InsideAnchorLayout>:
anchor_x: 'center'
In your Calculate Button, the add method is trying to call static methods, but the methods you name are actually instance methods. So you need to call them as instance methods. That means that you must have the instances of SaveDepositInput and GoalAmountInput. Since all of these things are in your SaveScreen, you can easily reference them using ids. To do this you cn add an id to SaveDepositInput in your SaveScreen:
InsideAnchorLayout:
TwoColGridLayout:
SaveDepositLabel:
SaveDepositInput:
id: save_deposit_input
and for the GoalInputAmount:
InsideAnchorLayout:
TwoColGridLayout:
GoalAmount:
GoalAmountInput:
id: goal_amount_input
then, in order to access these from the Calculate Button:
InsideAnchorLayout:
AColGridLayout:
Calculate:
gai: goal_amount_input
sdi: save_deposit_input
Then, the CalculateButton class becomes:
class Calculate(Button):
def add(self):
total = self.sdi.inideposit() + self.gai.amount()
return total
A couple other issues. The CalculateButton in your Buttons.kv should be:
<Calculate>:
text: '[u][b]Calculate[/b][/u]'
markup: True
on_release: self.add()
( you were missing the ())
And you have a similar class vs instance issue with your OutputLabel in your Labels.kv.

Kivy Buttons and Labels size based on display size

I'm trying to figure out how to make my buttons and labels fix perfectly depending on my display size. So if the phone display is different, it will always be in fixed size.
Python Code:
import kivy
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
class OpeningScreen(Screen):
pass
class LoginScreen(Screen):
pass
class SignupScreen(Screen):
pass
class ScreenManagement(ScreenManager):
pass
AppKv = Builder.load_file("App.kv")
class MyApp(App):
def build(self):
return AppKv
if __name__ == '__main__':
MyApp().run()
Kv code:
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
#: import hex kivy.utils.get_color_from_hex
#------------------------------------------------------------#
ScreenManagement:
transition: FadeTransition()
OpeningScreen:
LoginScreen:
SignupScreen:
#------------------------------------------------------------#
<OpeningScreen>:
name: "OpeningScreen"
canvas:
Color:
rgb: 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
Label:
text: "Welcome"
color: 1,1,1,1
font_size: 45
size_hint: 0.2,0.1
pos_hint: {"x":0.40, "top":0.995}
Button:
size: 100,75
on_release: app.root.current = "LoginScreen"
text: "Login"
font_size: 50
color: 1,1,1,1
background_color: (0,0,0,1)
background_normal: ""
background_down: ""
size_hint: 0.3,0.2
pos_hint: {"x":0.35, "top":0.7}
Button:
size: 100,75
on_release: app.root.current = "SignupScreen"
text: "Sign up"
font_size: 50
color: 1,1,1,1
background_color: (0,0,0,1)
background_normal: ""
background_down: ""
size_hint: 0.3,0.2
pos_hint: {"x":0.35, "top":0.4}
#------------------------------------------------------------#
<LoginScreen>:
name: "LoginScreen"
canvas:
Color:
rgb: 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
Label:
text: "Login In"
color: 0,0,0,1
font_size: 45
size_hint: 0.2,0.1
pos_hint: {"x":0.40, "top":0.995}
#------------------------------------------------------------#
<SignupScreen>:
name: "SignupScreen"
canvas:
Color:
rgb: 1, 1, 1
Rectangle:
pos: self.pos
size: self.size
Label:
text: "Sign Up"
color: 0,0,0,1
font_size: 45
size_hint: 0.2,0.1
pos_hint: {"x":0.40, "top":0.995}
I would really appreciate if anyone could help me with this. I was trying to find out how to do this but I couldn't
Button and Label sizes can be set using several different approaches:
Use Kivy Metrics. You can set sizes using dp (for example dp(100)), this is a Density-independent Pixel size. There is also a sp (used similarly) that is Scale-independent Pixels (normally used for font sizes)
Use self.texture_size. You can set sizes using this as size: self.texture_size. This will make your Button and Label widgets just large enough to fit the text in it. You can add some padding by using something like:
width: self.texture_size[0] + dp(10)
height: self.texture_size[1] + dp(10)
Use size_hint. This will ensure that the Button and Label widgets take up the same percentage of your display, regardless of the device.
Don't forget to set size_hint to None, None for the first two above to work.

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

Categories