I'm just learning the Python , please don't judge too harshly. I'm working on a coffee shop position count program using Kivy. I've run into a few issues that I can't resolve.
There are 4 screens in the reduced copy of my program: MainWindow, CoffeeWindow, TeaWindow and SumMenu. There are buttons on the CoffeeWindow and TeaWindow screens, the program count clicks on these buttons and should show the result in the SumMenu screen .
I can show in SumMenu the number of clicks on the buttons from CoffeeWindow or TeaWindow, but I can't show the result from both screens at the same time, I tried different ways, but I constantly encounter errors.
Please help me solve my problem, thanks in advance!
main.py:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
class SumMenu(Screen):
def on_pre_enter(self, *args):
if self.manager.ids.coffee.coff: self.ids.label1.text = ""
if self.manager.ids.coffee.coff1 != 0: self.ids.label1.text += f"coffee 1 : {self.manager.ids.coffee.coff1}\n"
if self.manager.ids.coffee.coff2 != 0: self.ids.label1.text += f"coffee 2 : {self.manager.ids.coffee.coff2}\n"
if self.manager.ids.coffee.coff3 != 0: self.ids.label1.text += f"coffee 3 : {self.manager.ids.coffee.coff3}\n"
if self.manager.ids.coffee.coff4 != 0: self.ids.label1.text += f"coffee 4 : {self.manager.ids.coffee.coff4}\n"
#if self.manager.ids.tea.tea: self.ids.label1.text = ""
#if self.manager.ids.tea.tea1 != 0: self.ids.label2.text += f"tea 1 : {self.manager.ids.tea.tea1}\n"
#if self.manager.ids.tea.tea2 != 0: self.ids.label2.text += f"tea 2 : {self.manager.ids.tea.tea2}\n"
#if self.manager.ids.tea.tea3 != 0: self.ids.label2.text += f"tea 3 : {self.manager.ids.tea.tea3}\n"
#if self.manager.ids.tea.tea4 != 0: self.ids.label2.text += f"tea 4 : {self.manager.ids.tea.tea4}\n"
class CoffeeWindow(Screen):
coff = True
coff_tup = []
def count(self, num):
self.coff_tup.append(num)
self.coff1 = self.coff_tup.count(1)
self.coff2 = self.coff_tup.count(2)
self.coff3 = self.coff_tup.count(3)
self.coff4 = self.coff_tup.count(4)
class TeaWindow(Screen):
tea = True
tea_tup = []
def count(self, num):
self.tea_tup.append(num)
self.tea1 = self.tea_tup.count(1)
self.tea2 = self.tea_tup.count(2)
self.tea3 = self.tea_tup.count(3)
self.tea4 = self.tea_tup.count(4)
class MainWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
kv = Builder.load_file('my.kv')
class MainApp(App):
def build(self):
return kv
if __name__ == '__main__':
MainApp().run()
my.kv:
<CoffeeButton@Button>:
font_size: "23sp"
size_hint: 0.5, 0.12
bold: True
<HorizBoxLayout@BoxLayout>:
padding: 30
spacing: 25
orientation: "horizontal"
<SumLabel@Label>:
bold: True
halign: "left"
valign: "middle"
WindowManager:
MainWindow:
id: main
CoffeeWindow:
id: coffee
TeaWindow:
id: tea
SumMenu:
id: summenu
<MainWindow>:
name: "main_window"
HorizBoxLayout:
CoffeeButton:
text: "Go to Coffee"
pos_hint: {'x' : 0, 'center_y' : 0.5}
on_release:
app.root.current = "coffee"
app.root.transition.direction = "left"
CoffeeButton:
text: "Go to Tea"
pos_hint: {'x' : 0, 'center_y' : 0.5}
on_release:
app.root.current = "tea"
app.root.transition.direction = "left"
<CoffeeWindow>:
name: "coffee"
GridLayout:
rows: 3
size_hint: 1, 0.8
pos_hint: {'center_x' : 0.5, 'top' : 0.95}
padding: 40
spacing: 40
CoffeeButton:
text: "coffee 1"
on_release:
root.count(1)
CoffeeButton:
text: "coffee 2"
on_release:
root.count(2)
CoffeeButton:
text: "coffee 3"
on_release:
root.count(3)
CoffeeButton:
text: "coffee 4"
on_release:
root.count(4)
Button:
bold: True
size_hint: 0.5, 0.1
font_size: "18sp"
pos_hint: {'right' : 0.5, 'y' : 0}
text: "Show Menu"
on_release:
app.root.current = "summenu"
app.root.transition.direction = "left"
Button:
bold: True
size_hint: 0.5, 0.1
font_size: "18sp"
pos_hint: {'right' : 0.5, 'y' : 0}
text: "Go to Main"
on_release:
app.root.current = "main_window"
app.root.transition.direction = "right"
<TeaWindow>:
name: "tea"
GridLayout:
rows: 3
size_hint: 1, 0.8
pos_hint: {'center_x' : 0.5, 'top' : 0.95}
padding: 40
spacing: 40
CoffeeButton:
text: "tea 1"
on_release:
root.count(1)
CoffeeButton:
text: "tea 2"
on_release:
root.count(2)
CoffeeButton:
text: "tea 3"
on_release:
root.count(3)
CoffeeButton:
text: "tea 4"
on_release:
root.count(4)
Button:
bold: True
size_hint: 0.5, 0.1
font_size: "18sp"
pos_hint: {'right' : 0.5, 'y' : 0}
text: "Show Menu"
on_release:
app.root.current = "summenu"
app.root.transition.direction = "left"
Button:
bold: True
size_hint: 0.5, 0.1
font_size: "18sp"
pos_hint: {'right' : 0.5, 'y' : 0}
text: "Go to Main"
on_release:
app.root.current = "main_window"
app.root.transition.direction = "right"
<SumMenu>:
name: "summenu"
BoxLayout:
orientation: "vertical"
padding: 30
spacing: 15
pos_hint: {'x' : 0, 'center_y' : 0.57}
size_hint: 1, 0.88
SumLabel:
id: label1
SumLabel:
id: label2
Button:
bold: True
size_hint: 0.5, 0.1
font_size: "18sp"
pos_hint: {'right' : 0.5, 'y' : 0}
text: "Go to Main"
on_release:
app.root.current = "main_window"
app.root.transition.direction = "right"
You can try storing all of the button click counts in a dictionary that get's updated whenever one of the buttons is pressed. Then in your summ screen you can simply build your label text based off of the keys and values in the dictionaries for the Tea and Coffee screens.
For example:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
class SumMenu(Screen):
def on_pre_enter(self, *args):
coffee = self.manager.ids.coffee.clickCounter
tea = self.manager.ids.tea.clickCounter
self.set_label_text("Coffee", coffee, self.ids.label1)
self.set_label_text("Tea", tea, self.ids.label2)
def set_label_text(self, title, counts, label):
label.text = ""
for item_num, count in counts.items():
label.text += f"{title} {item_num} : {count}\n\n"
class CoffeeWindow(Screen):
clickCounter = {}
def count(self, num):
self.clickCounter.setdefault(num, 0)
self.clickCounter[num] += 1
class TeaWindow(Screen):
clickCounter = {}
def count(self, num):
self.clickCounter.setdefault(num, 0)
self.clickCounter[num] += 1
class MainWindow(Screen):
pass
class WindowManager(ScreenManager):
pass
kv = Builder.load_file('my.kv')
class MainApp(App):
def build(self):
return kv
if __name__ == '__main__':
MainApp().run()