Updating Data from Kivy Python to Excel - python

I have a problem on updating data that I want to insert to excel. At first, I create the data and it work successfully without error. After I want to insert new data from Kivy, it doesn't work. Error said that the process denied by PC. I had watch tutorial and it seem my problem still unsolved. Here is my code from VSCode:
Window.size = (500, 500)
outWorkbook = xlsxwriter.Workbook("staff.xlsx")
outSheet = outWorkbook.add_worksheet()
class Menu(Screen):
pass
class Enter(Screen):
input1 = ObjectProperty(None)
input2 = ObjectProperty(None)
input3 = ObjectProperty(None)
input4 = ObjectProperty(None)
input5 = ObjectProperty(None)
input6 = ObjectProperty(None)
def clear(self):
self.ids.inherent_input.text = ''
self.ids.inherent2_input.text = ''
self.ids.inherent3_input.text = ''
self.ids.inherent4_input.text = ''
self.ids.inherent5_input.text = ''
self.ids.inherent6_input.text = ''
def btn(self):
self.L = ()
print("Name: " + self.input1.text,
"Activity/Programme: " + self.input2.text,
"Date: " + self.input3.text,
"Place: " + self.input4.text,
"Time from: " + self.input5.text,
"Time to: " + self.input6.text)
staff = ({"Name": [str(self.input1.text)], "Program/Activity": [str(self.input2.text)], "Place" : [str(self.input3.text)], "Date": [str(self.input4.text)], "Time From" : [str(self.input5.text)], "Time To" : [str(self.input6.text)]})
self.L = pd.DataFrame(staff)
self.input1.text = ''
self.input2.text = ''
self.input3.text = ''
self.input4.text = ''
self.input5.text = ''
self.input6.text = ''
print(self.L)
with pd.ExcelWriter('staff.xlsx') as writer:
self.L.to_excel(writer)
class Info(Screen):
pass
class WindowManager(ScreenManager):
pass
kv = Builder.load_file('window.kv')
class MyLayout(Widget):
class WindowApp(App):
def build(self):
return kv
if name == 'main':
WindowApp().run()
And here is my kv file:
WindowManager:
Menu:
Enter:
Info:
:
name: "MainMenu"
BoxLayout:
orientation: "vertical"
size: root.width, root.height
padding: 10
spacing: 15
Button:
id: enter
text: "Enter"
on_release: app.root.current = "enter"
Button:
id: info
text: "Information"
on_release: app.root.current = "info"
Button:
id: exit
text: "Exit"
on_press: quit()
:
name: "enter"
input1: inherent_input
input2: inherent2_input
input3: inherent3_input
input4: inherent4_input
input5: inherent5_input
input6: inherent6_input
BoxLayout:
orientation: "vertical"
size: root.width, root.height
padding:10
spacing:10
BoxLayout:
spacing: 10
Label:
text: "Name"
font_size: 20
TextInput:
id: inherent_input
multiline: False
BoxLayout:
spacing: 10
Label:
text: "Activity/Programme"
font_size: 20
TextInput:
id: inherent2_input
multiline: False
BoxLayout:
spacing: 10
Label:
text: "Place"
font_size: 20
TextInput:
id: inherent3_input
multiline: False
BoxLayout:
Label:
text: "Date"
font_size: 20
TextInput:
id: inherent4_input
multiline: False
BoxLayout:
padding: 10
spacing: 10
Label:
text: "Time from"
font_size: 20
TextInput:
id: inherent5_input
multiline: False
Label:
text: "to"
font_size: 20
TextInput:
id: inherent6_input
multiline: False
BoxLayout:
padding: 15
spacing: 15
Button:
id: clear
text: "Clear"
font_size: 12
on_press: root.clear()
Button:
id: back
text: "Back to Menu"
font_size: 12
on_release: app.root.current = "MainMenu"
Button:
id: submit
text: "Submit"
font_size: 12
on_press: root.btn()
:
name: "info"
BoxLayout:
orientation: 'vertical'
padding: 15
spacing: 15
Label:
id: information
text: "This is just a test, an Alpha version of Prototype"
font_size: 20
bold: True
italic: False
outline_color: (0,0,0)
Button:
id: returnBack
text: "Return"
on_release: app.root.current = "MainMenu"
Any help will be appreciated.

