vendredi 24 janvier 2020

Tkinter issue with the label in if block

I have an issue with the label in if statement in tkinter, also similar with the try/except block.

I am trying to create a Pokemon fight simulator based on the data from csv.

This is pulling data from csv, but the exemption mechanism, when someone puts a name that is not in base is not working.

     else:
            my_label = Label(root, text="Oops! I don't think we got such Pokemon archive.", font=("Comic Sans", 20))
            my_label.grid(row=1, column=1)

I want the program to show additional label with the text but it's not working. It's freezing a program instead so I am not able to discover the error, nor able to figure out the reason. Could someone please advise me how to fix this?

   from tkinter import *


def create_pokemon_b():
    import pandas as pd
    df_pokemons = pd.read_csv('/home/js/PycharmProjects/untitled/main_Pokemon.csv')
    while True:
        name = str(entry_pokename_b.get()).capitalize()
        if name in list(df_pokemons['pokename']):
            while True:
                level = entry_pokelevel_b.get()
                try:
                    level = int(level)
                    speed = int(df_pokemons[(df_pokemons['pokename'] == name) & (df_pokemons['pokelevel'] == level)][
                                    'speed'].mean())
                    hp = int(df_pokemons[(df_pokemons['pokename'] == name) & (df_pokemons['pokelevel'] == level)][
                                 'hp'].mean())
                    att = int(df_pokemons[(df_pokemons['pokename'] == name) & (df_pokemons['pokelevel'] == level)][
                                  'attack'].mean())
                    defense = int(df_pokemons[(df_pokemons['pokename'] == name) & (df_pokemons['pokelevel'] == level)][
                                      'defense'].mean())
                    type1 = str(df_pokemons[(df_pokemons['pokename'] == name) & (df_pokemons['pokelevel'] == level)][
                                    'type1'].unique()[0])
                    stats = Label(root,
                                  text='{} on level {}: \nType = {} \nHP = {} \nDefense = {} \nSpeed = {}\nAttack = {}\n'.format(
                                      name, level, type1, hp, defense, speed, att), font=("Comic Sans", 15))
                    stats.grid(row=7, column=1, columnspan=6, rowspan=6)
                    global poke_b
                    poke_b = tuple(type1, name, level, speed, hp, defense, att)

                except ValueError:
                    error = Label(root, text="Oops! I don't think we got such Pokemon archive.",
                                  font=("Comic Sans", 20))
                    error.grid(row=7, column=1)
        else:
            my_label = Label(root, text="Oops! I don't think we got such Pokemon archive.", font=("Comic Sans", 20))
            my_label.grid(row=1, column=1)


root = Tk()

my_label = Label(root)
my_label.grid(row=1, column=1)
#entry fields where name and level of pokemon B are entered

label_pokename = Label(root, text='Enter name of Pokemon A.',font=("Comic Sans", 15))
label_pokelevel = Label(root, text='What is it\'s level?',font=("Comic Sans", 15))

entry_pokename = Entry(root, width=40, font=("Comic Sans", 15))
entry_pokelevel = Entry(root, width=40, font=("Comic Sans", 15))

label_pokename.grid(row=0)
entry_pokename.grid(row=1)
label_pokelevel.grid(row=2)
entry_pokelevel.grid(row=3)

#statistics field

stats = Label(root)
stats.grid(row=1,column=1, columnspan=6, rowspan=6)

#button starting function used to pull pokemon data from the csv

button_a = Button(root, text='Search',font=("Comic Sans", 15),command=create_pokemon_a)
button_a.grid(row=4)

root.mainloop()

Aucun commentaire:

Enregistrer un commentaire