Python Code om voor Pokemon Candy toe te voegen en te machten deze gaan naar een kladblok de matches, apart gaan Candy en Pokemon naar een kladblok

import tkinter as tk
from tkinter import messagebox

# Functie om data in te laden vanuit bestanden
def load_data():
try:
with open(‘PokemonCandys.txt’, ‘r’) as f:
for line in f:
candy_listbox.insert(tk.END, line.strip())
except FileNotFoundError:
pass # Als het bestand niet bestaat, doe dan niets

try:
with open(‘Pokemonsoort.txt’, ‘r’) as f:
for line in f:
pokemon_listbox.insert(tk.END, line.strip())
except FileNotFoundError:
pass # Als het bestand niet bestaat, doe dan niets

# Functie om bestaande matches in te laden en weer te geven
def view_matches():
# Maak een nieuw scherm voor het bekijken van matches
view_matches_window = tk.Toplevel(root)
view_matches_window.title(“Bekijk Matches”)

# Maak een tekstvak om de opgeslagen matches weer te geven
try:
with open(‘PokemonCandyMatches.txt’, ‘r’) as file:
matches = file.readlines()
matches_display = tk.Listbox(view_matches_window, height=10, width=50)
matches_display.pack(padx=20, pady=20)

# Sorteer de matches op Pokemon naam
sorted_matches = sorted(matches) # Sorteer automatisch alfabetisch (als het nodig is)

for match in sorted_matches:
matches_display.insert(tk.END, match.strip()) # Voeg elke match toe aan de listbox
except FileNotFoundError:
messagebox.showwarning(“Bestand niet gevonden”, “Geen matches gevonden!”)

# Voeg een knop toe om terug te keren naar het hoofdscherm
back_button = tk.Button(view_matches_window, text=”Terug naar Hoofdscherm”, command=view_matches_window.destroy)
back_button.pack(pady=10)

# Functie om Candy toe te voegen
def add_candy():
candy = candy_entry.get()
if candy:
candy_listbox.insert(tk.END, candy)
candy_entry.delete(0, tk.END)
save_candies() # Candy wordt na toevoeging opgeslagen in bestand
else:
messagebox.showwarning(“Input Error”, “Vul een Candy in”)

# Functie om Pokemon toe te voegen
def add_pokemon():
pokemon = pokemon_entry.get()
if pokemon:
pokemon_listbox.insert(tk.END, pokemon)
pokemon_entry.delete(0, tk.END)
save_pokemons() # Pokemon wordt na toevoeging opgeslagen in bestand
else:
messagebox.showwarning(“Input Error”, “Vul een Pokemon in”)

# Functie om een Candy naar de Match sectie te verplaatsen
def match_candy():
selected_candy_index = candy_listbox.curselection()
if selected_candy_index:
candy = candy_listbox.get(selected_candy_index)
# Verplaats de geselecteerde candy naar de match_candy_listbox
match_candy_listbox.insert(tk.END, candy)
candy_listbox.delete(selected_candy_index) # Verwijder de candy uit de oorspronkelijke lijst
else:
messagebox.showwarning(“Selection Error”, “Selecteer een Candy om te verplaatsen naar de matchlijst”)

# Functie om een Pokemon naar de Match sectie te verplaatsen
def match_pokemon():
selected_pokemon_index = pokemon_listbox.curselection()
if selected_pokemon_index:
pokemon = pokemon_listbox.get(selected_pokemon_index)
# Verplaats de geselecteerde pokemon naar de match_pokemon_listbox
match_pokemon_listbox.insert(tk.END, pokemon)
pokemon_listbox.delete(selected_pokemon_index) # Verwijder de pokemon uit de oorspronkelijke lijst
else:
messagebox.showwarning(“Selection Error”, “Selecteer een Pokemon om te verplaatsen naar de matchlijst”)

# Functie om de Candy en Pokemon matches op te slaan
def save_matches():
# Verkrijg alle items uit de matchlijsten
candy_matches = list(match_candy_listbox.get(0, tk.END))
pokemon_matches = list(match_pokemon_listbox.get(0, tk.END))

# Zorg ervoor dat de Candy’s en Pokemon’s in alfabetische volgorde staan
combined_matches = zip(candy_matches, pokemon_matches) # Combineer de Candy’s en Pokemon’s
sorted_matches = sorted(combined_matches, key=lambda x: x[1]) # Sorteer op de Pokemon-naam (de tweede in de tuple)

# Bewaar de matches in beide bestanden
try:
# PokemonCandyMatches.txt sorteren op Pokemon (de tweede waarde in de tuple)
with open(‘PokemonCandyMatches.txt’, ‘a’) as f:
for candy, pokemon in sorted_matches:
f.write(f”{pokemon} – {candy}\n”) # Alleen de Pokemon-naam en de Candy-naam zonder prefix

# CandyPokemonMatches.txt sorteren op Candy (de eerste waarde in de tuple)
with open(‘CandyPokemonMatches.txt’, ‘a’) as f:
for candy, pokemon in sorted_matches:
f.write(f”{candy} – {pokemon}\n”) # Alleen de Candy-naam en de Pokemon-naam zonder prefix

except FileNotFoundError:
messagebox.showwarning(“Bestand niet gevonden”, “Bestand voor Matches bestaat niet.”)

# Laat een bericht zien dat de gegevens succesvol zijn opgeslagen
messagebox.showinfo(“Opslaan”, “Nieuwe matches succesvol toegevoegd aan PokemonCandyMatches.txt en CandyPokemonMatches.txt”)

