此问题与:How to imitate this table using Tkinter?。
有关我尝试了第一个示例,但是我在打印的表格中获得了数组的边框[
,]
和字符串的'
符号。我怎么能摆脱他们?
以下是源代码,基于前面的示例:
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 10 14:26:37 2016
@author: peterk
"""
from tkinter import *
from tkinter.ttk import *
import numpy as np
class App(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.CreateUI()
self.LoadTable()
self.grid(sticky = (N,S,W,E))
parent.grid_rowconfigure(0, weight = 1)
parent.grid_columnconfigure(0, weight = 1)
def CreateUI(self):
tv = Treeview(self)
tv['columns'] = ('Parameter A', 'Parameter m', 'Parameter n')
tv.heading("#0", text='Based on fit', anchor='w')
tv.column("#0", anchor="w")
tv.heading('Parameter A', text='Parameter A')
tv.column('Parameter A', anchor='center', width=100)
tv.heading('Parameter m', text='Parameter m')
tv.column('Parameter m', anchor='center', width=100)
tv.heading('Parameter n', text='Parameter n')
tv.column('Parameter n', anchor='center', width=100)
tv.grid(sticky = (N,S,W,E))
self.treeview = tv
self.grid_rowconfigure(0, weight = 1)
self.grid_columnconfigure(0, weight = 1)
def LoadTable(self):
table1=np.reshape(np.array([10.,2.,3.,4.,5.,6.,7.,8.,9.,10.,11.,12.,13.,14.,15.,16.,17.,18.]), (6,3))
# self.treeview.insert('', 'end', text="First", values=('10:00',
# '10:10', 'Ok'))
table2 = numpy.array(["%.8e" % w for w in table1.reshape(table1.size)])
table2 = table2.reshape(table1.shape)
print(table2)
for i in range(6):
self.treeview.insert('', 'end', text="based_on_line_"+str(i), values=(table2[i,:]))
def main():
root = Tk()
App(root)
root.mainloop()
if __name__ == '__main__':
main()
答案 0 :(得分:1)
您的numpy数组public static void main(String[] aArgs) {
TextDB txtDB = new TextDB();
String filename = "professor.txt";
try {
//check if the file exist
File oFile = new File(filename);
if(!oFile.exist()) {
oFile.mkdirs(); //optional
oFile.createNewFile();
}
// read file containing Professor records.
ArrayList al = TextDB.readProfessors(filename);
for (int i = 0; i < al.size(); i++) {
Professor prof = (Professor) al.get(i);
System.out.println("Name " + prof.getName());
System.out.println("Contact " + prof.getContact());
}
Professor p1 = new Professor("Joseph", "jos@ntu.edu.sg", 67909999);
// al is an array list containing Professor objs
al.add(p1);
// write Professor record/s to file.
TextDB.saveProfessors(filename, al);
} catch (IOException e) {
System.out.println("IOException > " + e.getMessage());
}
}
的类型为string:
table2
在您的>>> import numpy as np
>>> table1=np.reshape(np.array([10.,2.,3.,4.,5.,6.,7.,8.,9.,10.,11.,12.,13.,14.,15.,16.,17.,18.]), (6,3))
>>> table2 = np.array(["%.8e" % w for w in table1.reshape(table1.size)])
>>> table2.dtype
dtype('S14')
>>>
方法中,您正在插入字符串:
LoadTable()
因此,获得该结果是正常的,因为table2只是一个字符串表:
self.treeview.insert('', 'end', text="based_on_line_"+str(i), values=(table2[i,:]))
所以可能解决方案是将字符串numpy数组>>> table2
array(['1.00000000e+01', '2.00000000e+00', '3.00000000e+00',
'4.00000000e+00', '5.00000000e+00', '6.00000000e+00',
'7.00000000e+00', '8.00000000e+00', '9.00000000e+00',
'1.00000000e+01', '1.10000000e+01', '1.20000000e+01',
'1.30000000e+01', '1.40000000e+01', '1.50000000e+01',
'1.60000000e+01', '1.70000000e+01', '1.80000000e+01'],
dtype='|S14')
>>>
转换为float numpy数组:
table2
但是,这不是您期望的结果:
>>> table3 = table2.astype(np.float)
所以你可能唯一的解决方案就是使用Python正则表达式re
模块:
>>> table3
array([ 10., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11.,
12., 13., 14., 15., 16., 17., 18.])
>>>
因此,您可以编写一个循环于>>> import re
>>> begueradj = "['1.00000000e+01']"
>>> print begueradj
['1.00000000e+01']
>>> begueradj = re.sub("[\['\]]","",begueradj)
>>> print begueradj
1.00000000e+01
>>>
的每个值的方法,以删除那些不需要的字符,如上例所示。
但是,为什么要使用正则表达式方法添加更多代码,而您可以通过对table2[i,:]
方法进行少量修改来以较简单的方式解决问题?
LoadTable()
上述程序输出了您正在寻找的演示:
备注:强>
我使用Python 2.7对此进行编码,因此如果您想使用Python 3.x,那么您只需要调整两个导入:
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 16 13:07:36 2016
@author: Bill BEGUERADJ
"""
from Tkinter import *
from ttk import *
import numpy as np
class App(Frame):
def __init__(self, parent):
Frame.__init__(self, parent)
self.CreateUI()
self.LoadTable()
self.grid(sticky = (N,S,W,E))
parent.title("Bill Begueradj Python ttk.Treeview Demo Solution")
parent.grid_rowconfigure(0, weight = 1)
parent.grid_columnconfigure(0, weight = 1)
def CreateUI(self):
tv = Treeview(self)
tv['columns'] = ('Parameter A', 'Parameter m', 'Parameter n')
tv.heading("#0", text='Based on fit', anchor='c')
tv.column("#0", anchor="c")
tv.heading('Parameter A', text='Parameter A')
tv.column('Parameter A', anchor='center', width=100)
tv.heading('Parameter m', text='Parameter m')
tv.column('Parameter m', anchor='center', width=100)
tv.heading('Parameter n', text='Parameter n')
tv.column('Parameter n', anchor='center', width=100)
tv.grid(sticky = (N,S,W,E))
self.treeview = tv
self.grid_rowconfigure(0, weight = 1)
self.grid_columnconfigure(0, weight = 1)
def LoadTable(self):
table1=np.reshape(np.array([10.,2.,3.,4.,5.,6.,7.,8.,9.,10.,11.,12.,13.,14.,15.,16.,17.,18.]), (6,3))
table2 = np.array(["%.8e" % w for w in table1.reshape(table1.size)])
table2 = table2.reshape(table1.shape)
for i in range(6):
self.treeview.insert('', 'end', text="based_on_line_"+str(i), values=(table2[i,0], table2[i,1],table2[i,2]))
# print table2[i,:]
def main():
root = Tk()
App(root)
root.mainloop()
if __name__ == '__main__':
main()
代替from tkinter import *
from Tkinter import *
代替from tkinter.ttk import *