Tkinter 表格显示
Tkinter tabular display
我在控制台中有这种类型的数据
['0.0.0.0' 68 '255.5.55.5' 67 '04/05/2021 02:42:15 AM'] Threat
['4.37.53.24' 67 '5.225.5.255' 68 '04/05/2021 02:42:15 AM'] Threat
['503.637.53.15' 385 '8.8.9.8' 53 '04/05/2021 02:42:17 AM'] Ok
['23.27.43.65' 446 '8.8.8.8' 53 '04/05/2021 02:42:17 AM'] Ok
['23.27.53.5' 814 '172.217.24.78' 443 '04/05/2021 02:42:17 AM'] Ok
['8.1.0.1' 0 '8.1.6.1' 0 '04/05/2021 02:42:17 AM'] Ok
它是用下面的代码生成的。问题是我想以正确的列和行形状显示,我将 tkinter 库用于 Gui 目的。在 Tkinter 中,我尝试过 tksheet 但在这种情况下它不起作用。如何以正确的列和行形式显示它?哪个 Gui tkinter 小部件支持它?
from tkinter import ttk
columns = ('#1', '#2', '#3', '#4', '#5', '#6')
tree = ttk.Treeview(root, columns=columns, show='headings')
# define headings
tree.heading('#1', text='S-IP')
tree.heading('#2', text='S-Port')
tree.heading('#3', text='D-IP')
tree.heading('#4', text='D-port')
tree.heading('#5', text='Time-Stamp')
tree.heading('#6', text='Status')
tree.grid(row=0, column=0, sticky='nsew')
df = pd.read_csv('Flo.csv')
classifier_model_reloaded = pickle.load(open('pickle_model.pkl', 'rb'))
predictions=classifier_model_reloaded.predict(X)
for i in range(len(predictions)):
num = '%d' % (predictions[i])
if int(num) == 0:
results.append('OK')
else:
results.append('Threat')
for i in range(len(results)):
k = (df.loc[i, ["Src IP", "Src Port", "Dst IP", "Dst Port", "Timestamp" ]].values, results[i])
tree.insert('', tk.END, values=k)
这是因为k
下面一行:
k = (df.loc[i, ["Src IP", "Src Port", "Dst IP", "Dst Port", "Timestamp" ]].values, results[i])
是一个包含 2 个项目的元组:numpy.ndarray
实例和一个字符串。
您需要将 numpy.ndarray
实例和字符串合并为一个元组或列表:
k = df.loc[i, ["Src IP", "Src Port", "Dst IP", "Dst Port", "Timestamp"]].values.tolist() + [results[i]]
我在控制台中有这种类型的数据
['0.0.0.0' 68 '255.5.55.5' 67 '04/05/2021 02:42:15 AM'] Threat
['4.37.53.24' 67 '5.225.5.255' 68 '04/05/2021 02:42:15 AM'] Threat
['503.637.53.15' 385 '8.8.9.8' 53 '04/05/2021 02:42:17 AM'] Ok
['23.27.43.65' 446 '8.8.8.8' 53 '04/05/2021 02:42:17 AM'] Ok
['23.27.53.5' 814 '172.217.24.78' 443 '04/05/2021 02:42:17 AM'] Ok
['8.1.0.1' 0 '8.1.6.1' 0 '04/05/2021 02:42:17 AM'] Ok
它是用下面的代码生成的。问题是我想以正确的列和行形状显示,我将 tkinter 库用于 Gui 目的。在 Tkinter 中,我尝试过 tksheet 但在这种情况下它不起作用。如何以正确的列和行形式显示它?哪个 Gui tkinter 小部件支持它?
from tkinter import ttk
columns = ('#1', '#2', '#3', '#4', '#5', '#6')
tree = ttk.Treeview(root, columns=columns, show='headings')
# define headings
tree.heading('#1', text='S-IP')
tree.heading('#2', text='S-Port')
tree.heading('#3', text='D-IP')
tree.heading('#4', text='D-port')
tree.heading('#5', text='Time-Stamp')
tree.heading('#6', text='Status')
tree.grid(row=0, column=0, sticky='nsew')
df = pd.read_csv('Flo.csv')
classifier_model_reloaded = pickle.load(open('pickle_model.pkl', 'rb'))
predictions=classifier_model_reloaded.predict(X)
for i in range(len(predictions)):
num = '%d' % (predictions[i])
if int(num) == 0:
results.append('OK')
else:
results.append('Threat')
for i in range(len(results)):
k = (df.loc[i, ["Src IP", "Src Port", "Dst IP", "Dst Port", "Timestamp" ]].values, results[i])
tree.insert('', tk.END, values=k)
这是因为k
下面一行:
k = (df.loc[i, ["Src IP", "Src Port", "Dst IP", "Dst Port", "Timestamp" ]].values, results[i])
是一个包含 2 个项目的元组:numpy.ndarray
实例和一个字符串。
您需要将 numpy.ndarray
实例和字符串合并为一个元组或列表:
k = df.loc[i, ["Src IP", "Src Port", "Dst IP", "Dst Port", "Timestamp"]].values.tolist() + [results[i]]