Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Course_work/15var (IV semester) .py
- import tkinter
- import tkinter.ttk as ttk
- import tkinter.filedialog
- class MusicCD:
- def __init__(self, id, singer, edition, recordsCount):
- self.id = id
- self.singer = singer
- self.edition = edition
- self.recordsCount = recordsCount
- def setId(self, id):
- self.id = id
- def getId(self):
- return self.id
- def setSinger(self, singer):
- self.singer = singer
- def getSinger(self):
- return self.singer
- def setEdition(self, edition):
- self.edition = edition
- def getEdition(self):
- return self.edition
- def setRecordsCount(self, recordsCount):
- self.recordsCount = recordsCount
- def getRecordsCount(self):
- return self.recordsCount
- def Center(toplevel):
- toplevel.update_idletasks()
- w = toplevel.winfo_screenwidth()
- h = toplevel.winfo_screenheight()
- size = tuple(int(_) for _ in toplevel.geometry().split('+')[0].split('x'))
- x = w / 2 - size[0] / 2
- y = h / 2 - size[1] / 2
- toplevel.geometry("%dx%d+%d+%d" % (size + (x, y)))
- def OnOpen():
- ftypes = [('Python files', '*.txt'), ('All files', '*')]
- dlg = tkinter.filedialog.Open(filetypes=ftypes)
- fl = dlg.show()
- if fl != '':
- # text = readFile(fl)
- print(fl)
- ReadFileText(fl)
- def OnSave():
- file = open(path, 'w')
- result = ''
- for i in range(0, len(arrayMusicCD)):
- if (i < len(arrayMusicCD) - 1):
- result += arrayMusicCD[i].getSinger() + '\t' + str(arrayMusicCD[i].getEdition()) + '\t' + str(
- arrayMusicCD[i].getRecordsCount()) + '\n'
- else:
- result += arrayMusicCD[i].getSinger() + '\t' + str(arrayMusicCD[i].getEdition()) + '\t' + str(
- arrayMusicCD[i].getRecordsCount())
- print(arrayMusicCD[i].getRecordsCount())
- file.write(result)
- file.close()
- def ReadFileText(arg):
- global path, screen, root, arrayMusicCD
- path = arg
- file = open(path, 'rt')
- inputFile = []
- arrayMusicCD = []
- for line in file:
- row = [i for i in line.split('\t')]
- inputFile.append(row)
- countId = 0
- for line in inputFile:
- countId += 1
- arrayMusicCD.append(MusicCD(countId, line[0], float(line[1]), int(line[2].replace("\n", ""))))
- for i in range(0, len(arrayMusicCD)):
- print(arrayMusicCD[i].getSinger())
- if (screen == 0):
- screen = TableScreen(root)
- screen.pack(side="top", fill="both", expand=True)
- screen.LoadTable(arrayMusicCD)
- file.close()
- # ---1.2
- def AddCD():
- addWindow = tkinter.Tk()
- addWindow.geometry('225x250')
- addWindow.title("Add")
- Center(addWindow)
- global singerEntryBox, editionEntryBox, recordEntryBox
- singerLabel = tkinter.Label(addWindow, text='Enter a Singer')
- singerLabel.pack(side=tkinter.TOP)
- singerEntryBox = tkinter.Entry(addWindow)
- singerEntryBox.focus_set()
- singerEntryBox.pack(side=tkinter.TOP, pady=10, padx=10)
- # 2
- editionLabel = tkinter.Label(addWindow, text='Enter a edition')
- editionLabel.pack(side=tkinter.TOP)
- editionEntryBox = tkinter.Entry(addWindow)
- editionEntryBox.pack(side=tkinter.TOP, pady=10, padx=10)
- # 3
- countRecordLabel = tkinter.Label(addWindow, text='Enter a records')
- countRecordLabel.pack(side=tkinter.TOP)
- recordEntryBox = tkinter.Entry(addWindow)
- recordEntryBox.pack(side=tkinter.TOP, pady=10, padx=10)
- mySubmitButton = tkinter.Button(addWindow, text='Add', height=1, width=16, command=AddItem)
- mySubmitButton.pack()
- def AddItem():
- print(singerEntryBox.get())
- print(editionEntryBox.get())
- print(recordEntryBox.get())
- arrayMusicCD.append(
- MusicCD(len(arrayMusicCD) + 1, singerEntryBox.get(), float(editionEntryBox.get()), int(recordEntryBox.get())))
- screen.LoadTable(arrayMusicCD)
- # ---2.3
- def InsertCD():
- insertWindow = tkinter.Tk()
- insertWindow.geometry('225x290')
- insertWindow.title("Insert")
- Center(insertWindow)
- global indexEntryBox, singerEntryBox, editionEntryBox, recordEntryBox
- indexLabel = tkinter.Label(insertWindow, text='Insert before index [1-' + str(len(arrayMusicCD)) + ']')
- indexLabel.pack(side=tkinter.TOP)
- indexEntryBox = tkinter.Entry(insertWindow)
- indexEntryBox.focus_set()
- indexEntryBox.pack(side=tkinter.TOP, pady=10, padx=10)
- # 1
- singerLabel = tkinter.Label(insertWindow, text='Enter a Singer')
- singerLabel.pack(side=tkinter.TOP)
- singerEntryBox = tkinter.Entry(insertWindow)
- singerEntryBox.pack(side=tkinter.TOP, pady=10, padx=10)
- # 2
- editionLabel = tkinter.Label(insertWindow, text='Enter a edition')
- editionLabel.pack(side=tkinter.TOP)
- editionEntryBox = tkinter.Entry(insertWindow)
- editionEntryBox.pack(side=tkinter.TOP, pady=10, padx=10)
- # 3
- countRecordLabel = tkinter.Label(insertWindow, text='Enter a records')
- countRecordLabel.pack(side=tkinter.TOP)
- recordEntryBox = tkinter.Entry(insertWindow)
- recordEntryBox.pack(side=tkinter.TOP, pady=10, padx=10)
- mySubmitButton = tkinter.Button(insertWindow, text='Insert', height=1, width=16, command=InsertItem)
- mySubmitButton.pack()
- def InsertItem():
- print(indexEntryBox.get())
- print(singerEntryBox.get())
- print(editionEntryBox.get())
- print(recordEntryBox.get())
- if (0 <= int(indexEntryBox.get()) - 1 < len(arrayMusicCD)):
- arrayMusicCD.insert(int(indexEntryBox.get()) - 1,
- MusicCD(int(indexEntryBox.get()), singerEntryBox.get(), float(editionEntryBox.get()),
- int(recordEntryBox.get())))
- for i in range(len(arrayMusicCD)):
- arrayMusicCD[i].setId(i + 1)
- screen.LoadTable(arrayMusicCD)
- else:
- print('Invalid index')
- # ---2.6
- def DeleteCD():
- deleteWindow = tkinter.Tk()
- deleteWindow.geometry('225x100')
- deleteWindow.title("Delete")
- Center(deleteWindow)
- global indexEntryBox
- indexLabel = tkinter.Label(deleteWindow, text='Insert before index [1-' + str(len(arrayMusicCD)) + ']')
- indexLabel.pack(side=tkinter.TOP)
- indexEntryBox = tkinter.Entry(deleteWindow)
- indexEntryBox.focus_set()
- indexEntryBox.pack(side=tkinter.TOP, pady=10, padx=10)
- mySubmitButton = tkinter.Button(deleteWindow, text='Delete', height=1, width=16, command=DeleteItem)
- mySubmitButton.pack()
- def DeleteItem():
- global arrayMusicCD
- if (0 <= int(indexEntryBox.get()) - 1 < len(arrayMusicCD)):
- index = int(indexEntryBox.get()) - 1
- arrayMusicCD = arrayMusicCD[:index]
- screen.LoadTable(arrayMusicCD)
- else:
- print('Invalid index')
- screen.LoadTable(arrayMusicCD)
- # ---2.8
- def SortCD():
- sortWindow = tkinter.Tk()
- sortWindow.geometry('225x250')
- sortWindow.title("Sort")
- Center(sortWindow)
- global indexFromEntryBox, indexToEntryBox, variableSortBy, variableSortReverse
- # from
- indexFromLabel = tkinter.Label(sortWindow, text='Index from [1-' + str(len(arrayMusicCD)) + ']')
- indexFromLabel.pack(side=tkinter.TOP)
- indexFromEntryBox = tkinter.Entry(sortWindow)
- indexFromEntryBox.focus_set()
- indexFromEntryBox.pack(side=tkinter.TOP, pady=10, padx=10)
- # to
- indexToLabel = tkinter.Label(sortWindow, text='Index to [1-' + str(len(arrayMusicCD)) + ']')
- indexToLabel.pack(side=tkinter.TOP)
- indexToEntryBox = tkinter.Entry(sortWindow)
- indexToEntryBox.pack(side=tkinter.TOP, pady=10, padx=10)
- # sortBy
- sortByLabel = tkinter.Label(sortWindow, text='Sort by')
- sortByLabel.pack(side=tkinter.TOP)
- variableSortBy = tkinter.StringVar(sortWindow)
- variableSortBy.set("Id") # default value
- sortOptionMenu = tkinter.OptionMenu(sortWindow, variableSortBy, "Id", "Singer", "Edition", "Record count")
- sortOptionMenu.pack(side=tkinter.TOP)
- # reverse
- reverseLabel = tkinter.Label(sortWindow, text='Reverse sort')
- reverseLabel.pack(side=tkinter.TOP)
- variableSortReverse = tkinter.IntVar(sortWindow)
- variableSortReverse.set(0) # default value
- checkbutton = tkinter.Checkbutton(sortWindow, variable=variableSortReverse)
- checkbutton.pack(side=tkinter.TOP)
- # button
- mySubmitButton = tkinter.Button(sortWindow, text='Sort', height=1, width=16, command=SortItem)
- mySubmitButton.pack()
- def SortItem():
- global arrayMusicCD
- print(indexFromEntryBox.get())
- print(indexToEntryBox.get())
- print(variableSortBy.get())
- print(variableSortReverse.get())
- if ((0 <= int(indexFromEntryBox.get()) - 1 < len(arrayMusicCD))
- and (0 <= int(indexToEntryBox.get()) - 1 < len(arrayMusicCD))
- and (int(indexFromEntryBox.get()) - 1 < int(indexToEntryBox.get()) - 1)):
- sortArrayCD = arrayMusicCD[int(indexFromEntryBox.get()) - 1:int(indexToEntryBox.get())]
- if (variableSortBy.get() == 'Id'):
- sortArrayCD = sorted(sortArrayCD, key=lambda x: x.getId(), reverse=variableSortReverse.get())
- elif (variableSortBy.get() == 'Singer'):
- sortArrayCD = sorted(sortArrayCD, key=lambda x: x.getSinger().lower(), reverse=variableSortReverse.get())
- elif (variableSortBy.get() == 'Edition'):
- sortArrayCD = sorted(sortArrayCD, key=lambda x: x.getEdition(), reverse=variableSortReverse.get())
- elif (variableSortBy.get() == 'Record count'):
- sortArrayCD = sorted(sortArrayCD, key=lambda x: x.getRecordsCount(), reverse=variableSortReverse.get())
- for i in range(len(sortArrayCD)):
- print(sortArrayCD[i].getSinger(), sortArrayCD[i].getEdition(), sortArrayCD[i].getRecordsCount())
- screen.LoadTable(sortArrayCD)
- else:
- print('Invalid index')
- class TableScreen(tkinter.Frame):
- def __init__(self, parent):
- ttk.Frame.__init__(self, parent)
- if (tableIsCreated == 0):
- self.CreateUI()
- self.LoadTable(arrayMusicCD)
- parent.grid_rowconfigure(0, weight=1)
- parent.grid_columnconfigure(0, weight=1)
- def CreateUI(self):
- tv = ttk.Treeview(self)
- tv['columns'] = ('singer', 'edition', 'recordsCount')
- tv.heading("#0", text='Id')
- tv.column("#0", width=1)
- tv.heading('singer', text='Singer')
- tv.column('singer', anchor='center', width=100)
- tv.heading('edition', text='Edition')
- tv.column('edition', anchor='center', width=100)
- tv.heading('recordsCount', text='Records count')
- tv.column('recordsCount', anchor='center', width=100)
- tv.grid(sticky=(tkinter.N, tkinter.S, tkinter.W, tkinter.E))
- self.treeview = tv
- self.grid_rowconfigure(0, weight=1)
- self.grid_columnconfigure(0, weight=1)
- global tableIsCreated
- tableIsCreated = 1
- def LoadTable(self, arr):
- for i in self.treeview.get_children():
- self.treeview.delete(i)
- for i in range(len(arr)):
- self.treeview.insert('', 'end', text=str(arr[i].getId()),
- values=(arr[i].getSinger(), arr[i].getEdition(), arr[i].getRecordsCount()))
- def main():
- global root, arrayMusicCD, tableIsCreated, screen
- root = tkinter.Tk()
- root.geometry('750x500')
- root.title("Music CD")
- arrayMusicCD = []
- tableIsCreated = 0
- screen = 0
- menubar = tkinter.Menu(root)
- filemenu = tkinter.Menu(menubar, tearoff=0)
- filemenu.add_command(label="Open", command=OnOpen)
- filemenu.add_command(label="Save", command=OnSave)
- filemenu.add_separator()
- filemenu.add_command(label="Exit", command=root.quit)
- editmenu = tkinter.Menu(menubar, tearoff=0)
- editmenu.add_command(label="Add CD", command=AddCD)
- editmenu.add_command(label="Insert", command=InsertCD)
- editmenu.add_command(label="Delete", command=DeleteCD)
- menubar.add_cascade(label="File", menu=filemenu)
- menubar.add_cascade(label="Edit", menu=editmenu)
- menubar.add_command(label="Sort", command=SortCD)
- root.config(menu=menubar)
- Center(root)
- root.mainloop()
- if __name__ == "__main__":
- main()
- #Example database.txt
- Imagine Dragons 23.6 545
- BRUTTO 102.3 98
- NEFFEX 302.44 193
- Skillet 204.6 304
- Green Day 66.7 345
- Beartooth 23.554 45
- Drowning Pool 54.66 236
- Our Last Night 468.6325 45
- Project Vela 2 67
- The Siege 123 2354
- Annisokay 54.342 4
- Black Veil Brides 2.3 5
Advertisement
Add Comment
Please, Sign In to add comment