首页 > 解决方案 > What would be the best Data Structure for this tkinter RPG Item Generator

问题描述

I am working on a generator for an RPG game and am trying to figure out the best way to organize my data. Right now I have it in .csv format and am processing it using pandas.

My goal is to be able to get a random item from the files, with some being more rare than others, then add random effects to it that may change some of the stats like damage.

This will all be displayed in tkinter; showing the name of the items, its stats such as type, damage, value, etc. and then magical effects it may have come with.

Right now I am hitting a roadblock with pandas in being able to display individual cell data in tkinter. When I have the pandas processing in the main root it works fine but when I seperate it as a function that I want to be able to loop it doesnt display correctly. I get a bunch of numbers then the name of the function in the label text.

I am wondering if it would be easier to use a different data structure such as an array or dictionary but what I have found seems to be more oriented towards data sets that only have one variable and are just ints.

Or maybe there is a way to actually get the pandas data cells I want to display correctly in tkinter...

my pandas list has 3 different data sources right now and each row has about 12 statistics of both ints and strs.

Here is my code:

from tkinter import *
import pandas as pd
import random 

root = Tk()
root.title("Random Item Generator")

il_label = Label(root, text = "Max Item Level")
il_label.grid(row=0,column=0)

il_entry = Entry(root, width=10, borderwidth=1)
il_entry.grid(row=0,column=1)

df = [pd.read_csv('weapons.csv'), pd.read_csv('armor.csv'), pd.read_csv('aether_infused.csv')]

def rand_il():  #This is not being used yet
    return random.randint(1,int(il_entry.get()))

item_class = random.choices(df, weights=(45,40,15), k=1) 
item = item_class[0].sample()

item_name = item.iloc[0]['Name']
iLevel_base = item.iloc[0]['Item Level'] 

def gen_item():
    name_txt = Label(root,text= item_name)
    ilBase_txt = Label(root,text= iLevel_base)
    
    name_txt.grid(row=2, column=0)
    ilBase_txt.grid(row=2, column=1)


gen_button = Button(root, text="Generate Item", command=gen_item).grid(row=1)

root.mainloop()

this one works but I can't loop the process to generate the item which I will need to do in order to restrict certain items based on level

when I change it to this:

def get_itemBase():
    item_class = random.choices(df, weights=(45,40,15), k=1) 
    item = item_class[0].sample()
    return item

it starts to fall apart. get_itemBase.iloc doesn't work and neither do my labels in gen_item

Any help would be greatly appreciated.

Kevin

标签: pythonpandasdataframetkinter

解决方案


get_itemBase.iloc because get_itemBase is a method. You have to do get_itemBase().iloc. How did you call the function? The best thing to do is

item = get_itemBase()
item_name = item.iloc[0]['Name']`
iLevel_base = item.iloc[0]['Item Level']

because you have to be sure you are using the same item when reading the name and the level.


推荐阅读