python 3.x - Python3 tk TreeView dynamic column width -
relevant code below. looking way dynamically size ttk treeview widget's column fit text inside it. have treeview (within class) minwidths on columns, text fill "notes" column infinitely long , column needs resize take string. limit max size needless still want dynamic hold large string of text if needed neat when string shorter.
there 2 solutions really, either dynamic sizing or calculating pixel width required given string, font.measure() mentioned doesn't seem applicable python 3.x
any great, i'd tiny thing sorted can finish up!
sql.connect(dbfile) dbconnection: cursor = dbconnection.cursor() self.treestudentlog = ttk.treeview(self.mainframe, columns=("type","points", "notes"), height=21) yscrollbar = ttk.scrollbar(self.mainframe, orient='vertical', command=self.treestudentlog.yview) xscrollbar = ttk.scrollbar(self.mainframe, orient='horizontal', command=self.treestudentlog.xview) yscrollbar.place(x=822, y=240, height=440) xscrollbar.place(x=10, y=678, width=813) self.treestudentlog.configure(yscroll=yscrollbar.set, xscroll=xscrollbar.set) self.treestudentlog.heading("#0", text="student") self.treestudentlog.column("#0", minwidth=180) self.treestudentlog.heading("type", text="log type") self.treestudentlog.column("type", minwidth=160) self.treestudentlog.heading("points", text="points") self.treestudentlog.column("points", minwidth=60) self.treestudentlog.heading("notes", text="teacher comments") #populate student log cursor.execute("select * studentlogs order logid desc limit 100") alllogs = cursor.fetchall() notelength = 0 log in alllogs: lognotes = log[3] print(type(lognotes)) try: if len(lognotes) > notelength: notelength = len(lognotes) except: notelength = notelength cursor.execute("select * users userid = ?", (log[2],)) studentdetails = cursor.fetchone() cursor.execute("select * logtypes typeid = ?", (log[1],)) logtypedetails = cursor.fetchone() logdesc = logtypedetails[2] logvalue = logtypedetails[1] studentname = studentdetails[1] + " " + studentdetails[2] self.treestudentlog.insert('', 'end', text=studentname, values=(logdesc, logvalue, lognotes)) self.treestudentlog.column("notes", minwidth=notelength * 8) self.treestudentlog.place(x=10, y=240, width=812)
edit: code i'm using define font:
import tkinter tk tkinter import font treefont = font(self.mainwindow, "calibri", "12")
where self.mainwindow current top level window (not root, i've tried too). i've tried tk.font(blah) same effect.
i end getting typeerror "module" object not callable
Comments
Post a Comment