MizunoBrasil

Cadastro de filmes (incompleto)

Jul 22nd, 2023
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.81 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import ttk
  3. import csv
  4. import webbrowser
  5.  
  6. def save_movie():
  7.     movie_name = entry_name.get()
  8.     movie_year = entry_year.get()
  9.     movie_genre = entry_genre.get()
  10.     movie_duration = entry_duration.get()
  11.     movie_synopsis = text_synopsis.get("1.0", tk.END)
  12.     movie_trailer = entry_trailer.get()
  13.  
  14.     with open('filmes.csv', 'a', newline='', encoding='utf-8') as file:
  15.         writer = csv.writer(file)
  16.         writer.writerow([movie_name, movie_year, movie_genre, movie_duration, movie_synopsis, movie_trailer])
  17.  
  18.     # Limpa os campos de entrada após salvar
  19.     entry_name.delete(0, tk.END)
  20.     entry_year.delete(0, tk.END)
  21.     entry_genre.delete(0, tk.END)
  22.     entry_duration.delete(0, tk.END)
  23.     text_synopsis.delete("1.0", tk.END)
  24.     entry_trailer.delete(0, tk.END)
  25.  
  26.     status_label.config(text="Filme cadastrado com sucesso!")
  27.  
  28.     # Atualiza a Treeview com os filmes cadastrados
  29.     update_treeview()
  30.  
  31. def update_treeview(page=1):
  32.     # Limpa todos os itens da Treeview
  33.     tree.delete(*tree.get_children())
  34.  
  35.     # Lê todos os filmes cadastrados a partir do arquivo CSV
  36.     with open('filmes.csv', 'r', newline='', encoding='utf-8') as file:
  37.         reader = csv.reader(file)
  38.         movies = list(reader)
  39.  
  40.     # Calcula o índice inicial e final para a página atual
  41.     items_per_page = 15
  42.     start_index = (page - 1) * items_per_page
  43.     end_index = page * items_per_page
  44.  
  45.     # Insere os filmes da página atual na Treeview
  46.     for movie in movies[start_index:end_index]:
  47.         tree.insert('', 'end', values=movie)
  48.  
  49.     # Atualiza o estado dos botões de navegação
  50.     btn_prev.config(state=tk.NORMAL if page > 1 else tk.DISABLED)
  51.     btn_next.config(state=tk.NORMAL if end_index < len(movies) else tk.DISABLED)
  52.  
  53. def show_movie_info(event):
  54.     selected_item = tree.selection()
  55.     if selected_item:
  56.         movie_name = tree.item(selected_item, 'values')[0]
  57.         movie_year = tree.item(selected_item, 'values')[1]
  58.         movie_genre = tree.item(selected_item, 'values')[2]
  59.         movie_duration = tree.item(selected_item, 'values')[3]
  60.         movie_synopsis = tree.item(selected_item, 'values')[4]
  61.         movie_trailer = tree.item(selected_item, 'values')[5]
  62.  
  63.         info_window = tk.Toplevel(app)
  64.         info_window.title(f"Informações do Filme: {movie_name}")
  65.         info_window.geometry("500x300")
  66.         info_window.resizable(False, False)
  67.  
  68.         scrollbar = tk.Scrollbar(info_window)
  69.         scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
  70.  
  71.         info_text = tk.Text(info_window, wrap=tk.WORD, yscrollcommand=scrollbar.set)
  72.         info_text.insert(tk.END, f"Informações do Filme: {movie_name}\n"
  73.                                  f"Ano de estreia/lancamento: {movie_year}\n"
  74.                                  f"Gênero: {movie_genre}\n"
  75.                                  f"Duração: {movie_duration}\n"
  76.                                  f"\nSinopse:\n{movie_synopsis}\n"
  77.                                  f"\nTrailer: {movie_trailer}\n")
  78.         info_text.config(state=tk.DISABLED)
  79.         info_text.pack(side=tk.LEFT, padx=10, pady=10, fill=tk.BOTH, expand=True)
  80.  
  81.         scrollbar.config(command=info_text.yview)
  82.  
  83.         # Abre o URL do trailer no navegador padrão quando clicado
  84.         def open_trailer(event):
  85.             webbrowser.open_new(movie_trailer)
  86.  
  87.         info_text.tag_configure("link", foreground="blue", underline=True)
  88.         info_text.tag_bind("link", "<Button-1>", open_trailer)
  89.         info_text.tag_bind("link", "<Enter>", lambda event, tag="link": info_text.config(cursor="hand2"))
  90.         info_text.tag_bind("link", "<Leave>", lambda event, tag="link": info_text.config(cursor=""))
  91.  
  92. app = tk.Tk()
  93. app.title("Cadastro de Filmes")
  94.  
  95. # Labels
  96. label_name = tk.Label(app, text="Nome do filme:")
  97. label_year = tk.Label(app, text="Ano de estreia/lancamento:")
  98. label_genre = tk.Label(app, text="Gênero:")
  99. label_duration = tk.Label(app, text="Duração:")
  100. label_synopsis = tk.Label(app, text="Sinopse:")
  101. label_trailer = tk.Label(app, text="Trailer:")
  102.  
  103. label_name.grid(row=0, column=0, padx=5, pady=5)
  104. label_year.grid(row=1, column=0, padx=5, pady=5)
  105. label_genre.grid(row=2, column=0, padx=5, pady=5)
  106. label_duration.grid(row=3, column=0, padx=5, pady=5)
  107. label_synopsis.grid(row=4, column=0, padx=5, pady=5)
  108. label_trailer.grid(row=5, column=0, padx=5, pady=5)
  109.  
  110. # Campos de entrada
  111. entry_name = ttk.Entry(app, width=30)
  112. entry_year = ttk.Entry(app, width=30)
  113. entry_genre = ttk.Entry(app, width=30)
  114. entry_duration = ttk.Entry(app, width=30)
  115. entry_trailer = ttk.Entry(app, width=30)
  116.  
  117. entry_name.grid(row=0, column=1, padx=5, pady=5)
  118. entry_year.grid(row=1, column=1, padx=5, pady=5)
  119. entry_genre.grid(row=2, column=1, padx=5, pady=5)
  120. entry_duration.grid(row=3, column=1, padx=5, pady=5)
  121. entry_trailer.grid(row=5, column=1, padx=5, pady=5)
  122.  
  123. # Campo de texto para sinopse
  124. text_synopsis = tk.Text(app, width=30, height=5)
  125. text_synopsis.grid(row=4, column=1, padx=5, pady=5)
  126.  
  127. # Botão para cadastrar o filme
  128. btn_save = ttk.Button(app, text="Salvar", command=save_movie)
  129. btn_save.grid(row=6, column=0, columnspan=2, padx=5, pady=10)
  130.  
  131. # Label para exibir status
  132. status_label = tk.Label(app, text="")
  133. status_label.grid(row=7, column=0, columnspan=2, padx=5, pady=10)
  134.  
  135. # Adicione o código para criar a árvore (treeview) que exibirá os registros cadastrados
  136. tree = ttk.Treeview(app, columns=("name", "year", "genre", "duration", "synopsis", "trailer"), show="headings")
  137.  
  138. # Configuração das colunas e cabeçalhos da Treeview
  139. tree.heading("name", text="Nome do Filme")
  140. tree.heading("year", text="Ano de Estreia/Lançamento")
  141. tree.heading("genre", text="Gênero")
  142. tree.heading("duration", text="Duração")
  143. tree.heading("synopsis", text="Sinopse")
  144. tree.heading("trailer", text="Trailer")
  145.  
  146. tree.column("name", width=150)
  147. tree.column("year", width=100)
  148. tree.column("genre", width=100)
  149. tree.column("duration", width=100)
  150. tree.column("synopsis", width=200)
  151. tree.column("trailer", width=200)
  152.  
  153. tree.grid(row=8, column=0, columnspan=2, padx=5, pady=5)
  154.  
  155. # Adicione botões de navegação para a paginação
  156. current_page = 1
  157. btn_prev = ttk.Button(app, text="<< Página Anterior", command=lambda: navigate_page(-1))
  158. btn_next = ttk.Button(app, text="Próxima Página >>", command=lambda: navigate_page(1))
  159.  
  160. btn_prev.grid(row=9, column=0, padx=5, pady=10)
  161. btn_next.grid(row=9, column=1, padx=5, pady=10)
  162.  
  163. def navigate_page(direction):
  164.     global current_page
  165.     new_page = current_page + direction
  166.     if new_page >= 1:
  167.         current_page = new_page
  168.         update_treeview(current_page)
  169.  
  170. # Carrega os filmes cadastrados no arquivo CSV para a Treeview na primeira página
  171. update_treeview()
  172.  
  173. # Bind do clique duplo na Treeview para abrir as informações do registro selecionado
  174. tree.bind("<Double-1>", show_movie_info)
  175.  
  176. app.mainloop()
  177.  
Add Comment
Please, Sign In to add comment