Please help me figure it out. I’m beginner in learning Python.
The training program I'm currently working on, counts items sold.
The CoffeeWindow and TeaWindow screens have buttons with items (coffee, tea) that the program counts when pressed.
On the TotalWindow screen, the program should show the amount of selected items and their general price as one order. After pressing the Delete button, the program should delete the contents of this screen in order to show the next new order.
I can't figure out, how I can clear the result of the set_label_text method and run it again after pressing the Delete button. I created a delete_total method that runs after clicking on Delete, in it I change the text label to ‘ ’ and change the price list to empty list, it seems to work, the text is removed from the screen. But when you press any buttons with items, the result is shown the same plus new.
I try different ways but nothing works. What am I doing wrong?
Thanks in advance!
main.py:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
class TotalWindow(Screen):
def on_pre_enter(self, *args):
coffee = self.manager.ids.coffee.clickCounter
tea = self.manager.ids.tea.clickCounter
big_dict = {**coffee, **tea}
self.price_list = []
for item_name, count in big_dict.items():
self.price_list.append(count[0] * count[1])
self.set_label_text(big_dict, self.ids.label1)
def delete_total(self):
self.ids.label1.text = ""
self.price_list = []
def set_label_text(self, counts, label):
label.text = ""
for item_name, count in counts.items():
label.text = f"{item_name} ({count[0]})\n"
label.text = "\nTotal Price: " str(sum(self.price_list))
class CoffeeWindow(Screen):
clickCounter = {}
def count(self, name, price):
self.clickCounter.setdefault(name, [0, price])
self.clickCounter[name][0] = 1
class TeaWindow(Screen):
clickCounter = {}
def count(self, name, price):
self.clickCounter.setdefault(name, [0, price])
self.clickCounter[name][0] = 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()
my.kv:
<ItemButton@Button>:
size_hint: 0.5, 0.12
bold: True
WindowManager:
MainWindow:
id: main
CoffeeWindow:
id: coffee
TeaWindow:
id: tea
TotalWindow:
id: total
<TotalWindow>:
name: "total"
BoxLayout:
orientation: "vertical"
pos_hint: {'x' : 0, 'center_y' : 0.57}
size_hint: 1, 0.88
Label:
id: label1
Button:
size_hint: 0.5, 0.1
pos_hint: {'right' : 0.5, 'y' : 0}
text: "Go to Main"
on_release:
app.root.current = "main_window"
app.root.transition.direction = "right"
Button:
size_hint: 0.5, 0.1
pos_hint: {'right' : 1, 'y' : 0}
text: "Delete"
on_release:
root.delete_total()
<MainWindow>:
name: "main_window"
BoxLayout:
ItemButton:
text: "Go to Coffee"
pos_hint: {'x' : 0, 'center_y' : 0.5}
on_release:
app.root.current = "coffee"
app.root.transition.direction = "left"
ItemButton:
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}
ItemButton:
text: "black coffee"
on_release:
root.count("black coffee", 20)
ItemButton:
text: "milk coffee"
on_release:
root.count("milk coffee", 20)
Button:
size_hint: 0.5, 0.1
text: "Show Total Menu"
on_release:
app.root.current = "total"
app.root.transition.direction = "left"
Button:
size_hint: 0.5, 0.1
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}
ItemButton:
text: "green tea"
on_release:
root.count("green tea", 20)
ItemButton:
text: "black tea"
on_release:
root.count("black tea", 20)
Button:
size_hint: 0.5, 0.1
text: "Show Total Menu"
on_release:
app.root.current = "total"
app.root.transition.direction = "left"
Button:
size_hint: 0.5, 0.1
text: "Go to Main"
on_release:
app.root.current = "main_window"
app.root.transition.direction = "right"
CodePudding user response:
Your delete_total()
method is clearing the text, but not removing the counts in your clickCounters
. You can clear the counts by adding lines to your delete_total()
method:
def delete_total(self):
self.ids.label1.text = ""
self.price_list = []
self.manager.ids.coffee.clickCounter = {}
self.manager.ids.tea.clickCounter = {}