Double Underline text Tkinter - python

please I need help using double underlines for a text in tkinter.
Sample code is:
allbl=tk.Label(hmpage,text='Purchases', font='calibri 25 bold doubleunderline',fg='white',bg='#2a475e')
allbl.place(relx=0.45,rely=0.25,anchor="center")

There is no font option for double underlining, therefore this cannot be done with a simple Label widget. However, one can create a custom class based on a Canvas to draw two lines below the text:
import tkinter as tk
from tkinter.font import Font
class DblUnderlineLabel(tk.Canvas):
def __init__(self, master, **kw):
# collect text's properties
font = Font(master, kw.pop('font', ''))
text = kw.pop('text', '')
if 'fg' in kw:
fg = kw.pop('fg')
else:
fg = kw.pop('foreground', 'black')
# initialize the canvas
tk.Canvas.__init__(self, master, **kw)
# display the text
self.create_text(2, 2, anchor='nw', font=font, text=text, fill=fg, tags='text')
h = font.metrics('linespace') # font property needed to position correctly the underlining
bbox = self.bbox('text') # get text bounding box in the canvas
w = font.actual('size')//8 # scale thickness of the underlining with fontsize
# create the double underlining
self.create_line(bbox[0], h - 1, bbox[2], h - 1, fill=fg, width=w, tags='line')
self.create_line(bbox[0], h + int(1.1*w), bbox[2], h + int(1.1*w), fill=fg, width=w, tags='line')
# resize the canvas to fit the text
bbox = self.bbox('all')
self.configure(width=bbox[2], height=bbox[3])
root = tk.Tk()
for size in [8, 16, 32, 64]:
DblUnderlineLabel(root, text="Arial %i bold" % size, font="Arial %i bold" % size).pack()
root.mainloop()
Text sample:

It seems that you are using the font attribute incorrectly.
I would say...
Instead of writing this:
font='calibri 25 bold doubleunderline'
Write this:
font=('calibri', 25, 'bold underline')
Also I didn't write doubleunderline as nothing like that exists in tkinter so you would get a straight Traceback.
So the corrected code would be:
allbl=tk.Label(hmpage,text='Purchases', font=('calibri', 25, 'bold underline'),fg='white',bg='#2a475e')
allbl.place(relx=0.45,rely=0.25,anchor="center")

After a long search, I have to terms with the sad reality as at this time of writing that tkinter doesn't support the double-underline feature.

Related

Need help Dynamically resizing my text to fit in the tkinter canvas

I was working on a small python script lately when I came across this problem. I tried to create a canvas in a strip shape and write text into it expecting that the text would auto adjust itself to the boundaries of canvas(similar to how a text box works in word processing software). But the text is apparently going out of boundaries.
ScreenShot
Code
from tkinter import *
top = Tk()
top.geometry("130x370")
c = Canvas(top,bg = "pink",height = "370")
c.create_text(30,30,fill="darkblue",font="Times 20 italic bold",text="Hey There!")
c.pack()
top.mainloop()
Firstly, the .create_text() method of the Canvas has a width option which sets the maximum width of the text beyond which it is wrapped. To get a dynamical effect when resizing the window, this width option can be changed in a function bound to the <Configure> event (the resize() function in the example below).
Secondly, to check that the text fits vertically in the canvas, I use the .bbox(item_id) method of the Canvas to get the coordinates of the bounding box of the text. Then, I decrement the fontsize as long as the bottom of the text is lower than the bottom of the canvas.
Here is the example:
import tkinter as tk
top = tk.Tk()
top.geometry("130x370")
def resize(event):
font = "Times %i italic bold"
fontsize = 20
x0 = c.bbox(text_id)[0] # x-coordinate of the left side of the text
c.itemconfigure(text_id, width=c.winfo_width() - x0, font=font % fontsize)
# shrink to fit
height = c.winfo_height() # canvas height
y1 = c.bbox(text_id)[3] # y-coordinate of the bottom of the text
while y1 > height and fontsize > 1:
fontsize -= 1
c.itemconfigure(text_id, font=font % fontsize)
y1 = c.bbox(text_id)[3]
c = tk.Canvas(top, bg="pink", height="370")
text_id = c.create_text(30, 30, anchor="nw", fill="darkblue", font="Times 20 italic bold", text="Hey There!")
c.pack(fill="both", expand=True)
c.bind("<Configure>", resize)
top.mainloop()
Also note that I set the anchor of the text to north west in .create_text() so that (30, 30) are the coordinates of the top-left corner of the text and not of the center to ensure that the start of the text is visible.

Tkinter.text - How to calculate the height of a dynamic string?

I have a Text widget that holds a custom string that contains \n chars (multiple lines).
The widget is placed within a vertical panedwindow which I want to adjust the panedwindow's sash to display the whole string in the Text widget.
The string is dynamic by nature (which means, it is being updated by other methods in my application).
As the Text widget is configured with wrap='word', how can I calculate the string height in pixels to adjust the sash accordingly?
I tried to use text.dlineInfo('end -1c')[1] + text.dlineinfo('end -1c')[3] (for line's y coordinate + height) after the string was loaded to the widget. The problem is that if the last line is not visible, then dlineinfo returns none.
I also tried to use Font.measure routine, but this doesn't include wrap aspects of the Text widget.
Here is a Minimal, Complete, and Verifiable example:
import tkinter
from tkinter import scrolledtext
class GUI():
def __init__(self, master):
self.master = master
self.body_frame = tkinter.PanedWindow(self.master, orient='vertical', sashwidth=4)
self.body_frame.pack(expand=1, fill='both')
self.canvas_frame = tkinter.Frame(self.body_frame)
self.description_frame = tkinter.Frame(self.body_frame)
self.body_frame.add(self.canvas_frame, sticky='nsew')
self.body_frame.add(self.description_frame, sticky='nsew')
tkinter.Button(self.canvas_frame, text='Update Text', command = lambda : self.update_text("""
A very long string with new lines
A very long string with new lines
A very long string with new lines
A very long string with new lines
A very long string with new lines
A very long string with new lines
""")).pack(fill='x')
self.field_description = scrolledtext.ScrolledText(self.description_frame, width=20, wrap='word')
self.field_description.pack(expand=1, fill='both')
self.master.update()
self.body_frame.sash_place(0,0,self.body_frame.winfo_height() - 50) # force sash to be lower
def update_text(self, description):
self.field_description.delete('1.0', 'end')
self.field_description.insert('1.0', description)
height = self.body_frame.winfo_height()
lastline_index = self.field_description.index('end - 1c')
text_height = self.field_description.dlineinfo(lastline_index)[1] + \
self.field_description.dlineinfo(lastline_index)[3]
self.body_frame.sash_place(0, 0, height - text_height)
root = tkinter.Tk()
my_gui = GUI(root)
root.mainloop()
I don't know of any built-in method that returns the total number of lines (including wrapped lines) in a tkinter Text widget.
However, you can manually calculate this number by comparing the lengths of the unbroken strings in the Text widget to the Text widget's exact width (minus padding). This is what the LineCounter class below does:
# python 2.x
# from tkFont import Font
# python 3.x
from tkinter.font import Font
class LineCounter():
def __init__(self):
"""" This class can count the total number of lines (including wrapped
lines) in a tkinter Text() widget """
def count_total_nb_lines(self, textWidget):
# Get Text widget content and split it by unbroken lines
textLines = textWidget.get("1.0", "end-1c").split("\n")
# Get Text widget wrapping style
wrap = text.cget("wrap")
if wrap == "none":
return len(textLines)
else:
# Get Text widget font
font = Font(root, font=textWidget.cget("font"))
totalLines_count = 0
maxLineWidth_px = textWidget.winfo_width() - 2*text.cget("padx") - 1
for line in textLines:
totalLines_count += self.count_nb_wrapped_lines_in_string(line,
maxLineWidth_px, font, wrap)
return totalLines_count
def count_nb_wrapped_lines_in_string(self, string, maxLineWidth_px, font, wrap):
wrappedLines_count = 1
thereAreCharsLeftForWrapping = font.measure(string) >= maxLineWidth_px
while thereAreCharsLeftForWrapping:
wrappedLines_count += 1
if wrap == "char":
string = self.remove_wrapped_chars_from_string(string,
maxLineWidth_px, font)
else:
string = self.remove_wrapped_words_from_string(string,
maxLineWidth_px, font)
thereAreCharsLeftForWrapping = font.measure(string) >= maxLineWidth_px
return wrappedLines_count
def remove_wrapped_chars_from_string(self, string, maxLineWidth_px, font):
avgCharWidth_px = font.measure(string)/float(len(string))
nCharsToWrap = int(0.9*maxLineWidth_px/float(avgCharWidth_px))
wrapLine_isFull = font.measure(string[:nCharsToWrap]) >= maxLineWidth_px
while not wrapLine_isFull:
nCharsToWrap += 1
wrapLine_isFull = font.measure(string[:nCharsToWrap]) >= maxLineWidth_px
return string[nCharsToWrap-1:]
def remove_wrapped_words_from_string(self, string, maxLineWidth_px, font):
words = string.split(" ")
nWordsToWrap = 0
wrapLine_isFull = font.measure(" ".join(words[:nWordsToWrap])) >= maxLineWidth_px
while not wrapLine_isFull:
nWordsToWrap += 1
wrapLine_isFull = font.measure(" ".join(words[:nWordsToWrap])) >= maxLineWidth_px
if nWordsToWrap == 1:
# If there is only 1 word to wrap, this word is longer than the Text
# widget width. Therefore, wrapping switches to character mode
return self.remove_wrapped_chars_from_string(string, maxLineWidth_px, font)
else:
return " ".join(words[nWordsToWrap-1:])
Example of use:
import tkinter as tk
root = tk.Tk()
text = tk.Text(root, wrap='word')
text.insert("1.0", "The total number of lines in this Text widget is " +
"determined accurately, even when the text is wrapped...")
lineCounter = LineCounter()
label = tk.Label(root, text="0 lines", foreground="red")
def show_nb_of_lines(evt):
nbLines = lineCounter.count_total_nb_lines(text)
if nbLines < 2:
label.config(text="{} line".format(nbLines))
else:
label.config(text="{} lines".format(nbLines))
label.pack(side="bottom")
text.pack(side="bottom", fill="both", expand=True)
text.bind("<Configure>", show_nb_of_lines)
text.bind("<KeyRelease>", show_nb_of_lines)
root.mainloop()
In your specific case, the height of the wrapped text in your ScrolledText can be determined in update_text() as follows:
from tkinter.font import Font
lineCounter = LineCounter()
...
class GUI():
...
def update_text(self, description):
...
nbLines = lineCounter.count_total_nb_lines(self.field_description)
font = Font(font=self.field_description.cget("font"))
lineHeight = font.metrics("linespace")
text_height = nbLines * lineHeight
...
You know the number of lines in your Text. And you can tell when a line is off the scrolled region when dlineinfo returns None. So go through each line and "see" it, to make sure it's visible before you run the dlineinfo() call on it. Then sum them all up, and that's the minimum new height you need for the lines to all appear at the current width. From the height of a line's bbox and the height of the biggest font in the line, you can determine if the line is wrapped, and if so, how many times, if you care about that. The trick is to then use paneconfig() to modify the height of the paned window. Even if the child window would resize automatically normally, the paned window will not. It must be told to resize through the paneconfig() call.
If you "see" each line before measuring, you'll get all the measurements. And "seeing" each line shouldn't be a big deal since you intend to show them all at the end anyway.

When using python Tkinter, how can I stop two entry box's in the same class showing the same input text?

I am having this issue with Python Tkinter. I am trying to make a user interface form screen which requires the user to enter values into entry box's displayed on screen. I have set it so the two Entry Box's are in the same class (that class being the interface screen). The problem is that while I type into one of the box's, the text which I type not only displays in the box in which I am typing into, but also in the other box.
Below is the code in question.
class GenericSkeleton: # The template for all the screens in the program
def __init__(self):
self.GenericGui = Tk()
self.GenericGui.title('Radial Arc Calculator')
self.GenericGui.geometry('360x540')
self.GenericGui.resizable(width = FALSE, height = FALSE)
Label(self.GenericGui,text = 'Radial Arc Calculator',font = ('Ariel',18)).place(x=65,y=35)
def destroy(self):
self.GenericGui.destroy()
class InputScreen(GenericSkeleton):
def __init__(self):
GenericSkeleton.__init__(self)
Button(self.GenericGui,text = 'CALCULATE',height = 1, width = 25, command = calculate, font = ('TkDefaultFont',14)).place(x=37,y=400)
Button(self.GenericGui,text = 'CLOSE',height = 1, width = 11, command = close, font = ('TkDefaultFont',14)).place(x=37, y=450)
Button(self.GenericGui,text = 'HELP', height = 1, width = 11, command = DisplayHelp, font = ('TkDefaultFont',14)).place(x=190, y=450)
Label(self.GenericGui,text = 'Enter Radius (mm):', font = ('TkDefaultFont',14)).place(x=37, y=180)
Label(self.GenericGui,text = 'Enter point distance (mm):', font = ('TkDefaultFont',14)).place(x=37, y=250)
Entry(self.GenericGui,textvariable = Radius, width = 10, font = ('TkDefaultFont',14)).place(x=210, y=180)
Entry(self.GenericGui,textvariable = Distance, width = 5, font = ('TkDefaultFont',14)).place(x=265, y=250)
run = InputScreen()
The entry box's are at the bottom of the code, I hope its enough/not too much to solve the problem.
The problem is that they both share the same textvariable (you use different variable names, but they have the same value which makes them the same in the eyes of tkinter). My advice is to not use the textvariable attribute. You don't need it.
However, if you remove the use of textvariable then you need to separate your widget creation from widget layout so that you can keep a reference to the widget. Then you can use the get method on the widget (rather than on the variable) to get the value:
self.entry1 = Entry(...)
self.entry2 = Entry(...)
self.entry1.place(...)
self.entry2.place(...)
Later, you can get the values like this:
radius = int(self.entry1.get())
distance = int(self.entry2.get())
If you do need the textvariable (usually only if you're using the trace feature of a tkinter variable), you must use a tkinter variable (StringVar, IntVar, etc) rather than a regular variable.

Python Megawidgets tab widths

How to I change the default width of the tabs on a notebook page?
import tkinter #widget library ships with Python
import Pmw #allows access to update tkinter widgets
class TextBookGUI:
# class constructor
# populates each note book page
def __init__(self, master):
#place hash tables here
"""
Create 5 pages on using Pmw notebook widget.
Documenation for notebook:
http://pmw.sourceforge.net/doc/NoteBook.html
"""
self.nb = Pmw.NoteBook(master)
Pmw.Color.changecolor(self.nb.component('hull'), background='blue')
self.HomeTab = self.nb.add("Welcome")
self.nb.tab('Welcome').focus_set()
self.StudentTab = self.nb.add("Students")
self.BookTab = self.nb.add("Books")
self.LoanTab = self.nb.add("Loans")
self.HelpTab = self.nb.add("Help")
self.nb.pack(fill = 'both', expand = 1, padx = 10, pady = 10)
self.nb.setnaturalsize()
#format the house style of tabs: yellow bg and blue text
self.nb.component('Welcome-tab').configure(font= ('Helvetica', 7 ,'bold', 'italic'), width= 30,fg= "yellow", bg="blue")
I need to set the default tab width to be wider to accomodate the tab names. I cannot access the tab width property. The last line I tried using .configure(width=30), this had no effect on the tab!
OK , I have got the solution , although ugly. It does manipulate tab widths.
class TextBookGUI:
# class constructor
# populates each note book page
def __init__(self, master):
#place hash tables here
"""
Create 5 pages on using Pmw notebook widget.
Documenation for notebook:
http://pmw.sourceforge.net/doc/NoteBook.html
"""
Pmw.initialise(master)
self.nb = Pmw.NoteBook(master,borderwidth=2,arrownavigation=True,tabpos='n')
self.HomeTab = self.nb.add("Welcome")
self.nb.tab('Welcome').focus_set()
self.StudentTab = self.nb.add("Students")
self.BookTab = self.nb.add("Books")
self.LoanTab = self.nb.add("Loans")
self.HelpTab = self.nb.add("Help")
*self.nb._pageAttrs['Welcome']['tabreqwidth'] = 200
self.nb._pageAttrs["Welcome"]['tabreqheight'] = 100
self.nb._pageAttrs["Students"]['tabreqwidth'] = 200
self.nb._pageAttrs["Students"]['tabreqheight'] = 100
self.nb._pageAttrs["Books"]['tabreqwidth'] = 200
self.nb._pageAttrs["Books"]['tabreqheight'] = 100
self.nb._pageAttrs['Loans']['tabreqwidth'] = 200
self.nb._pageAttrs["Loans"]['tabreqheight'] = 100
self.nb._pageAttrs['Help']['tabreqwidth'] = 200
self.nb._pageAttrs["Help"]['tabreqheight'] = 100*
#format the house style of tabs: yellow bg and blue text
self.nb.component('Welcome-tab').configure(font= ('Helviticva',20 ,'bold italic'),
fg= "yellow",bg="blue",wraplength=150)
self.nb.component('Students-tab').configure(font= ('Helviticva',20 ,'bold italic'),
fg= "yellow",bg="blue",wraplength=150)
self.nb.component('Books-tab').configure(font= ('Helviticva',20 ,'bold italic'),
fg= "yellow",bg="blue",wraplength=150)
self.nb.component('Loans-tab').configure(font= ('Helviticva',20 ,'bold italic'),
fg= "yellow",bg="blue",wraplength=150)
self.nb.component('Help-tab').configure(font= ('Helviticva',20,'bold italic'),
fg= "yellow",bg="blue",wraplength=150)
self.nb.pack(fill = 'both', expand = 1, padx = 10, pady = 10)
self.nb.setnaturalsize()
Having searched the net ,someone suggested iterating the page attribute dictionary with iterrkeys(). This did not work so I took the code out of the loop and used the attributes from the pageAttrs dictionary, which were were [reqtabwidth], [reqtabheight]. It worked!The code has stars around it and was added to the code above.You can see the text size is now 20 and it displays in a nice large tab.That is two evenings wasted for such a simple task.
Annoyingly, while you've correctly set the tab width within the widget (I can see it using cget('width'), the megawidget doesn't appear to honor the width setting.
However, it does resize each tab to ensure that it's wide enough to correctly display the text. If you're not seeing the full text of each tab, it may be because the root window isn't sized wide enough to allow the full width to be displayed. Try adding a forced size (e.g., root.geometry('500x200')) and see if that helps.
I'd love to have a better answer for you, but that's the best I can do. Good luck!

TkInter Label Change Font Size by Text Length

Good morning,
I have a Tkinter label with a fixed width. In this label I have set a dynamic text. I need to change the font size (decrease or increase) when the text width is longer than label width.
This is an example:
To do this you need to give the label a unique font, and then use the measure method of the font to compute how much space is needed for a given string in that font. Then you just need to keep increasing or decreasing the font size until it fits in the label.
A simple way to create a label with a custom font looks something like this (for python 2.x; for 3.x the imports will be a little different):
import Tkinter as tk
import tkFont
label = tk.Label(...)
original_font = tkFont.nametofont(label.cget("font"))
custom_font = tkFont.Font()
custom_font.configure(**original_font.configure())
label.configure(font=custom_font)
Now you can use custom_font.measure(...) to figure out how many pixels you need for the label at the current font size. If the number of pixels is too big, change the size of the font and measure again. Repeat, until the font is just big enough to hold the text.
When you change the size of the font, the label will automatically redraw the text in the new font size.
Here's a complete working example that illustrates the technique:
import Tkinter as tk
import tkFont
class DynamicLabel(tk.Label):
def __init__(self, *args, **kwargs):
tk.Label.__init__(self, *args, **kwargs)
# clone the font, so we can dynamically change
# it to fit the label width
font = self.cget("font")
base_font = tkFont.nametofont(self.cget("font"))
self.font = tkFont.Font()
self.font.configure(**base_font.configure())
self.configure(font=self.font)
self.bind("<Configure>", self._on_configure)
def _on_configure(self, event):
text = self.cget("text")
# first, grow the font until the text is too big,
size = self.font.actual("size")
while size < event.width:
size += 1
self.font.configure(size=size)
# ... then shrink it until it fits
while size > 1 and self.font.measure(text) > event.width:
size -= 1
self.font.configure(size=size)
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.label = DynamicLabel(self, text="Resize the window to see the font change", width=20)
self.label.pack(fill="both", expand=True, padx=20, pady=20)
parent.geometry("300x200")
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(fill="both", expand=True)
root.mainloop()

Categories

Resources