How to highlight Label/Text in tkinter ?

VGNU Linux vgnulinux at gmail.com
Wed Nov 3 06:47:44 EDT 2010


I was able to solve the problem. But unable to use Label or Text widget. Any
idea as to why focus does not work with Label or Text widget.
Here is the program using button widget.

from Tkinter import *
import tkFont

class DisplayFrame:
    """
    Uses GUI to display contacts
    """
    def __init__(self, root):
        self.b = Button(root, text="Peter Pan",font=("Arial",10,"bold"),
                          anchor=W, justify=LEFT, relief=FLAT)
        self.b.pack(fill=BOTH)
        self.b.focus_set()

        self.b = Button(root, text="Marry Joe", font=("Arial",10,"bold"),
                         anchor=W, justify=LEFT, relief=FLAT)
        self.b.pack(fill=BOTH)

        self.b = Button(root, text="Michael Kin", font=("Arial",10,"bold"),
                         anchor=W, justify=LEFT, relief=FLAT)
        self.b.pack(fill=BOTH)

        self.b = Button(root, text="Jennifer McConelly",
font=("Arial",10,"bold"),
                         anchor=W, justify=LEFT, relief=FLAT)
        self.b.pack(fill=BOTH)

        self.b = Button(root, text="Jonathen Broady",
font=("Arial",10,"bold"),
                         anchor=W, justify=LEFT, relief=FLAT)
        self.b.pack(fill=BOTH)

def handleReturn(event):
    event.widget["font"] = "Arial 16 bold"
    print event.widget.focus_get()
    nxtwid = event.widget.tk_focusNext()
    prewid = event.widget.tk_focusPrev()
    prewid["font"] = "Arial 10 bold"
    nxtwid.focus_set()

if __name__ == '__main__':
    root = Tk()
    frame = DisplayFrame(root)
    root.bind("<Return>",handleReturn)
    root.mainloop()

Regards,
VGNU

On Wed, Nov 3, 2010 at 12:02 PM, VGNU Linux <vgnulinux at gmail.com> wrote:

> Hi,
> I am writing a program teach myself python and tkinter.
> Below given is a program which displays label with different fonts and
> sizes.
> How to highlight text and change font size if the up/down arrow keys are
> pressed ?
> from Tkinter import *
> import tkFont
>
> class MyFrame(Frame):
>
>     def __init__(self, root):
>         Frame.__init__(self, root)
>         self.txt = Label(self, text="Arial 10 bold",
> font=("Arial",10,"bold"))
>         self.txt.grid(row=1,column=1)
>         self.txt = Label(self, text="Courier 12 bold",
> font=("Courier",12,"bold"))
>         self.txt.grid(row=2,column=1)
>         self.txt = Label(self, text="Comic Sans MS 14 bold", font=("Comic
> Sans MS",14,"bold"))
>         self.txt.grid(row=3,column=1)
>         self.txt = Label(self, text="Fixedsys 16 bold",
> font=("Fixedsys",16,"bold"))
>         self.txt.grid(row=4,column=1)
>         self.txt = Label(self, text="MS Sans Serif 18 bold", font=("MS Sans
> Serif",18,"bold"))
>         self.txt.grid(row=5,column=1)
>         self.txt = Label(self, text="MS Serif, Symbol 20 bold", font=("MS
> Serif, Symbol",20,"bold"))
>         self.txt.grid(row=6,column=1)
>         self.txt = Label(self, text="System 22 bold",
> font=("System",22,"bold"))
>         self.txt.grid(row=7,column=1)
>         self.txt = Label(self, text="Verdana 24 bold",
> font=("Verdana",24,"bold"))
>         self.txt.grid(row=8,column=1)
>
> if __name__ == '__main__':
>     root = Tk()
>     c = MyFrame(root)
>     c.pack(fill=BOTH, expand=1)
>     root.mainloop()
>
> any help?
>
> Regards,
> VGNU
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20101103/b3e2a8a2/attachment-0001.html>


More information about the Python-list mailing list