Home > Enterprise >  How can I get new row's value repeatedly in tkinter? The code below gets only the last row in t
How can I get new row's value repeatedly in tkinter? The code below gets only the last row in t

Time:07-27

The code below is only displaying the last row's values in tkinter entry box, whereas when printing in the shell, it prints all the values from each row. This is how I want it to be displayed in tkinter.

data=csv.reader(file)
for r in data:
    x_var.set(r[1]) #x_var entry widget here r keeps incrementing but displays only the last row value.[1] is first column.
    y_var.set(r[2]) #y_var entry widget here r keeps incrementing but displays only the last row value.[2 is second column.
    z_var.set(r[3]) #z_var entry widget here r keeps incrementing but displays only the last row value.[3] is third column.

is only displaying the last row's values in tkinter, whereas in a shell, I want to create new tkinter entry box automatically for each row or get one row value at a time from csv in the above entry widget. In short if I can control r or increment (r =1) it manually using loop or any condition.

data in file is: enter image description here

CodePudding user response:

In response to your last comment, whose answer was too long to put in a comment:

Would you be able to make a function:

def func(num):
    global i
    i  = 1
    x_var.set(data[num])
    y_var.set(data[num])
    z_var.set(data[num])

And for labels:

def func(num):
    global i
    i  = 1
    lbl1.config(text=data[num])
    lbl2.config(text=data[num])
    lbl3.config(text=data[num])

And make a button:

import tkinter as tk
i = 0
btn = tk.Button(master, text="Show next values", command=lambda: func(i))
btn.pack()

where master is the widget master. This would do what you want to do, but to do this without the button would, in my knowledge, require much manual work, which defeats the point of python.

CodePudding user response:

You can use a global variable to keep track of the current row, I've called mine current_row. The next_row function uses current_row to determine which data to show and then increments it. The button command is set to next_row, so every time you press it it shows the next row.

import tkinter as tk

root = tk.Tk()

data = [[600, 250, 522, 200], [400, 210, 512, 225], [940, 250, 502, 250], [920, 250, 492, 275], [800, 240, 482, 300], [850, 230, 472, 325], [900, 220, 462, 350], [920, 210, 452, 375], [880, 220, 442, 400], [850, 230, 432, 425], [840, 245, 422, 450], [800, 250, 412, 475], [700, 255, 402, 500], [600, 250, 392, 525]]
current_row = 0

def next_row():
    global current_row
    x_var.set(data[current_row][1])
    y_var.set(data[current_row][2])
    z_var.set(data[current_row][3])
    current_row  = 1

x_var = tk.IntVar(root)
y_var = tk.IntVar(root)
z_var = tk.IntVar(root)
next_row() # show first row

x_ent = tk.Entry(root, textvariable = x_var, width = 10)
y_ent = tk.Entry(root, textvariable = y_var, width = 10)
z_ent = tk.Entry(root, textvariable = z_var, width = 10)
x_ent.grid(row = 0, column = 0)
y_ent.grid(row = 0, column = 1)
z_ent.grid(row = 0, column = 2)

next_btn = tk.Button(root, text = "Show next row", command = next_row)
next_btn.grid(row = 1, column = 0, columnspan = 3)

root.mainloop()
  • Related