Xom9ik

Course_work/15var (IV semester) .py

Mar 30th, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.43 KB | None | 0 0
  1. #Course_work/15var (IV semester) .py
  2. import tkinter
  3. import tkinter.ttk as ttk
  4. import tkinter.filedialog
  5.  
  6.  
  7. class MusicCD:
  8.     def __init__(self, id, singer, edition, recordsCount):
  9.         self.id = id
  10.         self.singer = singer
  11.         self.edition = edition
  12.         self.recordsCount = recordsCount
  13.  
  14.     def setId(self, id):
  15.         self.id = id
  16.  
  17.     def getId(self):
  18.         return self.id
  19.  
  20.     def setSinger(self, singer):
  21.         self.singer = singer
  22.  
  23.     def getSinger(self):
  24.         return self.singer
  25.  
  26.     def setEdition(self, edition):
  27.         self.edition = edition
  28.  
  29.     def getEdition(self):
  30.         return self.edition
  31.  
  32.     def setRecordsCount(self, recordsCount):
  33.         self.recordsCount = recordsCount
  34.  
  35.     def getRecordsCount(self):
  36.         return self.recordsCount
  37.  
  38.  
  39. def Center(toplevel):
  40.     toplevel.update_idletasks()
  41.     w = toplevel.winfo_screenwidth()
  42.     h = toplevel.winfo_screenheight()
  43.     size = tuple(int(_) for _ in toplevel.geometry().split('+')[0].split('x'))
  44.     x = w / 2 - size[0] / 2
  45.     y = h / 2 - size[1] / 2
  46.     toplevel.geometry("%dx%d+%d+%d" % (size + (x, y)))
  47.  
  48.  
  49. def OnOpen():
  50.     ftypes = [('Python files', '*.txt'), ('All files', '*')]
  51.     dlg = tkinter.filedialog.Open(filetypes=ftypes)
  52.     fl = dlg.show()
  53.     if fl != '':
  54.         # text = readFile(fl)
  55.         print(fl)
  56.         ReadFileText(fl)
  57.  
  58.  
  59. def OnSave():
  60.     file = open(path, 'w')
  61.     result = ''
  62.     for i in range(0, len(arrayMusicCD)):
  63.         if (i < len(arrayMusicCD) - 1):
  64.             result += arrayMusicCD[i].getSinger() + '\t' + str(arrayMusicCD[i].getEdition()) + '\t' + str(
  65.                 arrayMusicCD[i].getRecordsCount()) + '\n'
  66.         else:
  67.             result += arrayMusicCD[i].getSinger() + '\t' + str(arrayMusicCD[i].getEdition()) + '\t' + str(
  68.                 arrayMusicCD[i].getRecordsCount())
  69.         print(arrayMusicCD[i].getRecordsCount())
  70.     file.write(result)
  71.     file.close()
  72.  
  73.  
  74. def ReadFileText(arg):
  75.     global path, screen, root, arrayMusicCD
  76.     path = arg
  77.     file = open(path, 'rt')
  78.     inputFile = []
  79.     arrayMusicCD = []
  80.  
  81.     for line in file:
  82.         row = [i for i in line.split('\t')]
  83.         inputFile.append(row)
  84.     countId = 0
  85.     for line in inputFile:
  86.         countId += 1
  87.         arrayMusicCD.append(MusicCD(countId, line[0], float(line[1]), int(line[2].replace("\n", ""))))
  88.  
  89.     for i in range(0, len(arrayMusicCD)):
  90.         print(arrayMusicCD[i].getSinger())
  91.  
  92.     if (screen == 0):
  93.         screen = TableScreen(root)
  94.         screen.pack(side="top", fill="both", expand=True)
  95.     screen.LoadTable(arrayMusicCD)
  96.     file.close()
  97.  
  98.  
  99. # ---1.2
  100. def AddCD():
  101.     addWindow = tkinter.Tk()
  102.     addWindow.geometry('225x250')
  103.     addWindow.title("Add")
  104.     Center(addWindow)
  105.     global singerEntryBox, editionEntryBox, recordEntryBox
  106.     singerLabel = tkinter.Label(addWindow, text='Enter a Singer')
  107.     singerLabel.pack(side=tkinter.TOP)
  108.     singerEntryBox = tkinter.Entry(addWindow)
  109.     singerEntryBox.focus_set()
  110.     singerEntryBox.pack(side=tkinter.TOP, pady=10, padx=10)
  111.     # 2
  112.     editionLabel = tkinter.Label(addWindow, text='Enter a edition')
  113.     editionLabel.pack(side=tkinter.TOP)
  114.     editionEntryBox = tkinter.Entry(addWindow)
  115.     editionEntryBox.pack(side=tkinter.TOP, pady=10, padx=10)
  116.     # 3
  117.     countRecordLabel = tkinter.Label(addWindow, text='Enter a records')
  118.     countRecordLabel.pack(side=tkinter.TOP)
  119.     recordEntryBox = tkinter.Entry(addWindow)
  120.     recordEntryBox.pack(side=tkinter.TOP, pady=10, padx=10)
  121.  
  122.     mySubmitButton = tkinter.Button(addWindow, text='Add', height=1, width=16, command=AddItem)
  123.     mySubmitButton.pack()
  124.  
  125.  
  126. def AddItem():
  127.     print(singerEntryBox.get())
  128.     print(editionEntryBox.get())
  129.     print(recordEntryBox.get())
  130.     arrayMusicCD.append(
  131.         MusicCD(len(arrayMusicCD) + 1, singerEntryBox.get(), float(editionEntryBox.get()), int(recordEntryBox.get())))
  132.     screen.LoadTable(arrayMusicCD)
  133.  
  134.  
  135. # ---2.3
  136. def InsertCD():
  137.     insertWindow = tkinter.Tk()
  138.     insertWindow.geometry('225x290')
  139.     insertWindow.title("Insert")
  140.     Center(insertWindow)
  141.     global indexEntryBox, singerEntryBox, editionEntryBox, recordEntryBox
  142.  
  143.     indexLabel = tkinter.Label(insertWindow, text='Insert before index [1-' + str(len(arrayMusicCD)) + ']')
  144.     indexLabel.pack(side=tkinter.TOP)
  145.     indexEntryBox = tkinter.Entry(insertWindow)
  146.     indexEntryBox.focus_set()
  147.     indexEntryBox.pack(side=tkinter.TOP, pady=10, padx=10)
  148.     # 1
  149.     singerLabel = tkinter.Label(insertWindow, text='Enter a Singer')
  150.     singerLabel.pack(side=tkinter.TOP)
  151.     singerEntryBox = tkinter.Entry(insertWindow)
  152.     singerEntryBox.pack(side=tkinter.TOP, pady=10, padx=10)
  153.     # 2
  154.     editionLabel = tkinter.Label(insertWindow, text='Enter a edition')
  155.     editionLabel.pack(side=tkinter.TOP)
  156.     editionEntryBox = tkinter.Entry(insertWindow)
  157.     editionEntryBox.pack(side=tkinter.TOP, pady=10, padx=10)
  158.     # 3
  159.     countRecordLabel = tkinter.Label(insertWindow, text='Enter a records')
  160.     countRecordLabel.pack(side=tkinter.TOP)
  161.     recordEntryBox = tkinter.Entry(insertWindow)
  162.     recordEntryBox.pack(side=tkinter.TOP, pady=10, padx=10)
  163.  
  164.     mySubmitButton = tkinter.Button(insertWindow, text='Insert', height=1, width=16, command=InsertItem)
  165.     mySubmitButton.pack()
  166.  
  167.  
  168. def InsertItem():
  169.     print(indexEntryBox.get())
  170.     print(singerEntryBox.get())
  171.     print(editionEntryBox.get())
  172.     print(recordEntryBox.get())
  173.     if (0 <= int(indexEntryBox.get()) - 1 < len(arrayMusicCD)):
  174.         arrayMusicCD.insert(int(indexEntryBox.get()) - 1,
  175.                             MusicCD(int(indexEntryBox.get()), singerEntryBox.get(), float(editionEntryBox.get()),
  176.                                     int(recordEntryBox.get())))
  177.         for i in range(len(arrayMusicCD)):
  178.             arrayMusicCD[i].setId(i + 1)
  179.         screen.LoadTable(arrayMusicCD)
  180.     else:
  181.         print('Invalid index')
  182.  
  183.  
  184. # ---2.6
  185. def DeleteCD():
  186.     deleteWindow = tkinter.Tk()
  187.     deleteWindow.geometry('225x100')
  188.     deleteWindow.title("Delete")
  189.     Center(deleteWindow)
  190.     global indexEntryBox
  191.     indexLabel = tkinter.Label(deleteWindow, text='Insert before index [1-' + str(len(arrayMusicCD)) + ']')
  192.     indexLabel.pack(side=tkinter.TOP)
  193.     indexEntryBox = tkinter.Entry(deleteWindow)
  194.     indexEntryBox.focus_set()
  195.     indexEntryBox.pack(side=tkinter.TOP, pady=10, padx=10)
  196.  
  197.     mySubmitButton = tkinter.Button(deleteWindow, text='Delete', height=1, width=16, command=DeleteItem)
  198.     mySubmitButton.pack()
  199.  
  200.  
  201. def DeleteItem():
  202.     global arrayMusicCD
  203.     if (0 <= int(indexEntryBox.get()) - 1 < len(arrayMusicCD)):
  204.         index = int(indexEntryBox.get()) - 1
  205.         arrayMusicCD = arrayMusicCD[:index]
  206.         screen.LoadTable(arrayMusicCD)
  207.     else:
  208.         print('Invalid index')
  209.     screen.LoadTable(arrayMusicCD)
  210.  
  211.  
  212. # ---2.8
  213. def SortCD():
  214.     sortWindow = tkinter.Tk()
  215.     sortWindow.geometry('225x250')
  216.     sortWindow.title("Sort")
  217.     Center(sortWindow)
  218.     global indexFromEntryBox, indexToEntryBox, variableSortBy, variableSortReverse
  219.     # from
  220.     indexFromLabel = tkinter.Label(sortWindow, text='Index from [1-' + str(len(arrayMusicCD)) + ']')
  221.     indexFromLabel.pack(side=tkinter.TOP)
  222.     indexFromEntryBox = tkinter.Entry(sortWindow)
  223.     indexFromEntryBox.focus_set()
  224.     indexFromEntryBox.pack(side=tkinter.TOP, pady=10, padx=10)
  225.     # to
  226.     indexToLabel = tkinter.Label(sortWindow, text='Index to [1-' + str(len(arrayMusicCD)) + ']')
  227.     indexToLabel.pack(side=tkinter.TOP)
  228.     indexToEntryBox = tkinter.Entry(sortWindow)
  229.     indexToEntryBox.pack(side=tkinter.TOP, pady=10, padx=10)
  230.     # sortBy
  231.     sortByLabel = tkinter.Label(sortWindow, text='Sort by')
  232.     sortByLabel.pack(side=tkinter.TOP)
  233.     variableSortBy = tkinter.StringVar(sortWindow)
  234.     variableSortBy.set("Id")  # default value
  235.     sortOptionMenu = tkinter.OptionMenu(sortWindow, variableSortBy, "Id", "Singer", "Edition", "Record count")
  236.     sortOptionMenu.pack(side=tkinter.TOP)
  237.     # reverse
  238.     reverseLabel = tkinter.Label(sortWindow, text='Reverse sort')
  239.     reverseLabel.pack(side=tkinter.TOP)
  240.     variableSortReverse = tkinter.IntVar(sortWindow)
  241.     variableSortReverse.set(0)  # default value
  242.     checkbutton = tkinter.Checkbutton(sortWindow, variable=variableSortReverse)
  243.     checkbutton.pack(side=tkinter.TOP)
  244.     # button
  245.     mySubmitButton = tkinter.Button(sortWindow, text='Sort', height=1, width=16, command=SortItem)
  246.     mySubmitButton.pack()
  247.  
  248.  
  249. def SortItem():
  250.     global arrayMusicCD
  251.     print(indexFromEntryBox.get())
  252.     print(indexToEntryBox.get())
  253.     print(variableSortBy.get())
  254.     print(variableSortReverse.get())
  255.     if ((0 <= int(indexFromEntryBox.get()) - 1 < len(arrayMusicCD))
  256.         and (0 <= int(indexToEntryBox.get()) - 1 < len(arrayMusicCD))
  257.         and (int(indexFromEntryBox.get()) - 1 < int(indexToEntryBox.get()) - 1)):
  258.  
  259.         sortArrayCD = arrayMusicCD[int(indexFromEntryBox.get()) - 1:int(indexToEntryBox.get())]
  260.  
  261.         if (variableSortBy.get() == 'Id'):
  262.             sortArrayCD = sorted(sortArrayCD, key=lambda x: x.getId(), reverse=variableSortReverse.get())
  263.         elif (variableSortBy.get() == 'Singer'):
  264.             sortArrayCD = sorted(sortArrayCD, key=lambda x: x.getSinger().lower(), reverse=variableSortReverse.get())
  265.         elif (variableSortBy.get() == 'Edition'):
  266.             sortArrayCD = sorted(sortArrayCD, key=lambda x: x.getEdition(), reverse=variableSortReverse.get())
  267.         elif (variableSortBy.get() == 'Record count'):
  268.             sortArrayCD = sorted(sortArrayCD, key=lambda x: x.getRecordsCount(), reverse=variableSortReverse.get())
  269.  
  270.         for i in range(len(sortArrayCD)):
  271.             print(sortArrayCD[i].getSinger(), sortArrayCD[i].getEdition(), sortArrayCD[i].getRecordsCount())
  272.         screen.LoadTable(sortArrayCD)
  273.     else:
  274.         print('Invalid index')
  275.  
  276.  
  277. class TableScreen(tkinter.Frame):
  278.     def __init__(self, parent):
  279.         ttk.Frame.__init__(self, parent)
  280.         if (tableIsCreated == 0):
  281.             self.CreateUI()
  282.         self.LoadTable(arrayMusicCD)
  283.         parent.grid_rowconfigure(0, weight=1)
  284.         parent.grid_columnconfigure(0, weight=1)
  285.  
  286.     def CreateUI(self):
  287.         tv = ttk.Treeview(self)
  288.         tv['columns'] = ('singer', 'edition', 'recordsCount')
  289.         tv.heading("#0", text='Id')
  290.         tv.column("#0", width=1)
  291.         tv.heading('singer', text='Singer')
  292.         tv.column('singer', anchor='center', width=100)
  293.         tv.heading('edition', text='Edition')
  294.         tv.column('edition', anchor='center', width=100)
  295.         tv.heading('recordsCount', text='Records count')
  296.         tv.column('recordsCount', anchor='center', width=100)
  297.         tv.grid(sticky=(tkinter.N, tkinter.S, tkinter.W, tkinter.E))
  298.         self.treeview = tv
  299.         self.grid_rowconfigure(0, weight=1)
  300.         self.grid_columnconfigure(0, weight=1)
  301.         global tableIsCreated
  302.         tableIsCreated = 1
  303.  
  304.     def LoadTable(self, arr):
  305.         for i in self.treeview.get_children():
  306.             self.treeview.delete(i)
  307.         for i in range(len(arr)):
  308.             self.treeview.insert('', 'end', text=str(arr[i].getId()),
  309.                                  values=(arr[i].getSinger(), arr[i].getEdition(), arr[i].getRecordsCount()))
  310.  
  311.  
  312. def main():
  313.     global root, arrayMusicCD, tableIsCreated, screen
  314.     root = tkinter.Tk()
  315.     root.geometry('750x500')
  316.     root.title("Music CD")
  317.  
  318.     arrayMusicCD = []
  319.     tableIsCreated = 0
  320.     screen = 0
  321.  
  322.     menubar = tkinter.Menu(root)
  323.  
  324.     filemenu = tkinter.Menu(menubar, tearoff=0)
  325.     filemenu.add_command(label="Open", command=OnOpen)
  326.     filemenu.add_command(label="Save", command=OnSave)
  327.     filemenu.add_separator()
  328.     filemenu.add_command(label="Exit", command=root.quit)
  329.  
  330.     editmenu = tkinter.Menu(menubar, tearoff=0)
  331.     editmenu.add_command(label="Add CD", command=AddCD)
  332.     editmenu.add_command(label="Insert", command=InsertCD)
  333.     editmenu.add_command(label="Delete", command=DeleteCD)
  334.  
  335.     menubar.add_cascade(label="File", menu=filemenu)
  336.     menubar.add_cascade(label="Edit", menu=editmenu)
  337.     menubar.add_command(label="Sort", command=SortCD)
  338.     root.config(menu=menubar)
  339.  
  340.     Center(root)
  341.     root.mainloop()
  342.  
  343.  
  344. if __name__ == "__main__":
  345.     main()
  346.  
  347. #Example database.txt
  348. Imagine Dragons 23.6    545
  349. BRUTTO  102.3   98
  350. NEFFEX  302.44  193
  351. Skillet 204.6   304
  352. Green Day   66.7    345
  353. Beartooth   23.554  45
  354. Drowning Pool   54.66   236
  355. Our Last Night  468.6325    45
  356. Project Vela    2   67
  357. The Siege   123 2354
  358. Annisokay   54.342  4
  359. Black Veil Brides   2.3 5
Advertisement
Add Comment
Please, Sign In to add comment