简体   繁体   English

如何在 tkinter 中重复获取新行的值? 下面的代码仅获取 tkinter 条目小部件中的最后一行

[英]How can I get new row's value repeatedly in tkinter? The code below gets only the last row in tkinter entry widget

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.下面的代码仅在 tkinter 输入框中显示最后一行的值,而在 shell 中打印时,它会打印每行的所有值。 This is how I want it to be displayed in tkinter.这就是我希望它在 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. 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.简而言之,如果我可以控制 r 或使用循环或任何条件手动增加(r+=1)它。

    reader=csv.reader(file) #csv.reader is a generator that reads line by line
    for i,j,k,l in reader:
        print(i,j,k,l)
        x_var.set(j)                                                           
        print(j)
        y_var.set(k)                               
        print(k)    
        z_var.set(l)                               
        print(l)
        break  

the above code reads the first row and breaks out are you looking for something to repeat the above code after some time?上面的代码读取第一行并中断您是否正在寻找一些东西在一段时间后重复上面的代码?

This is because the values are quickly changing from the first row's values to the last row's values.这是因为值从第一行的值快速更改为最后一行的值。 To see this, import time and add time.sleep(num) to the end of the for loop, where num is any number.要查看这一点,请导入time并将time.sleep(num)添加到 for 循环的末尾,其中 num 是任意数字。 A solution is as follows:一个解决方案如下:

data = csv.reader(file)
for r in data:
    x_var.set(x_var.get() + r[1])
    y_var.set(y_var.get() + r[2])
    z_var.set(z_var.get() + r[3])

In response to your last comment, whose answer was too long to put in a comment:在回复您的最后一条评论时,他的回答太长而无法发表评论:

Would you be able to make a function:你能做一个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.其中 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.这会做你想做的事,但据我所知,在没有按钮的情况下这样做需要大量的手动工作,这违背了 python 的观点。

You can use a global variable to keep track of the current row, I've called mine current_row .您可以使用全局变量来跟踪当前行,我将其称为current_row The next_row function uses current_row to determine which data to show and then increments it. next_row function 使用current_row来确定要显示的数据,然后将其递增。 The button command is set to next_row , so every time you press it it shows the next row.按钮命令设置为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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM