Home > OS >  labeling tkinter buttons with pandas dataframe
labeling tkinter buttons with pandas dataframe

Time:01-22

I have a simple for loop that creates two rows of three buttons in a window

for c in range(3):
    for r in range(2):
        tk.Button(width=10, height=2).grid(column=c, row=r, pady=6, padx=2)

I've been trying to label each individual button with text taken from each row in a pandas dataframe like so

import tkinter as tk
import pandas as pd
window = tk.Tk()
window.geometry('255x200')
data = {'name': ['Name1', 'Name2', 'Name3', 'Name4', 'Name5', 'Name6'],
        'Value': ['Value1', 'Value2', 'Value3', 'Value4', 'Value5', 'Value6']}
df = pd.DataFrame(data)
index = df.index
rows = len(index)
namelist = df.iloc[1:rows,0]
for c in range(3):
    for r in range(2):
        for t in namelist:
            tk.Button(width=10, height=2, text=t).grid(column=c, row=r, pady=6, padx=2)
window.mainloop()

the issue this causes is that every button is labeled with the latest entry in the 'Name' column. is there a way to properly label these buttons using this for loop?

CodePudding user response:

The issue is with your looping method. You are looping through the row and column numbers and then by each name in the namelist. Essentially what is happening is for each row and column, you then loop through each name in namelist. This means you are creating 36 buttons instead of 6.

Lets look at r=0 and c=0. In r=0 and c=0 you then loop through all names in namelist. First, in the grid (0,0) you place the button Name1. However, then you place another button in grid (0,0) with the label Name2 that overlaps it, so you only see Name2. This continues until you reach Name6. This is why you only see Name6 in each location.

Also, don't forget to index starting with 0 in namelist = df.iloc[0:rows,0]. Alternatively you could set namelist=df['name'], then you don't need to calculate the rows.

One solution is to loop through the namelist only and use separate counters inside the loop for the rows and columns that you manually increment.

Try this. I edited the loop so you only loop through each name once.

import tkinter as tk
import pandas as pd
window = tk.Tk()
window.geometry('255x200')
data = {'name': ['Name1', 'Name2', 'Name3', 'Name4', 'Name5', 'Name6'],
        'Value': ['Value1', 'Value2', 'Value3', 'Value4', 'Value5', 'Value6']}
df = pd.DataFrame(data)
index = df.index
rows = len(index)
namelist = df.iloc[0:rows,0]
c = 0
r = 0
for t in namelist:
    tk.Button(width=10, height=2, text=t).grid(column=c, row=r, pady=6, padx=2)
    c = c   1
    if c > 2:
        c = 0
        r = r   1
window.mainloop()

CodePudding user response:

Easy task. You can do it very sophisticated with variables variables or like this:

import tkinter as tk
import pandas as pd


window = tk.Tk()
window.geometry('255x200')

data = {'name': ['Name1', 'Name2', 'Name3', 'Name4', 'Name5', 'Name6'],
        'Value': ['Value1', 'Value2', 'Value3', 'Value4', 'Value5', 'Value6']}
df = pd.DataFrame(data)
index = df.index
rows = len(index)
namelist = df.iloc[0:rows,0]

for r in range(2):
    for c in range(3):
        idx_namelist = (((r 1)*(r 1) c)-1)
        tk.Button(width=10, height=2, text=namelist.iloc[idx_namelist]).grid(column=c, row=r, pady=6, padx=2)
window.mainloop()

And your code had an error in here:

namelist = df.iloc[1:rows,0]

  •  Tags:  
  • Related