Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import ttk
- import csv
- import webbrowser
- def save_movie():
- movie_name = entry_name.get()
- movie_year = entry_year.get()
- movie_genre = entry_genre.get()
- movie_duration = entry_duration.get()
- movie_synopsis = text_synopsis.get("1.0", tk.END)
- movie_trailer = entry_trailer.get()
- with open('filmes.csv', 'a', newline='', encoding='utf-8') as file:
- writer = csv.writer(file)
- writer.writerow([movie_name, movie_year, movie_genre, movie_duration, movie_synopsis, movie_trailer])
- # Limpa os campos de entrada após salvar
- entry_name.delete(0, tk.END)
- entry_year.delete(0, tk.END)
- entry_genre.delete(0, tk.END)
- entry_duration.delete(0, tk.END)
- text_synopsis.delete("1.0", tk.END)
- entry_trailer.delete(0, tk.END)
- status_label.config(text="Filme cadastrado com sucesso!")
- # Atualiza a Treeview com os filmes cadastrados
- update_treeview()
- def update_treeview(page=1):
- # Limpa todos os itens da Treeview
- tree.delete(*tree.get_children())
- # Lê todos os filmes cadastrados a partir do arquivo CSV
- with open('filmes.csv', 'r', newline='', encoding='utf-8') as file:
- reader = csv.reader(file)
- movies = list(reader)
- # Calcula o índice inicial e final para a página atual
- items_per_page = 15
- start_index = (page - 1) * items_per_page
- end_index = page * items_per_page
- # Insere os filmes da página atual na Treeview
- for movie in movies[start_index:end_index]:
- tree.insert('', 'end', values=movie)
- # Atualiza o estado dos botões de navegação
- btn_prev.config(state=tk.NORMAL if page > 1 else tk.DISABLED)
- btn_next.config(state=tk.NORMAL if end_index < len(movies) else tk.DISABLED)
- def show_movie_info(event):
- selected_item = tree.selection()
- if selected_item:
- movie_name = tree.item(selected_item, 'values')[0]
- movie_year = tree.item(selected_item, 'values')[1]
- movie_genre = tree.item(selected_item, 'values')[2]
- movie_duration = tree.item(selected_item, 'values')[3]
- movie_synopsis = tree.item(selected_item, 'values')[4]
- movie_trailer = tree.item(selected_item, 'values')[5]
- info_window = tk.Toplevel(app)
- info_window.title(f"Informações do Filme: {movie_name}")
- info_window.geometry("500x300")
- info_window.resizable(False, False)
- scrollbar = tk.Scrollbar(info_window)
- scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
- info_text = tk.Text(info_window, wrap=tk.WORD, yscrollcommand=scrollbar.set)
- info_text.insert(tk.END, f"Informações do Filme: {movie_name}\n"
- f"Ano de estreia/lancamento: {movie_year}\n"
- f"Gênero: {movie_genre}\n"
- f"Duração: {movie_duration}\n"
- f"\nSinopse:\n{movie_synopsis}\n"
- f"\nTrailer: {movie_trailer}\n")
- info_text.config(state=tk.DISABLED)
- info_text.pack(side=tk.LEFT, padx=10, pady=10, fill=tk.BOTH, expand=True)
- scrollbar.config(command=info_text.yview)
- # Abre o URL do trailer no navegador padrão quando clicado
- def open_trailer(event):
- webbrowser.open_new(movie_trailer)
- info_text.tag_configure("link", foreground="blue", underline=True)
- info_text.tag_bind("link", "<Button-1>", open_trailer)
- info_text.tag_bind("link", "<Enter>", lambda event, tag="link": info_text.config(cursor="hand2"))
- info_text.tag_bind("link", "<Leave>", lambda event, tag="link": info_text.config(cursor=""))
- app = tk.Tk()
- app.title("Cadastro de Filmes")
- # Labels
- label_name = tk.Label(app, text="Nome do filme:")
- label_year = tk.Label(app, text="Ano de estreia/lancamento:")
- label_genre = tk.Label(app, text="Gênero:")
- label_duration = tk.Label(app, text="Duração:")
- label_synopsis = tk.Label(app, text="Sinopse:")
- label_trailer = tk.Label(app, text="Trailer:")
- label_name.grid(row=0, column=0, padx=5, pady=5)
- label_year.grid(row=1, column=0, padx=5, pady=5)
- label_genre.grid(row=2, column=0, padx=5, pady=5)
- label_duration.grid(row=3, column=0, padx=5, pady=5)
- label_synopsis.grid(row=4, column=0, padx=5, pady=5)
- label_trailer.grid(row=5, column=0, padx=5, pady=5)
- # Campos de entrada
- entry_name = ttk.Entry(app, width=30)
- entry_year = ttk.Entry(app, width=30)
- entry_genre = ttk.Entry(app, width=30)
- entry_duration = ttk.Entry(app, width=30)
- entry_trailer = ttk.Entry(app, width=30)
- entry_name.grid(row=0, column=1, padx=5, pady=5)
- entry_year.grid(row=1, column=1, padx=5, pady=5)
- entry_genre.grid(row=2, column=1, padx=5, pady=5)
- entry_duration.grid(row=3, column=1, padx=5, pady=5)
- entry_trailer.grid(row=5, column=1, padx=5, pady=5)
- # Campo de texto para sinopse
- text_synopsis = tk.Text(app, width=30, height=5)
- text_synopsis.grid(row=4, column=1, padx=5, pady=5)
- # Botão para cadastrar o filme
- btn_save = ttk.Button(app, text="Salvar", command=save_movie)
- btn_save.grid(row=6, column=0, columnspan=2, padx=5, pady=10)
- # Label para exibir status
- status_label = tk.Label(app, text="")
- status_label.grid(row=7, column=0, columnspan=2, padx=5, pady=10)
- # Adicione o código para criar a árvore (treeview) que exibirá os registros cadastrados
- tree = ttk.Treeview(app, columns=("name", "year", "genre", "duration", "synopsis", "trailer"), show="headings")
- # Configuração das colunas e cabeçalhos da Treeview
- tree.heading("name", text="Nome do Filme")
- tree.heading("year", text="Ano de Estreia/Lançamento")
- tree.heading("genre", text="Gênero")
- tree.heading("duration", text="Duração")
- tree.heading("synopsis", text="Sinopse")
- tree.heading("trailer", text="Trailer")
- tree.column("name", width=150)
- tree.column("year", width=100)
- tree.column("genre", width=100)
- tree.column("duration", width=100)
- tree.column("synopsis", width=200)
- tree.column("trailer", width=200)
- tree.grid(row=8, column=0, columnspan=2, padx=5, pady=5)
- # Adicione botões de navegação para a paginação
- current_page = 1
- btn_prev = ttk.Button(app, text="<< Página Anterior", command=lambda: navigate_page(-1))
- btn_next = ttk.Button(app, text="Próxima Página >>", command=lambda: navigate_page(1))
- btn_prev.grid(row=9, column=0, padx=5, pady=10)
- btn_next.grid(row=9, column=1, padx=5, pady=10)
- def navigate_page(direction):
- global current_page
- new_page = current_page + direction
- if new_page >= 1:
- current_page = new_page
- update_treeview(current_page)
- # Carrega os filmes cadastrados no arquivo CSV para a Treeview na primeira página
- update_treeview()
- # Bind do clique duplo na Treeview para abrir as informações do registro selecionado
- tree.bind("<Double-1>", show_movie_info)
- app.mainloop()
Add Comment
Please, Sign In to add comment