The error you're getting can mean multiple things.
Are you trying to open a directory?
If so, don't do it. It will most likely result in the error you mentioned.
Is the file you are trying to opened anywhere else?
You may have to close it, for python to open it. You probably have experienced this before when trying to delete a file in the explorer, that is opened somewhere else. Windows won't let you do this. Check whether the file in question is opened before trying to access it.

Related

How to output the value to the screen from textinput in kivi?

I'm a new coder and I have so many things I don't understand. I'm making a book management GUI, buy and sell books.
main.py
from kivy.app import App
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.uix.widget import Widget
#Define our different screens
class FirstWindow(Screen):
pass
class SecondWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
kv = Builder.load_file('images.kv')
class MyLayout(Widget):
pass
class QuanlyApp(App):
def build(self):
return kv
if __name__=='__main__':
QuanlyApp().run()
images.kv
WindowManager:
FirstWindow:
SecondWindow:
<FirstWindow>:
name: "first"
BoxLayout:
Button:
text: "enter"
font_size: 32
size: 100, 50
pos: root.x, root.top - 50
on_release:
app.root.current = "second"
root.manager.transition.direction = "left"
Button:
text: "import"
font_size: 32
size: 100, 50
pos: root.x - 50, root.top
on_release:
app.root.current = "second"
root.manager.transition.direction = "left"
<SecondWindow>:
name: "second"
BoxLayout:
orientation: "vertical"
size: root.width, root.height
Label:
text: "hi"
font_size: 24
GridLayout:
cols: 2
rows: 7
Label:
text: "Ten sach"
id: name
color: '6EE369'
TextInput:
id: name
multiline: False
Label:
text: "Tac gia"
color: '6EE369'
TextInput:
id: author
multiline: False
Label:
text: "The loai"
color: '6EE369'
TextInput:
id: kind
multiline: False
Label:
text: "Ma"
color: '6EE369'
TextInput:
id: id
multiline: False
Label:
text: "Gia nhap"
color: '6EE369'
TextInput:
id: price1
multiline: False
Label:
text: "Gia xuat"
color: '6EE369'
TextInput:
id: price2
multiline: False
Button:
text: "buy"
font_size: 24
size: 100, 50
background_color: 'E36982'
background_normal: ''
Button:
text: "sell"
font_size: 24
size: 100, 50
background_color: '82E369'
background_normal: ''
The result I want is:
enter image description here
and
enter image description here
I can print value from images.kv but I can't find how to print the values of two TextInputs when a button is pressed.
This is the first time I have posted so I have a lot of mistakes. Thanks if I can get any help from everyone.

Access python list in kivy file

