472,352 Members | 1,485 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,352 software developers and data experts.

Retrieve Tkinter listbox item by string, not by index

I'm trying to set the active item in a Tkinter listbox to my
application's currently-defined default font.

Here's how I get the fonts loaded into the listbox:

self.fonts=list(tkFont.families())
self.fonts.sort()

for item in self.fonts:
self.fontlist.insert(END, item) #self.fontlist is the
ListBox instance
So far, so good. But I don't know how to set the active selection in the
listbox to the default font. All the methods for getting or setting a
selection in the listbox are based on index, not a string. And using
standard list search methods like this:

if "Courier" in self.fontlist:
print "list contains", value
else:
print value, "not found"

returns an error:

TypeError: cannot concatenate 'str' and 'int' objects

So I'm stuck. Can someone point me in the right direction?
--
Kevin Walzer
Code by Kevin
http://www.codebykevin.com
Dec 22 '06 #1
3 4146
Kevin Walzer wrote:
I'm trying to set the active item in a Tkinter listbox to my
application's currently-defined default font.

Here's how I get the fonts loaded into the listbox:

self.fonts=list(tkFont.families())
self.fonts.sort()

for item in self.fonts:
self.fontlist.insert(END, item) #self.fontlist is the
ListBox instance
So far, so good. But I don't know how to set the active selection in the
listbox to the default font. All the methods for getting or setting a
selection in the listbox are based on index, not a string. And using
standard list search methods like this:

if "Courier" in self.fontlist:
print "list contains", value
else:
print value, "not found"

returns an error:

TypeError: cannot concatenate 'str' and 'int' objects

So I'm stuck. Can someone point me in the right direction?
I would keep a separate data structure for the fonts and update the
scrollbar when the list changed. This would help to separate the
representation from the data represented. Here is a pattern I have found
most useful and easy to maintain:

# untested
class FontList(Frame):
def __init__(self, *args, **kwargs):
Frame.__init__(self, *args, **kwargs)
self.pack()
self.fonts = list(kwargs['fonts'])
self.default = self.fonts.index(kwargs['default_font'])
self.lb = Listbox(self)
# add scrollbar for self.lb, pack scrollbar
# pack self.lb
self.set_bindings()
self.update()
def set_bindings(self):
# put your bindings and behavior here for FontList components
def update(self):
self.lb.delete(0, END)
for f in self.fonts:
self.lb.insert(f)
self.highlight()
def highlight(self):
index = self.default
self.lb.see(index)
self.lb.select_clear()
self.lb.select_adjust(index)
self.lb.activate(index)
def change_font(self, fontname):
self.default = self.fonts.index(fontname)
self.highlight()
def add_font(self, fontname, index=None):
if index is None:
self.fonts.append(fontname)
else:
self.fonts.insert(index, fontname)
self.update()
# other methods for adding multiple fonts or removing them, etc.
--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Dec 23 '06 #2
James Stroud wrote:
Kevin Walzer wrote:
>I'm trying to set the active item in a Tkinter listbox to my
application's currently-defined default font.

Here's how I get the fonts loaded into the listbox:

self.fonts=list(tkFont.families())
self.fonts.sort()

for item in self.fonts:
self.fontlist.insert(END, item) #self.fontlist is the
ListBox instance
So far, so good. But I don't know how to set the active selection in
the listbox to the default font. All the methods for getting or
setting a selection in the listbox are based on index, not a string.
And using standard list search methods like this:

if "Courier" in self.fontlist:
print "list contains", value
else:
print value, "not found"

returns an error:

TypeError: cannot concatenate 'str' and 'int' objects

So I'm stuck. Can someone point me in the right direction?


I would keep a separate data structure for the fonts and update the
scrollbar when the list changed. This would help to separate the
representation from the data represented. Here is a pattern I have found
most useful and easy to maintain:

# untested
class FontList(Frame):
def __init__(self, *args, **kwargs):
Frame.__init__(self, *args, **kwargs)
self.pack()
self.fonts = list(kwargs['fonts'])
self.default = self.fonts.index(kwargs['default_font'])
self.lb = Listbox(self)
# add scrollbar for self.lb, pack scrollbar
# pack self.lb
self.set_bindings()
self.update()
def set_bindings(self):
# put your bindings and behavior here for FontList components
def update(self):
self.lb.delete(0, END)
for f in self.fonts:
self.lb.insert(f)
self.highlight()
def highlight(self):
index = self.default
self.lb.see(index)
self.lb.select_clear()
self.lb.select_adjust(index)
self.lb.activate(index)
def change_font(self, fontname):
self.default = self.fonts.index(fontname)
self.highlight()
def add_font(self, fontname, index=None):
if index is None:
self.fonts.append(fontname)
else:
self.fonts.insert(index, fontname)
self.update()
# other methods for adding multiple fonts or removing them, etc.