# Maak de matchlijst leeg na het opslaan
match_candy_listbox.delete(0, tk.END)
match_pokemon_listbox.delete(0, tk.END)

# Werk de lijst bij
update_match_listbox()

# Functie om de matchlijst in de UI bij te werken en te sorteren
def update_match_listbox():
# Verkrijg alle matches
candy_matches = list(match_candy_listbox.get(0, tk.END))
pokemon_matches = list(match_pokemon_listbox.get(0, tk.END))

# Zorg ervoor dat de matches in alfabetische volgorde van Candy staan
sorted_matches = sorted(zip(candy_matches, pokemon_matches), key=lambda x: x[0])

# Maak de lijst leeg en voeg de gesorteerde matches toe
match_candy_listbox.delete(0, tk.END)
match_pokemon_listbox.delete(0, tk.END)

for candy, pokemon in sorted_matches:
match_candy_listbox.insert(tk.END, candy)
match_pokemon_listbox.insert(tk.END, pokemon)

# Functie om Candy’s op te slaan naar bestand in alfabetische volgorde
def save_candies():
candies = list(candy_listbox.get(0, tk.END)) # Verkrijg alle candy’s uit de lijst
candies.sort() # Sorteer de candy’s alfabetisch

with open(‘PokemonCandys.txt’, ‘w’) as f:
for candy in candies:
f.write(candy + ‘\n’)

# Functie om Pokemon op te slaan naar bestand in alfabetische volgorde
def save_pokemons():
pokemons = list(pokemon_listbox.get(0, tk.END)) # Verkrijg alle pokemon uit de lijst
pokemons.sort() # Sorteer de pokemon alfabetisch

with open(‘Pokemonsoort.txt’, ‘w’) as f:
for pokemon in pokemons:
f.write(pokemon + ‘\n’)

# Functie om naar het volgende invoerveld te schakelen met Enter
def switch_focus(event, next_widget):
next_widget.focus_set()

# Functie om geselecteerde Candy te verwijderen
def remove_candy():
selected_candy_index = candy_listbox.curselection()
if selected_candy_index:
candy_listbox.delete(selected_candy_index)
save_candies() # Na verwijderen opnieuw opslaan
else:
messagebox.showwarning(“Selection Error”, “Selecteer een Candy om te verwijderen”)

# Functie om geselecteerde Pokemon te verwijderen
def remove_pokemon():
selected_pokemon_index = pokemon_listbox.curselection()
if selected_pokemon_index:
pokemon_listbox.delete(selected_pokemon_index)
save_pokemons() # Na verwijderen opnieuw opslaan
else:
messagebox.showwarning(“Selection Error”, “Selecteer een Pokemon om te verwijderen”)

# Hoofdvenster
root = tk.Tk()
root.title(“Candy & Pokemon Matcher”)
root.geometry(“600×600+100+100″)

# UI-elementen voor Candy
candy_label = tk.Label(root, text=”Candy”)
candy_label.grid(row=0, column=0)
candy_entry = tk.Entry(root)
candy_entry.grid(row=1, column=0)
add_candy_button = tk.Button(root, text=”Voeg Candy toe”, command=add_candy)
add_candy_button.grid(row=2, column=0)
remove_candy_button = tk.Button(root, text=”Verwijder Candy”, command=remove_candy)
remove_candy_button.grid(row=3, column=0)
candy_listbox = tk.Listbox(root, height=5, selectmode=”single”)
candy_listbox.grid(row=4, column=0)

# UI-elementen voor Pokemon
pokemon_label = tk.Label(root, text=”Pokemon”)
pokemon_label.grid(row=0, column=1)
pokemon_entry = tk.Entry(root)
pokemon_entry.grid(row=1, column=1)
add_pokemon_button = tk.Button(root, text=”Voeg Pokemon toe”, command=add_pokemon)
add_pokemon_button.grid(row=2, column=1)
remove_pokemon_button = tk.Button(root, text=”Verwijder Pokemon”, command=remove_pokemon)
remove_pokemon_button.grid(row=3, column=1)
pokemon_listbox = tk.Listbox(root, height=5, selectmode=”single”)
pokemon_listbox.grid(row=4, column=1)

# UI-elementen voor Matches
match_candy_listbox = tk.Listbox(root, height=5, selectmode=”single”)
match_candy_listbox.grid(row=7, column=0)
match_pokemon_listbox = tk.Listbox(root, height=5, selectmode=”single”)
match_pokemon_listbox.grid(row=7, column=1)
save_matches_button = tk.Button(root, text=”Match Opslaan”, command=save_matches)
save_matches_button.grid(row=8, column=0, columnspan=2)

# Knop om naar de matches te bekijken
view_matches_button = tk.Button(root, text=”Bekijk Matches”, command=view_matches)
view_matches_button.grid(row=9, column=0, columnspan=2)

# Voeg knoppen toe om Candy en Pokemon naar de match secties te verplaatsen
match_candy_button = tk.Button(root, text=”Verplaats naar Match”, command=match_candy)
match_candy_button.grid(row=5, column=0)

match_pokemon_button = tk.Button(root, text=”Verplaats naar Match”, command=match_pokemon)
match_pokemon_button.grid(row=5, column=1)

# Laad de data uit de bestanden bij het starten van de app
load_data()

# Start de applicatie
root.mainloop()

Geef een reactie

Je e-mailadres wordt niet gepubliceerd. Vereiste velden zijn gemarkeerd met *