473,666 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.familie s())
self.fonts.sort ()

for item in self.fonts:
self.fontlist.i nsert(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 4296
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.familie s())
self.fonts.sort ()

for item in self.fonts:
self.fontlist.i nsert(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.inde x(kwargs['default_font'])
self.lb = Listbox(self)
# add scrollbar for self.lb, pack scrollbar
# pack self.lb
self.set_bindin gs()
self.update()
def set_bindings(se lf):
# 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(ind ex)
self.lb.select_ clear()
self.lb.select_ adjust(index)
self.lb.activat e(index)
def change_font(sel f, fontname):
self.default = self.fonts.inde x(fontname)
self.highlight( )
def add_font(self, fontname, index=None):
if index is None:
self.fonts.appe nd(fontname)
else:
self.fonts.inse rt(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.familie s())
self.fonts.sort ()

for item in self.fonts:
self.fontlist.i nsert(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.inde x(kwargs['default_font'])
self.lb = Listbox(self)
# add scrollbar for self.lb, pack scrollbar
# pack self.lb
self.set_bindin gs()
self.update()
def set_bindings(se lf):
# 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(ind ex)
self.lb.select_ clear()
self.lb.select_ adjust(index)
self.lb.activat e(index)
def change_font(sel f, fontname):
self.default = self.fonts.inde x(fontname)
self.highlight( )
def add_font(self, fontname, index=None):
if index is None:
self.fonts.appe nd(fontname)
else:
self.fonts.inse rt(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_fo nts" from kwargs before initializing with Frame:

# untested
class FontList(Frame) :
def __init__(self, *args, **kwargs):
self.fonts = list(kwargs['fonts'])
self.default = self.fonts.inde x(kwargs['default_font'])
kwargs.pop('fon ts')
kwargs.pop('def ault_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.familie s())
self.fonts.sort ()

for item in self.fonts:
self.fontlist.i nsert(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.c urselection()

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

StringAtCurrSel ection = self.fontlist.g et(idx)

should be the string containing the text on the selected line

you can use self.fontlist.d elete(idx) and
self.fontlist.i nsert(idx,SomeS tring) 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.i nsert(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
5124
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 insert a new item at the same position. I think there may be better way. Zhang Le
3
6688
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 pain in the @ss to create one. You have to re-create common methods and make sure they're mapped to the right component (inside this widget are frame, entry, listbox, and scrollbar widgets). I tried to look for ways I could inherit some methods from other widgets but couldn't think of how to do...
1
2875
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, 'Collections and Maturities') mysteriously seems to get bunched up at the right, i.e. squashed up! any idea why? The main bit of the code is as such // (in progressReporter.cs) protected struct LBRow //a row of the listbox, whether it be the title or a
1
8383
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 software, and it is showing the results fine. What I want to do is to allow the user to click on the row that corresponds to the correct address, and have the code behind populate the form's Address1, Address2 etc. controls with the relevant data items. I put the code for this into the...
3
19075
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 "item 2" 2 "item 3" and so on. I keep getting the same string for selected index 0 and 1; i.e. both are "item 1". I'm new to C# and not exactly an accomplished programmer. Any suggestions?
3
2575
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 selected item from server. my program (wrong example): import ftputil import Tkinter root=Tkinter.Tk()
2
1602
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 vedran.byethost12.com 3506 Jun 25 14:40 file.gif']
2
3588
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 know that once I have the window handle of the listbox I can retrieve the item where it matches the string value I passed in to the method. Once I retrieve the correct item I want to be able to highlight that item, but the problem is that I do not know the correct syntax for this to happen. ...
0
3712
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 sender, DrawItemEventArgs e) { ListBox lst = (ListBox)sender;
0
8871
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8552
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8640
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7387
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5666
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2773
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.