I'm a physicist, and as everyone knows, we like our cocktails. I am therefore trying to build an automatic bartender. Unfortunately, the only experience I have with programming in python is to do physics simulations, and I am not that savvy with coding anyways.
My problem is now this: there is a python list in the class BartenderApp that I want to use in the kivy file, specifically in the LoadNewIngredients in the kivy file, for the spinners to take their options from. I have looked quite a while for a solution and none have worked so far. I know I should be able to put the spinners and labels in the python file using a for loop, but I would much rather have it a bit more clean and keep them in the kivy file.
So if anyone could help me how to pass the list to the kivy file, it'd be much appreciated!
Here is the .py file:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window
from kivy.uix.button import Button
from kivy.clock import mainthread
from functools import partial
#Import drink list
from drinks import drink_list, drink_options
class drink:
def __init__(self,name,ingredients,amount):
self.name = name
self.ingredients = ingredients
self.amount = amount
#Define the different screens
class MainMenu(Screen):
pass
class UseLastIngredients(Screen):
pass
class DrinkMenu(Screen):
#Mainthread will pause shortly to give script time, the rest adds the buttons
#mainthread
def on_enter(self):
self.buttons = []
self.ids.drinks.clear_widgets()
for btn in range(len(drink_list)):
self.buttons.append(Button(text=str(drink_list[btn]['name'])))
self.buttons[btn].bind(on_press = self.pour_drink)
self.ids.drinks.add_widget(self.buttons[btn])
def pour_drink(self, button):
print(button.text)
class LoadNewIngredients(Screen):
def spinner_clicked(self, ident, value):
if ident == 1:
pass
if ident == 2:
pass
if ident == 3:
pass
if ident == 4:
pass
if ident == 5:
pass
if ident == 6:
pass
if ident == 7:
pass
if ident == 8:
pass
#Define the ScreenManager
class MenuManager(ScreenManager):
pass
#Designate the .kv design file
kv = Builder.load_file('bartenderkv.kv')
class BartenderApp(App):
#I want to use the ingredients list in the kivy file
global ingredients
ingredients = []
for drink in range(len(drink_list)):
ings = list(drink_list[drink]['ingredients'].keys())
for ing in range(len(ings)):
elem = ings[ing]
if elem not in ingredients:
ingredients.append(elem)
def build(self):
return kv
if __name__ == '__main__':
BartenderApp().run()
and here is the .kv file:
#:import Factory kivy.factory.Factory
#:import ScrollView kivy.uix.scrollview
MenuManager:
MainMenu:
LoadNewIngredients:
DrinkMenu:
<MainMenu>:
name: "MainMenu"
GridLayout:
rows: 3
size: root.width, root.height
padding: 10
spacing: 10
Label:
text: "Main Menu"
font_size: 32
GridLayout:
cols: 2
size: root.width, root.height
spacing: 10
Button:
text: "Use Last Ingredients"
font_size: 32
on_release: app.root.current = "DrinkMenu"
Button:
text: "Load New Ingredients"
font_size: 32
on_release: app.root.current = "LoadNewIngredients"
Button:
text: "See Permissable Ingredients"
font_size: 32
#on_press: print("It Works")
on_release: Factory.PermissablePopup().open()
<LoadNewIngredients>:
name: "LoadNewIngredients"
GridLayout:
cols: 2
size: root.width, root.height
padding: 10
spacing: 10
GridLayout:
size: root.width, root.height
size_hint_x: 0.4
rows: 2
Button:
text: "Continue"
font_size: 24
on_release: app.root.current = "DrinkMenu"
Button:
text: "Main Menu"
font_size: 24
on_release: app.root.current = "MainMenu"
GridLayout:
#This is where I want the spinners to take in the ingredients list as options.
id: choices
rows: 4
orientation: 'tb-lr'
Label:
id: pump_1
text: "Pump 1"
font_size: 24
Label:
id: pump_2
text: "Pump 2"
font_size: 24
Label:
id: pump_3
text: "Pump 3"
font_size: 24
Label:
id: pump_4
text: "Pump 4"
font_size: 24
#Spinner is the easy drop down version in kivy, lets see how it looks.
Spinner:
id: spinner_id_1
text: "Pump_1"
values: ["1", "2", "3"]
on_text: root.spinner_clicked(1,spinner_id_1.text)
Spinner:
id: spinner_id_2
text: "Pump_2"
values: ["1", "2", "3"]
on_text: root.spinner_clicked(2,spinner_id_2.text)
Spinner:
id: spinner_id_3
text: "Pump_3"
values: ["1", "2", "3"]
on_text: root.spinner_clicked(3,spinner_id_3.text)
Spinner:
id: spinner_id_4
text: "Pump_4"
values: ["1", "2", "3"]
on_text: root.spinner_clicked(4,spinner_id_4.text)
Label:
id: pump_5
text: "Pump 5"
font_size: 24
Label:
id: pump_6
text: "Pump 6"
font_size: 24
Label:
id: pump_7
text: "Pump 7"
font_size: 24
Label:
id: pump_8
text: "Pump 8"
font_size: 24
#Spinner is the drop down version, lets see how it looks.
Spinner:
id: spinner_id_5
text: "Pump_5"
values: ["1", "2", "3"]
on_text: root.spinner_clicked(5,spinner_id_5.text)
Spinner:
id: spinner_id_6
text: "Pump_6"
values: ["1", "2", "3"]
on_text: root.spinner_clicked(6,spinner_id_6.text)
Spinner:
id: spinner_id_7
text: "Pump_7"
values: ["1", "2", "3"]
on_text: root.spinner_clicked(7,spinner_id_7.text)
Spinner:
id: spinner_id_8
text: "Pump_8"
values: ["1", "2", "3"]
on_text: root.spinner_clicked(8,spinner_id_8.text)
<DrinkMenu>:
name: "DrinkMenu"
GridLayout:
cols: 2
width: root.width
height: self.minimum_height
padding: 10
spacing: 10
GridLayout:
height: root.height
size_hint_x: 0.4
rows: 2
Button:
text: "Top Up"
font_size: 24
on_release:
Button:
text: "Main Menu"
font_size: 24
on_release: app.root.current = "MainMenu"
ScrollView:
size_hint_y: 0.73
pos_hint: {'x':0, 'y': 0.11}
do_scroll_x: False
do_scroll_y: True
GridLayout:
id: drinks
orientation: 'lr-tb'
size_hint_y: None
size_hint_x: 1.0
cols: 3
height: self.minimum_height
row_default_height: 180
row_force_default: True
#Create a rounded button, the #Button is what it inherits
<RoundedButton#Button>
background_color: (0,0,0,0)
background_normal: ''
canvas.before:
Color:
rgba:
(48/255,84/255,150/255,1)\
if self.state == 'normal' else (0.6,0.6,1,1) # Color is red if button is not pressed, otherwise color is green
RoundedRectangle:
size: self.size
pos: self.pos
radius: [58]
<PermissablePopup#Popup>
auto_dismiss: False
#size_hint: 0.6,0.2
#pos_hint: {"x":0.2, "top":0.9}
title: "Permissable Ingredients"
GridLayout:
rows: 2
size: root.width, root.height
spacing: 10
GridLayout:
cols: 2
Label:
text: "Sodas"
font_size: 32
#Add list of sodas
Label:
text: "Alcohol"
font_size: 32
#Add list of alcohols
Button:
text: "Done"
font_size: 24
on_release: root.dismiss()
Try adding a ListProperty (https://kivy.org/doc/stable/api-kivy.properties.html) to you App class. Like this:
class BartenderApp(App):
ingredients = ListProperty()
for drink in range(len(drink_list)):
ings = list(drink_list[drink]['ingredients'].keys())
for ing in range(len(ings)):
elem = ings[ing]
if elem not in self.ingredients:
self.ingredients.append(elem)
A list property is both accessible in the Python and KV file. What's more is that they can be bound to a callback so that you can create your own event!
In the KV file you can now refer to app.ingredients and any object that is defined by the list will be automatically updated when app.ingredients is changed. So for example if you had the following label in your KV file:
Label:
text: app.ingredients[0]
The text would automatically update when you change the list. Be careful though, you may get an index error if there isn't an object in the index you're referencing.
Let me know how you get on with this and whether it solves your problem.
I finally managed to solve it, maybe not the best solution, but it works.
In the class LoadNewIngredients I wrote a function that checked what ingredients are available and returns a list with the ingredients. This function is then referenced in the kivy app using root.function_name().
The .py file:
#Everything needed for kivy
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window
from kivy.uix.button import Button
from kivy.clock import mainthread
from functools import partial
#Import drink list
from drinks import drink_list, drink_options
class drink:
def __init__(self,name,ingredients,amount):
self.name = name
self.ingredients = ingredients
self.amount = amount
#Define the different screens
class MainMenu(Screen):
pass
class UseLastIngredients(Screen):
pass
class DrinkMenu(Screen):
#Mainthread will pause shortly to give script time, the rest adds the buttons
#mainthread
def on_enter(self):
self.buttons = []
self.ids.drinks.clear_widgets()
for btn in range(len(drink_list)):
self.buttons.append(Button(text=str(drink_list[btn]['name'])))
self.buttons[btn].bind(on_press = self.pour_drink)
self.ids.drinks.add_widget(self.buttons[btn])
def pour_drink(self, button):
print(button.text)
class LoadNewIngredients(Screen):
def spinner_clicked(self, ident, value):
if ident == 1:
pass
if ident == 2:
pass
if ident == 3:
pass
if ident == 4:
pass
if ident == 5:
pass
if ident == 6:
pass
if ident == 7:
pass
if ident == 8:
pass
#I want to use the ingredients list in the kivy file
def get_ingredients(self,*args,**kwargs):
global ingredients
ingredients = []
for drink in range(len(drink_list)):
ings = list(drink_list[drink]['ingredients'].keys())
for ing in range(len(ings)):
elem = ings[ing]
if elem not in ingredients:
ingredients.append(elem)
return ingredients
#Define the ScreenManager
class MenuManager(ScreenManager):
pass
#Designate the .kv design file
kv = Builder.load_file('bartenderkv.kv')
class BartenderApp(App):
def build(self):
return kv
if __name__ == '__main__':
BartenderApp().run()
and here is the .kv file:
#Need to define everything, the ScreenManager is the entity that keeps tabs
#on all the different menu windows
#This is for the popup, lets you instansiate a class from anywhere
#:import Factory kivy.factory.Factory
#:import ScrollView kivy.uix.scrollview
MenuManager:
MainMenu:
LoadNewIngredients:
DrinkMenu:
<MainMenu>:
name: "MainMenu"
GridLayout:
rows: 3
size: root.width, root.height
padding: 10
spacing: 10
Label:
text: "Main Menu"
font_size: 32
GridLayout:
cols: 2
size: root.width, root.height
spacing: 10
Button:
text: "Use Last Ingredients"
font_size: 32
on_release: app.root.current = "DrinkMenu"
Button:
text: "Load New Ingredients"
font_size: 32
on_release: app.root.current = "LoadNewIngredients"
Button:
text: "See Permissable Ingredients"
font_size: 32
on_release: Factory.PermissablePopup().open()
<LoadNewIngredients>:
name: "LoadNewIngredients"
GridLayout:
cols: 2
size: root.width, root.height
padding: 10
spacing: 10
#size hint sets relative sized, x-dir, y-dir
GridLayout:
size: root.width, root.height
size_hint_x: 0.4
rows: 2
Button:
text: "Continue"
font_size: 24
on_release: app.root.current = "DrinkMenu"
Button:
text: "Main Menu"
font_size: 24
on_release: app.root.current = "MainMenu"
GridLayout:
id: choices
rows: 4
orientation: 'tb-lr'
Label:
id: pump_1
text: "Pump 1"
font_size: 24
Label:
id: pump_2
text: "Pump 2"
font_size: 24
Label:
id: pump_3
text: "Pump 3"
font_size: 24
Label:
id: pump_4
text: "Pump 4"
font_size: 24
Spinner:
id: spinner_id_1
text: "Pump_1"
#This references the get_ingredients function in the main py file
values: root.get_ingredients()
on_text: root.spinner_clicked(1,spinner_id_1.text)
Spinner:
id: spinner_id_2
text: "Pump_2"
values: root.get_ingredients()
on_text: root.spinner_clicked(2,spinner_id_2.text)
Spinner:
id: spinner_id_3
text: "Pump_3"
values: root.get_ingredients()
on_text: root.spinner_clicked(3,spinner_id_3.text)
Spinner:
id: spinner_id_4
text: "Pump_4"
values: root.get_ingredients()
on_text: root.spinner_clicked(4,spinner_id_4.text)
Label:
id: pump_5
text: "Pump 5"
font_size: 24
Label:
id: pump_6
text: "Pump 6"
font_size: 24
Label:
id: pump_7
text: "Pump 7"
font_size: 24
Label:
id: pump_8
text: "Pump 8"
font_size: 24
Spinner:
id: spinner_id_5
text: "Pump_5"
values: root.get_ingredients()
on_text: root.spinner_clicked(5,spinner_id_5.text)
Spinner:
id: spinner_id_6
text: "Pump_6"
values: root.get_ingredients()
on_text: root.spinner_clicked(6,spinner_id_6.text)
Spinner:
id: spinner_id_7
text: "Pump_7"
values: root.get_ingredients()
on_text: root.spinner_clicked(7,spinner_id_7.text)
Spinner:
id: spinner_id_8
text: "Pump_8"
values: root.get_ingredients()
on_text: root.spinner_clicked(8,spinner_id_8.text)
<DrinkMenu>:
name: "DrinkMenu"
GridLayout:
cols: 2
width: root.width
height: self.minimum_height
padding: 10
spacing: 10
GridLayout:
height: root.height
size_hint_x: 0.4
rows: 2
Button:
text: "Top Up"
font_size: 24
on_release:
Button:
text: "Main Menu"
font_size: 24
on_release: app.root.current = "MainMenu"
ScrollView:
size_hint_y: 0.1
pos_hint: {'x':0, 'y': 0.11}
do_scroll_x: False
do_scroll_y: True
GridLayout:
id: drinks
orientation: 'lr-tb'
size_hint_y: None
size_hint_x: 1.0
cols: 3
height: self.minimum_height
row_default_height: 100
row_force_default: True
<RoundedButton#Button>
background_color: (0,0,0,0)
background_normal: ''
canvas.before:
Color:
rgba:
(48/255,84/255,150/255,1)\
if self.state == 'normal' else (0.6,0.6,1,1)
RoundedRectangle:
size: self.size
pos: self.pos
radius: [58]
<PermissablePopup#Popup>
auto_dismiss: False
#size_hint: 0.6,0.2
#pos_hint: {"x":0.2, "top":0.9}
title: "Permissable Ingredients"
GridLayout:
rows: 2
size: root.width, root.height
spacing: 10
GridLayout:
cols: 2
Label:
text: "Sodas"
font_size: 32
#Add list of sodas
Label:
text: "Alcohol"
font_size: 32
#Add list of alcohols
Button:
text: "Done"
font_size: 24
on_release: root.dismiss()
Hopefully this helps if someone else has the same problem.
But if anyone has any other solutions (or suggestions about the code in general), please let me know!

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

Clearing inputText with a button release in Kivy

Let's imagine we have a form where the person wrote some stuff on the inputs but need to clear them all, how would I do that in Kivy?
I tried this but it didn't work:
main.py:
class CreateUra(GridLayout):
def clear_inputs(self, *args):
for item in args:
item = ''
class UraApp(App):
def build(self):
return CreateUra()
if __name__ == "__main__":
UraApp().run()
Ura.kv:
<CreateUra>:
cols: 1
padding: 10
spacing: 10
row_force_default: True
row_default_height: '32dp'
BoxLayout:
Label:
text: 'Contexto:'
TextInput:
hint_text: "Contexto da ura"
focus: True
multiline: False
cursor_color: (0, 0, 0, 0.8)
id: context_input
BoxLayout:
Label:
text: 'Nome da ura:'
TextInput:
hint_text: "Nome do arquivo da ura"
multiline: False
cursor_color: (0, 0, 0, 0.8)
id: ura_file_name
Button:
text: 'Create Ura'
id: bt_create
on_press: root.uraConfig()
on_release: root.clear_inputs(context_input.text, ura_file_name.text)
*Ignore the uraConfig on the on_press
I tried this way and it worked:
Button:
text: 'Create Ura'
id: bt_create
on_press: root.uraConfig()
on_release: context_input.text = ''
I'm quite new to kivy and Python, so I'm not sure if this was the best way to clear the texts, but what I did wrong? Or if you guys could give some light in the best way to do it.
The problem in your case is passing the text, but not text property but a copy of the text, so even if you set it to empty is not reflected in the textinput. You have to pass the textinput as a list, iterate and set the property with an empty string:
*.kv
Button:
text: 'Create Ura'
id: bt_create
on_press: root.uraConfig()
on_release: root.clear_inputs([context_input, ura_file_name]) # <-- add brackets
*.py
class CreateUra(GridLayout):
def clear_inputs(self, text_inputs):
for text_input in text_inputs:
text_input.text = ""
I tried this and it work for me:
*kv
TextInput:
id: my_text_input
hint_text: "Your name..."
size_hint: None, None
height: "50dp"
width: "100dp"
multiline: False
on_text_validate:root.on_text_validate(self)
Label:
id: txt
text: "Your name is: " + root.text_input_str
Button:
text: "Reset details"
on_press: root.on_button_reset_details()
size_hint: None, None
width: "100dp"
height: "50dp"
*py
class WidgetGalaxy(GridLayout):
text_input_str = StringProperty("")
def on_text_validate(self, widget):
self.text_input_str = widget.text
def on_button_reset_details(self):
self.text_input_str = ""
self.ids.my_text_input.text = ""
if self.ids.my_text_input.text == "":
self.ids.my_text_input.hint_text = self.ids.my_text_input.hint_text
with all that it should work for your code...
If having issue check your code preference and some predefined local or global variable.

Passing information between classes in Kivy

I have a basic app in Kivy with a class for each screen. I would like to have the user enter some text in one screen and for this to be displayed in a label on another screen.
I've been through all the similar posts but can't seem to get anything working.
The two classes EnterSentences and Review have ObjectProperties. Is there a way to connect these with bind? Or do I need to just structure things completely differently?
Any help much appreciated. Here's the .py file
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.properties import ObjectProperty
class MainMenu(BoxLayout):
def enter_sentences_button(self):
self.clear_widgets()
self.add_widget(EnterSentences())
def review_button(self):
self.clear_widgets()
self.add_widget(Review())
def settings_button(self):
pass
class EnterSentences(BoxLayout):
search_box = ObjectProperty()
def main_menu(self):
self.clear_widgets()
self.add_widget(MainMenu())
class Review(BoxLayout):
sentence = ObjectProperty()
class NewApp(App):
pass
if __name__ == "__main__":
NewApp().run()
and the .kv file:
MainMenu
<MainMenu>:
orientation: 'vertical'
Label:
text: "Welcome to your Flash Card App!"
size_hint_y: 5
BoxLayout:
size_hint_y: 1
orientation: "horizontal"
Button:
text: "Enter sentences"
on_press: root.enter_sentences_button()
Button:
text: "Review"
on_press: root.review_button()
Button:
text: "Settings"
<EnterSentences>:
search_box: enter_sentence
orientation: "vertical"
Label:
text: "Enter your sentences here:"
size_hint_y: 4
TextInput:
id: enter_sentence
TextInput:
TextInput:
BoxLayout:
orientation: 'horizontal'
Button:
text: 'Main Menu'
on_press: root.main_menu()
Label:
text: ''
<Review>:
orientation: 'vertical'
Label:
size_hint_y: 4
text: root.search_box
TextInput:
size_hint_y: 1
You only have to change the kv file. You could add the text from the 3 TextInputs to 3 variables readable from the whole app:
TextInput:
+ on_text: app.sentence1=self.text
id: enter_sentence
TextInput:
+ on_text: app.sentence2=self.text
TextInput:
+ on_text: app.sentence3=self.text
Then you could access the new variables in the Review:
<Review>:
orientation: 'vertical'
Label:
size_hint_y: 4
text: app.sentence1 + "\n" + app.sentence2 + "\n" + app.sentence3
TextInput:
size_hint_y: 1

Categories