将值插入 Python 中 tkinter 树视图中的特定列
Insert values to specific column in tkinter treeview in Python
这个问题与 Aleksandar Beat 的问题很相似,但又完全不同。
我用treeview做了一个系统GUI。我想像这样在特定列中插入值:
[1]: https://i.stack.imgur.com/N6ubb.png
然而,我总是得到这样的结果:
[2]: https://i.stack.imgur.com/5DKFI.png
这个问题的最佳解决方案是什么?
这是代码:
from tkinter import *
import tkinter
import tkinter.ttk as ttk
import csv
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
import plotly.express as px
from sklearn.linear_model import LinearRegression
data = pd.read_csv("2019-2020.csv")
x = np.array(data[["gender","distance","scholarship","stanine"]])
y = np.array(data["enrolled_2021"])
xtrain,xtest,ytrain,ytest = train_test_split(x,y,test_size=0.24,random_state=40)
model = LinearRegression()
model.fit(xtrain,ytrain)
features = np.array(data[["gender","distance","scholarship","stanine"]])
root = tkinter.Tk()
root.title("FAITH Enrollment Prediction Simulator")
root.geometry("900x500")
TableMargin = Frame(root, width=300)
TableMargin.pack(side=RIGHT)
scrollbarx = Scrollbar(TableMargin, orient=HORIZONTAL)
scrollbary = Scrollbar(TableMargin, orient=VERTICAL)
tree = ttk.Treeview(TableMargin,
columns=("student_no", "gender", "distance", "scholarship", "stanine", "prediction"), height=400,
selectmode="extended", yscrollcommand=scrollbary.set, xscrollcommand=scrollbarx.set)
scrollbary.config(command=tree.yview)
scrollbary.pack(side=RIGHT, fill=Y)
scrollbarx.config(command=tree.xview)
scrollbarx.pack(side=BOTTOM, fill=X)
tree.heading('student_no', text="Student No.", anchor=W)
tree.heading('gender', text="Gender", anchor=W)
tree.heading('distance', text="Distance in KM", anchor=W)
tree.heading('scholarship', text="Scholarship", anchor=W)
tree.heading('stanine', text="STANINE Rank", anchor=W)
tree.heading('prediction', text="Prediction in %", anchor=W)
tree.column('#0', stretch=NO, minwidth=0, width=0)
tree.column('#1', stretch=NO, minwidth=0, width=100)
tree.column('#2', stretch=NO, minwidth=0, width=100)
tree.column('#3', stretch=NO, minwidth=0, width=100)
tree.column('#4', stretch=NO, minwidth=0, width=100)
tree.column('#5', stretch=NO, minwidth=0, width=100)
tree.pack()
with open('2019-2020.csv') as f:
reader = csv.DictReader(f, delimiter=',')
for row in reader:
student_no = row['student_no']
gender = row['gender']
distance = row['distance']
scholarship = row['scholarship']
stanine = row['stanine']
tree.insert("", 0, values=(student_no, gender, distance, scholarship, stanine))
def predict():
pred = model.predict(features) * 100
for x in pred:
tree.insert("", 'end', values=('','','','','', x))
b1 = Button(root, text='Predict', command=predict, height = 2, width = 20, bg='#081947', fg='#fff',)
b1.pack(padx=15, pady=200)
#============================INITIALIZATION==============================
if __name__ == '__main__':
root.mainloop()
与相关问题类似,我也知道如何添加新列,但我不知道如何在特定列中插入值。
如果我确实理解您要添加的问题,请按以下方式向 i 列添加数据:
my_tree.insert(parent='', iid=(item identifier), index='end', text='', value=(value you wish to insert))
您可以使用tree.set(iid, column, value)
设置列中单元格的值:
def predict():
pred = model.predict(features) * 100
# assume size of "pred" is the same as number of rows in the treevew
for iid, x in zip(tree.get_children(), pred):
tree.set(iid, 'prediction', x)
这个问题与 Aleksandar Beat 的问题很相似,但又完全不同。
我用treeview做了一个系统GUI。我想像这样在特定列中插入值: [1]: https://i.stack.imgur.com/N6ubb.png
然而,我总是得到这样的结果: [2]: https://i.stack.imgur.com/5DKFI.png
这个问题的最佳解决方案是什么?
这是代码:
from tkinter import *
import tkinter
import tkinter.ttk as ttk
import csv
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
import plotly.express as px
from sklearn.linear_model import LinearRegression
data = pd.read_csv("2019-2020.csv")
x = np.array(data[["gender","distance","scholarship","stanine"]])
y = np.array(data["enrolled_2021"])
xtrain,xtest,ytrain,ytest = train_test_split(x,y,test_size=0.24,random_state=40)
model = LinearRegression()
model.fit(xtrain,ytrain)
features = np.array(data[["gender","distance","scholarship","stanine"]])
root = tkinter.Tk()
root.title("FAITH Enrollment Prediction Simulator")
root.geometry("900x500")
TableMargin = Frame(root, width=300)
TableMargin.pack(side=RIGHT)
scrollbarx = Scrollbar(TableMargin, orient=HORIZONTAL)
scrollbary = Scrollbar(TableMargin, orient=VERTICAL)
tree = ttk.Treeview(TableMargin,
columns=("student_no", "gender", "distance", "scholarship", "stanine", "prediction"), height=400,
selectmode="extended", yscrollcommand=scrollbary.set, xscrollcommand=scrollbarx.set)
scrollbary.config(command=tree.yview)
scrollbary.pack(side=RIGHT, fill=Y)
scrollbarx.config(command=tree.xview)
scrollbarx.pack(side=BOTTOM, fill=X)
tree.heading('student_no', text="Student No.", anchor=W)
tree.heading('gender', text="Gender", anchor=W)
tree.heading('distance', text="Distance in KM", anchor=W)
tree.heading('scholarship', text="Scholarship", anchor=W)
tree.heading('stanine', text="STANINE Rank", anchor=W)
tree.heading('prediction', text="Prediction in %", anchor=W)
tree.column('#0', stretch=NO, minwidth=0, width=0)
tree.column('#1', stretch=NO, minwidth=0, width=100)
tree.column('#2', stretch=NO, minwidth=0, width=100)
tree.column('#3', stretch=NO, minwidth=0, width=100)
tree.column('#4', stretch=NO, minwidth=0, width=100)
tree.column('#5', stretch=NO, minwidth=0, width=100)
tree.pack()
with open('2019-2020.csv') as f:
reader = csv.DictReader(f, delimiter=',')
for row in reader:
student_no = row['student_no']
gender = row['gender']
distance = row['distance']
scholarship = row['scholarship']
stanine = row['stanine']
tree.insert("", 0, values=(student_no, gender, distance, scholarship, stanine))
def predict():
pred = model.predict(features) * 100
for x in pred:
tree.insert("", 'end', values=('','','','','', x))
b1 = Button(root, text='Predict', command=predict, height = 2, width = 20, bg='#081947', fg='#fff',)
b1.pack(padx=15, pady=200)
#============================INITIALIZATION==============================
if __name__ == '__main__':
root.mainloop()
与相关问题类似,我也知道如何添加新列,但我不知道如何在特定列中插入值。
如果我确实理解您要添加的问题,请按以下方式向 i 列添加数据:
my_tree.insert(parent='', iid=(item identifier), index='end', text='', value=(value you wish to insert))
您可以使用tree.set(iid, column, value)
设置列中单元格的值:
def predict():
pred = model.predict(features) * 100
# assume size of "pred" is the same as number of rows in the treevew
for iid, x in zip(tree.get_children(), pred):
tree.set(iid, 'prediction', x)