Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news.mb-net.net!open-news-network.org!.POSTED!not-for-mail From: Bart Kastermans Newsgroups: comp.lang.python Subject: Re: Tkinter label height to fit content Date: Tue, 06 Sep 2011 16:00:51 -0600 Organization: MB-NET.NET for Open-News-Network e.V. Lines: 143 Message-ID: <87k49l49gs.fsf@gmail.com> References: <87mxelgtmx.fsf@gmail.com> <69ce4e11-5ef4-4b81-b66b-87d1017b1ec3@s7g2000yqk.googlegroups.com> <87obz0w0ze.fsf@gmail.com> <8d7cf6e4-5794-4f57-88ae-b0d0dcfd68a6@d25g2000yqh.googlegroups.com> <87obyx4ed4.fsf@gmail.com> <8777abed-e2b1-469e-96e5-662c535220be@y21g2000yqy.googlegroups.com> NNTP-Posting-Host: kasterma.colorado.edu Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: gwaiyur.mb-net.net 1315346456 25432 128.138.150.65 (6 Sep 2011 22:00:56 GMT) X-Complaints-To: abuse@open-news-network.org NNTP-Posting-Date: Tue, 6 Sep 2011 22:00:56 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) X-User-ID: U2FsdGVkX1/fcZkGKXAwBmo2mLdeGOQOQgwV2fHvdZb1g1ju7XxNF7CaxEDVU6ds Cancel-Lock: sha1:UqRdnC2aJODaIrM20bS3X0XYd20= sha1:hm/85cWBnonaEb85oa07MTR+0fc= Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:12856 rantingrick writes: > Hmm, i can replace all that code with this... Because I stupidly forgot to repeat the original problem I had, and my code doesn't show it (and doesn't show the correct use of the function I wrote). The code shows that I now know how to compute the number of lines and item will have; in the actual program I am developing I will take the max of these numbers and make all items that height. This means the code I should have shown is as follows (here I first compute the maximum height needed for any item, and then show all items using this height). Also in the actual program there will be scrolling options to change the item shown by the different labels. import Tkinter as Tk import tkFont import random import sys def genstr (j): rno = random.randint(4,50) ret_val = str(j) + ":" for i in range (0, rno): ret_val += "hello" + str(i) return ret_val def gendata (lh): ret_val = [] for i in range(0,lh): ret_val.append (genstr (i)) return ret_val data = gendata (100) root = Tk.Tk() font = tkFont.Font(family='times', size=13) class lines: def __init__ (self): self.lastct = 1 # remember where the cutoff was last work from there def count (self, text, cutoff = 400): global font no_lines = 1 start_idx = 0 idx = self.lastct while True: if idx > len (text): idx = len (text) # shrink from guessed value while font.measure (text[start_idx:idx - 1]) > cutoff: if idx <= start_idx: print "error" sys.exit () else: idx -= 1 self.lastct = idx - start_idx # adjust since was too big # increase from guessed value (note: if first shrunk then done) while (idx < len (text) and font.measure (text[start_idx:idx]) < cutoff): idx += 1 self.lastct = idx - start_idx # adjust since was too small # next line has been determined print "*" + text[start_idx:idx-1] + "*" if idx == len(text) and font.measure (text[start_idx:]) < cutoff: return no_lines elif idx == len(text): return no_lines + 1 else: no_lines += 1 start_idx = idx - 1 idx = start_idx + self.lastct lin = lines() max_ht = 0 for i in range(0,len(data)): ct = lin.count(data[i], 450) if ct > max_ht: max_ht = ct for i in range(0,min(len(data),5)): l = Tk.Label(root) l.pack() l['text'] = data[i] l['width'] = 50 l['justify'] = Tk.LEFT l['anchor'] = 'w' l['wraplength'] = 450 l['padx']=10 l['pady'] = 5 l['height'] = max_ht l['font'] = font if i % 2 == 0: l['background'] = 'grey80' else: l['background'] = 'grey70' root.mainloop() > > # > # Easy_as.py > # > import Tkinter as tk > from ScrolledText import ScrolledText > import tkFont > import random > # Create some puesdo data. > data = [ > '{0}.{1}'.format(x, 'blah'*random.randint(4, 50)) > for x in range(100) > ] > ##print data > # Create the main window and a scrolled text widget. > root = tk.Tk() > font = tkFont.Font(family='times', size=13) > textbox = ScrolledText( > root, > width=60, > height=20, > font=('Times', 10), > wrap=tk.WORD, > ) > textbox.pack( > fill=tk.BOTH, > expand=True, > padx=5, > pady=5, > ) > textbox.insert(1.0, '\n\n'.join(data)) > # Start the event loop. > root.mainloop() > # > # End > #