I overlooked that you will actually want to remove "fonts" and
"default_fonts" from kwargs before initializing with Frame:

# untested
class FontList(Frame):
def __init__(self, *args, **kwargs):
self.fonts = list(kwargs['fonts'])
self.default = self.fonts.index(kwargs['default_font'])
kwargs.pop('fonts')
kwargs.pop('default_font')
Frame.__init__(self, *args, **kwargs)
self.pack()
self.lb = Listbox(self):
# etc.

James

--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095

http://www.jamesstroud.com/
Dec 23 '06 #3
"Kevin Walzer" <kw@codebykevin.comwrote:

I'm trying to set the active item in a Tkinter listbox to my
application's currently-defined default font.
not sure if you can mix fonts in a listbox - the font option
when you create the listbox instance seems to apply globally
to all the lines in the box

Here's how I get the fonts loaded into the listbox:

self.fonts=list(tkFont.families())
self.fonts.sort()

for item in self.fonts:
self.fontlist.insert(END, item) #self.fontlist is the
ListBox instance
Does this actually give you different fonts on each line? or just
a list of available font names, all displayed in the same font?
>
So far, so good. But I don't know how to set the active selection in the
listbox to the default font. All the methods for getting or setting a
selection in the listbox are based on index, not a string. And using
standard list search methods like this:

if "Courier" in self.fontlist:
print "list contains", value
else:
print value, "not found"

returns an error:

TypeError: cannot concatenate 'str' and 'int' objects

So I'm stuck. Can someone point me in the right direction?
not too sure I understand what you are trying to do - but consider:

idx = self.fontlist.curselection()

this is the index of the selection,
(as controlled by the user's fingers) so:

StringAtCurrSelection = self.fontlist.get(idx)

should be the string containing the text on the selected line

you can use self.fontlist.delete(idx) and
self.fontlist.insert(idx,SomeString) to make changes to the text.

But as to how you change this to display in a different font -
no can tell - don't know how, and not sure if its possible.
Only know that you can use configure to change the font
for all the lines in the box by changing the box's font option...

if, OTOH you are just trying to find where your default
lives in the box, why don't you look for it and remember
the index when you are populating the box?

something like this:

self.idx = 0
for item in self.fonts:
if item == mydefaultfont:
self.defidx = idx
self.fontlist.insert(idx, item)
idx += 1

then you can do whatever with the item that
lives at self.defidx afterwards...

hth - Hendrik

Dec 23 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Zhang Le | last post by:
Hello, Is there a quick way to replace the content of a single item in tkinter's listbox? Currently my solution is to first delete the item, then...
3
by: Harlin Seritt | last post by:
I've created a ghetto-ized ComboBox that should work nicely for Tkinter (unfortunately no dropdown capabilities yet). I've found why it's such a...
1
by: Patty O'Dors | last post by:
Hi I have some code to create an ownerdrawn listbox (derived), and when I add an item to it, the bold text of the first item (the title,...
1
by: Edward | last post by:
I am having a terrible time getting anything useful out of a listbox on my web form. I am populating it with the results from Postcode lookup...
3
by: Mitch | last post by:
Is the following a correct representation of the relationship of the selected index to selected item: INDEX ITEM 0 "item 1" 1 ...
3
by: vedran_dekovic | last post by:
Hi, I need help about Tkinter listbox widget.I want,when somebody click on any item(file) in Listbox,then in new Label widget text must be...
2
by: vedran_dekovic | last post by:
Hi, Again I need help about tkinter listbox. example: In listbox must write (imaginary file in server): vedran@vedran.byethost12.com...
2
by: Kodiak | last post by:
I am currently trying to retrieve a certain item in a listbox by passing in the window handle and the string value of the item I am looking for. I...
0
by: Mark Smith | last post by:
hi i use an ownerdraw method for coloring some items in the list different then the others. code: private void ListBoxDrawItem(object...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.