listbox binding..what is the current selection?

R

Rex Macey

I have a listbox with two strings "Fixed" and "Random". It is bound by the following statement:
lbLengthtype.bind('<ButtonRelease-1>',set_lengthtype)

Here's the beginning of the set_lengthtype code:

def set_lengthtype(event=None):
s=lbLengthtype.get(tk.ACTIVE)
print(s)
.....

The print(s) statement is for debugging. If 'Random' was selected and the user clicks 'Fixed', then Random will print. I would like to know what the user just selected, not what was selected before the user clicked. How can I determine the current selection? Thanks.
 
R

Rick Johnson

I have a listbox with two strings "Fixed" and "Random".
[...] Here's the beginning of the set_lengthtype code:

def set_lengthtype(event=None):
s=lbLengthtype.get(tk.ACTIVE)
print(s)
.....

The print(s) statement is for debugging. If 'Random' was
selected and the user clicks 'Fixed', then Random will
print. I would like to know what the user just selected,
not what was selected before the user clicked. How can I
determine the current selection? Thanks.

Use "listbox.nearest(event.y)" instead of "get(ACTIVE"). And maybe you should spend some time reading the docs eh?

http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html
 
R

Rex Macey

Thanks. I have spent time with the docs, at least with the Python v3.3 andtkinter v8.5 (pdf). I wish they had more examples. My approach is to browse the docs, try a program, fail, read the docs, try again. When I can't figure it out, I post. I appreciate the help.


I have a listbox with two strings "Fixed" and "Random".
[...] Here's the beginning of the set_lengthtype code:
def set_lengthtype(event=None):
s=lbLengthtype.get(tk.ACTIVE)

print(s)

The print(s) statement is for debugging. If 'Random' was
selected and the user clicks 'Fixed', then Random will
print. I would like to know what the user just selected,
not what was selected before the user clicked. How can I
determine the current selection? Thanks.



Use "listbox.nearest(event.y)" instead of "get(ACTIVE"). And maybe you should spend some time reading the docs eh?



http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html
 
R

Rick Johnson

I have spent time with the docs, at least with the Python
v3.3 and tkinter v8.5 (pdf).

Could you post links to the documents you are reading please?

Actually when i said "read the docs" i did not mean the offical Python docson Tkinter (because, IMHO, they are woefully inadequate). I actually meant"read some online tutorials".

The link i gave you is really more of a reference than a tutorial. It's my go-to reference when i can't remember the behavior of "this" or "that" method, or the spelling of "this" or "that" event sequence. Here is another link that is much more of a tutorial.

http://effbot.org/tkinterbook/tkinter-whats-tkinter.htm
I wish they had more examples.

Yes me too! Examples speak much louder than mere words. Of course, ONLY if the examples are good examples. Maybe we could combine my ability to write good examples with your ability to judge the noob worthiness of such examples and contribute something positive to this community of ingrates. (pleasedon't mis-interpret the theatrical verbiage, i am, in fact, quite serious)
My approach is to browse the docs, try a program, fail,
read the docs, try again. When I can't figure it out, I
post.

That's a wise and considerate path. Considerate both for yourself and others.

============================================================
The path to enlightenment
============================================================
The temptation to run and ask somebody for an answer is *VERY*, well, tempting. The problem is, the more you ask and receive *easy* answers, the moreyou become dependent on your teacher and not yourself.

Luckily for me (although i was unaware of the benefit at the time) i was oblivious to online help forms when i learned programming; forcing me to be self-reliant when learning. It was just me, some badly written docs, and a text editor. :)

After lots of trial and error and many a smashed keyboard, i emerged from under my rock with a deep knowledge that i don't believe i could have attained by having someone spoon feed me the answers.

It seems that when you *DO* find yourself in a wet paper bag that you can'tseem to mentally punch yourself out of, and you are forced to get outside input, the last thing you need is people "gift wrapping" answers for you.

A wise teacher will give you a few clues and then aim you in the correct direction, leaving you to find the exact path to enlightenment. Learning "howto problem solve" is much more important skill than "learning how to solvea problem"; the old "teach a man to fish..." thing.

============================================================
Request for your personal experiences:
============================================================

Since you are new to Tkinter i wonder if you can give some insights into the specific troubles you are encountering. I have long since forgotten most of the troubles i experienced. This would be a good chance to document the issues with the documents.

============================================================
Learning GUI's cuts you twice!
============================================================

Learning GUI's for the first time is very difficult because before you can learn the API of the current GUI library, you must first learn what a "EditText" is, and what a "StaticText" is, and a "ProgressBar", and a "NoteBook", and abstract things like "InputEvents", and how to bind "inputEvents", and "GeometryManagment"; and blah, blah, blah.

This first step is where most GUI tutorials fail. They try to present both the widgets AND the code to control them simultaneously. It's no wonder thestudent becomes bewildered!

I believe the first step should involve NO code or even code examples. The first step should be mainly a visual experience combined with interactive delving into the intricacies of what "actions" each widget will expose; using only natural language and visuals to describe the actions.

But even when GUI tuts follow this wise progression they fail to utilize the power of visualization, and instead focus on pages and pages of boring text.

The second step is learning the actual methods of the widgets and how to link code to actions.

============================================================
Morals of My Experience:
============================================================

Looking back i believe the Tkinter learning path is flawed in to general ways.

1. The official docs are woefully inadequate.

Which could be solved by writing better documentation, or simply, by linking to a few of the existing good outside documentation. The two links i posted offer the best of both worlds (reference and tutorial[1]) But then, there are all the issues of poor examples around the web. I found myself being thrown into fits of confusion by reading documentations or tutorials that seemed to conflict with one another.

"""Misleading documentation can be more devastating than missing documentation."""

2. The Tkinter API is asinine, inconsistent, and full of multiplicities.

I know that sounds unfair but it *IS* true. Yes, Tkinter is one of the simplest Python GUI's to learn, and it has the benefit of being in the Python stdlib, however, it has many issues[2] that should have been resolved long ago, and could be resolved today if i could convince the "keepers of the source" to engage in healthy discussion of these specifics.

============================================================
REFERENCES:
============================================================
[1] Although the tutorial site has a few confusing examples and fails to cover the new ttk widgets.

[2] Plus, it is missing simple functionalities like "auto-scrollbars". (although this is a direct result of being a slave to the Tk:TCL folks.
 
W

William Ray Wing

Thanks. I have spent time with the docs, at least with the Python v3.3 and tkinter v8.5 (pdf). I wish they had more examples. My approach is to browse the docs, try a program, fail, read the docs, try again. When I can't figure it out, I post. I appreciate the help.

With apologies for jumping into the middle of this discussion, but I've found the most helpful tkinter v8.5 docs to be the set at New Mexico tech:

http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/index.html

Seem quite complete and in particular, starting at section 28, they are quite detailed in their explanation of the ttk themed widgets.

-Bill
 
R

Rick Johnson

With apologies for jumping into the middle of this
discussion, but I've found the most helpful tkinter v8.5
docs to be the set at New Mexico tech:

Well these are free and open forums the last time i checked. Don't worry, nobody is going to get upset because you offered your opinions. I mean, the worst think that could happen is if someone offers up the exact same information that had already been offered early in the thread because they were too lazy to read the thread in full, and now they look like a complete boob ( a helpful boob, but a boob none-the-less). But that's just a "crazy" hypothetical situation. You would never do that. So no need to worry.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top