Hi senior users of python, and thank you for reading this through. I am currently trying to program an accounting application that can read and write files for users.
I have a bit of trouble when showing the data from the user file in listitemview in kivy. I would like the expenditure and description of the item be aligned with their headings, basically by using the tab function in a keyboard. However, tab is not represented by the list item view at all. How do I fix this?
In addition, am i doing this app efficiently? I know logic is handled by the python file and design related stuff goes in the .kv file
Pardon if I do make some mistakes as this is my first post and I am new using python, and even newer with kivy.
python file:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.label import Label
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.lang import Builder
from kivy.properties import ObjectProperty, StringProperty, ListProperty
from kivy.uix.popup import Popup
from kivy.uix.button import Button
import os, re
class HomePage(Screen):
#Includes the name of the App, Instructions, as well as 2 buttons that lead to their respective screens
pass
class NewUserPage(Screen):
def saveuser(self, username, filename):
if len(username) != 0:
newuserfilepath = "users\\"+str(username)
if not os.path.exists(newuserfilepath):
os.makedirs(newuserfilepath)
if len(filename) != 0:
filepath = os.path.join("users\\" + str(username), str(filename) + ".txt")
f = open(filepath, "w")
f.close()
else:
print "Please key in at least one character for a filename."
else:
print "Please type in a Username."
def saveuserpopup(self, username, filename):
filepath = os.path.join("users\\"+str(username), str(filename))
if not os.path.exists(filepath):
box = BoxLayout(orientation="vertical")
box.add_widget(Label(text="Confirm Save %s, under filename: %s" %(username,filename)))
Button1 = Button(text = "Confirm")
Button2 = Button(text = "Cancel")
box.add_widget(Button1)
box.add_widget(Button2)
popup = Popup(title = "Confirm Save User", content = box, size_hint=(0.5,0.5))
buttoncallback = lambda *args: self.saveuser(username, filename)
Button1.bind(on_press = buttoncallback)
Button1.bind(on_release = popup.dismiss)
#Button1.bind(on_press = self.saveuser(username,filename))
Button2.bind(on_press = popup.dismiss)
popup.open()
class ExistingUserPage(Screen):
text = StringProperty("")
content = StringProperty("")
expenditure = StringProperty("")
itemlist = StringProperty("")
def loaduser(self, username, filename):
filepath = os.path.join("users\\"+str(username), str(filename)+".txt")
if os.path.exists(filepath):
try:
file = open(filepath,"r")
content = file.read()
expenditure = content.split()[0::2]
itemlist = content.split()[1::2]
self.text = "User: " + str(username).upper() + " filename: " + str(filename).upper()
self.content = str(content).upper()
self.expenditure = str(expenditure)
self.itemlist = str(itemlist)
except(IOError):
print "You did not fill in sufficient information"
self.content = str("Please fill in the details correctly")
else:
print "Please key in a valid username or filename."
self.text = "User: " + str(username).upper() + " filename: " + str(filename).upper()
self.content = str("Please fill in the details correctly")
class ProcessingUserPage(Screen):
text = StringProperty("")
content = StringProperty("")
expenditure = StringProperty("")
itemlist = StringProperty("")
content_list = ListProperty([])
def function(self,expenditure,itemlist,content): #Function called from the .kv file
integer = re.findall("[0-9]+", expenditure)
word = re.sub("[^\w]"," ", itemlist).split() #https://stackoverflow.com/questions/6181763/converting-a-string-to-a-list-of-words
for i in range(len(integer)):
self.content_list.append((integer[i] + word[i]))
class ScreenManagement(ScreenManager):
pass
class AccountApp(App):
def build(self):
pass
if __name__ == "__main__":
AccountApp().run()
.kv file:
#: import Factory kivy.factory.Factory
#: import ListAdapter kivy.adapters.listadapter.ListAdapter
#: import ListItemButton kivy.uix.listview.ListItemButton
ScreenManagement:
HomePage:
NewUserPage:
ExistingUserPage:
id: ExistingUserPage
ProcessingUserPage:
text: ExistingUserPage.text
content: ExistingUserPage.content
expenditure: ExistingUserPage.expenditure
itemlist: ExistingUserPage.itemlist
<CustomListItemButton@ListItemButton>:
halign: "left"
text_size: self.size
<HomePage>:
name: "HomePage"
GridLayout:
cols:1
rows:3
padding: 10
spacing: 10
BoxLayout:
Label:
text: "My Account"
color: 0,0,1,1 #Blue
text_size: root.width, None
font_size: root.height * 0.1
halign: "center"
valign: "top"
BoxLayout:
Label:
text: "Hello there! I am your friendly accountant Bill! Please select below if you are a 'New' or 'Existing' member"
color: 0.6, 0, 0.6, 1 #True Purple
text_size: root.width, None
font_size: root.height * 0.05
halign: "center"
valign: "top"
GridLayout:
cols: 2
rows: 1
spacing: 30
padding: 50
Button:
font_size: self.width * 0.1
text: "New"
color: 0.43, 0.61, 0.95, 1
background_color: 1, 0.84, 0, 1
on_release: app.root.current = "NewUserPage"
Button:
font_size: self.width * 0.1
text: "Existing"
color: 0.43, 0.61, 0.95, 1
background_color: 1, 0.84, 0, 1
on_release: app.root.current = "ExistingUserPage"
<NewUserPage>:
name: "NewUserPage"
id: NewUserPage
username: user_name
GridLayout:
rows: 7
cols: 1
BoxLayout:
height: "100dp"
size_hint_y: None
padding: 10
Label:
size: self.texture_size
text: "My Account"
color: 0,0,1,1 #Blue
text_size: root.width, None
font_size: root.height * 0.1
halign: "center"
valign: "top"
BoxLayout:
height: "100dp"
size_hint_y: None
Label:
size: self.texture_size
text: "New User Sign Up"
color: 0.6, 0, 0.6, 1 #True Purple
text_size: root.width, None
font_size: root.height * 0.05
halign: "center"
valign: "top"
BoxLayout:
FileChooserIconView:
path: "users\\"
BoxLayout:
height: "40dp"
size_hint_y: None
orientation: "horizontal"
Label:
text: "Insert User Name Here"
TextInput:
id: user_name
BoxLayout:
height: "40dp"
size_hint_y: None
orientation: "horizontal"
Label:
text: "Insert File Name Here"
TextInput:
id: file_name
Button:
height: "40dp"
size_hint_y: None
text: "Submit"
#on_release: NewUserPage.saveuserpopup(user_name.text, file_name.text)
on_release: root.saveuserpopup(user_name.text, file_name.text)
BoxLayout:
height: "40dp"
size_hint_y: None
Button:
text: "Return to Home Page"
on_release: app.root.current = "HomePage"
<ExistingUserPage>:
name: "ExistingUserPage"
id: ExistingUserPage
GridLayout:
cols: 1
rows: 7
BoxLayout:
height: "100dp"
size_hint_y: None
Label:
size: self.texture_size
text: "My Account"
color: 0,0,1,1 #Blue
text_size: root.width, None
font_size: root.height * 0.1
halign: "center"
valign: "bottom"
BoxLayout:
height: "100dp"
size_hint_y: None
Label:
size: self.texture_size
text: "Existing User"
color: 0.6, 0, 0.6, 1 #True Purple
text_size: root.width, None
font_size: root.height * 0.05
halign: "center"
valign: "top"
BoxLayout:
FileChooserIconView:
path: "C:\\Users\\Jamesong\\mystuff\\Kivy_apps\\account\\users\\"
BoxLayout:
height: "40dp"
size_hint_y: None
orientation: "horizontal"
Label:
text: "Insert User Name Here"
TextInput:
id: user_name
multiline: False
BoxLayout:
height: "40dp"
size_hint_y: None
orientation: "horizontal"
Label:
text: "Insert File Name Here"
TextInput:
id: file_name
multiline: False
Button:
height: "40dp"
size_hint_y: None
text: "Submit"
on_press: ExistingUserPage.loaduser(user_name.text, file_name.text)
on_release: app.root.current = "ProcessingUserPage"
on_release: user_name.text = ""
on_release: file_name.text = ""
#on_release: ProcessingPage.load(user_name.text, file_name.text)
BoxLayout:
height: "40dp"
size_hint_y: None
Button:
text: "Return to Home Page"
on_release: app.root.current = "HomePage"
<ProcessingUserPage>:
name: "ProcessingUserPage"
BoxLayout:
orientation: "vertical"
Label:
size_hint_y: 0.1
text: "My Account"
color: 0,0,1,1 #Blue
text_size: self.size
font_size: root.height * 0.1
halign: "center"
valign: "bottom"
Label:
size_hint_y: 0.1
text: "Processing " + root.text
color: 0.6, 0, 0.6, 1 #True Purple
text_size: self.size
font_size: root.height * 0.05
halign: "center"
valign: "bottom"
BoxLayout:
orientation: "horizontal"
size_hint_y: 0.1
Label:
size_hint_x: 0.2
text_size: self.size
text: "Expenditure"
halign: "center"
Label:
size_hint_x: 0.8
text_size: self.size
text: "Item"
halign: "left"
BoxLayout:
orientation: "horizontal"
size_hint_y: 0.5
ListView:
id: content_list_view
adapter:
ListAdapter(data = root.content_list, cls = Factory.CustomListItemButton)
BoxLayout:
orientation: "horizontal"
size_hint_y: 0.1
Label:
text: "Input"
TextInput:
id: input
multiline: False
Button:
size_hint_y: 0.1
text: "Press"
on_press: root.function(root.expenditure,root.itemlist,root.content)
Button:
size_hint_y: 0.1
text: "Back to Home"
on_press: app.root.current = "HomePage"
Eg .txt file
10 LUNCH
13 DINNER
8 DRINK
18 SHOP
13 DRINK
4 SNACK
6 SNACK
10 LUNCH
13 DINNER
8 DRINK
18 SHOP
13 DRINK
4 SNACK
6 SNACK