Еще попутный вопрос, можно ли как-то переработать мой код по правилам ООП, с классами, объектами?
import collections
from tkinter import *
import tkinter.ttk
other = collections.OrderedDict(
[
("aa", "0.2"),
("bb", "0.435"),
("cc", "0.275"),
("dd", "7"),
("ee", "1")
])
anchor = collections.OrderedDict(
[
('a', '1.6'),
('b', '2.45'),
('c', '3'),
('d', '3.9'),
('e', '1'),
('f', '1'),
('g', '1')
('h','1.1'),
('i', '1')
])
plate = collections.OrderedDict(
[
("aa", "0.314"),
("bb", "0.39"),
("cc", "1.427")
])
category = collections.OrderedDict()
category["Анкера"]=anchor
category["Шайба"]=plate
category["Прочее"]=other
summ = {}
root = Tk()
root.title("Расчет веса")
tree = tkinter.ttk.Treeview(root)
tree["columns"]=("c","a","b")
tree.column("c", width=100 )
tree.column("a", width=100 )
tree.column("b", width=100)
tree.heading("c", text="Длина")
tree.heading("a", text="Количество")
tree.heading("b", text="Вес, кг")
tree.grid(row=3, columnspan=2)
lbl = Label(root, text = 'Количество')
enterFieldQ = Entry(root,width=10)
enterFieldQ.grid(row=2,column=1)
lbl.grid(row=1, column=0)
lbl1 = Label(root, text = 'Длина')
enterField = Entry(root,width=10)
enterField.grid(row=1,column=1)
lbl1.grid(row=2, column=0)
lblRes = Label(root, text="Итого:")
lblRes.grid(row=4, column=0)
lblRes1 = Label(root, text="")
lblRes1.grid(row=4, column=1)
categoryList = Listbox(root, width = 15)
categoryList.grid(row=0, column=0)
for i in category:
categoryList.insert(END, i)
categorySelected = ""
totalW = 0
x=0
def addToTree(evt):
global x
global categorySelected
global totalW
global item
print ("Pressed enter")
quantity = enterField.get()
length = enterFieldQ.get()
if length == "":
length = 1
if quantity != "":
weight = int(quantity)*float(category[categorySelected][item])
weight = weight * float(length)
weight = round(weight,2)
tree.insert("" , x, text=item, values=(length,quantity,weight))
totalW = totalW + weight
totalW = round(totalW,2)
lblRes1.configure(text=totalW)
enterFieldQ.delete(0,END)
enterField.delete(0,END)
x=x+1
def onSelectCategory(evt):
print("Category selected")
global categorySelected
itemsCounter = productList.size()
if itemsCounter > 0:
productList.delete(0, END)
categorySelected = categoryList.get(categoryList.curselection())
tableFill = {}
if categorySelected == "Анкера":
tableFill = anchor
elif categorySelected == "Шайба":
tableFill = plate
elif categorySelected == "Прочее":
tableFill = other
for i in tableFill:
productList.insert(END, i)
item=""
def onSelectItem(evt):
print ("Item selected")
global item
item = productList.get(productList.curselection())
def doubleClick(evt):
global totalW
print("doubleClick")
if totalW > 0:
selected_item = tree.selection()[0]
print (selected_item)
curItem = tree.focus()
minusWeight = float(tree.item(curItem)["values"][2])
totalW = totalW - minusWeight
totalW = round(totalW,2)
lblRes1.configure(text=totalW)
tree.delete(selected_item)
productList = Listbox(root, width = 30)
productList.grid(row=0, column=1)
productList.bind('<<ListboxSelect>>', onSelectItem)
categoryList.bind('<<ListboxSelect>>', onSelectCategory)
enterField.bind("<Return>", addToTree)
enterFieldQ.bind("<Return>", addToTree)
tree.bind("<Double-1>", doubleClick)
root.mainloop()