I'm writing a MIDI application in Tk, and I'd like to use a BrowseEntry widget to offer the user a selection of their existing ALSA MIDI ports, which are being scanned with MIDI::ALSA and put into an array long before the BrowseEntry widget ever gets configured or displayed. (In other words, by the time we get to BrowseEntry(), our array exists and is populated with port names.) I'm seeing some really strange behavior here.

Sometimes, when you run the program, the dropdown list has the first item pre-selected in the entry widget, but the variable is still empty until you actually pick something from the dropdown. This is misleading, because it makes the user believe they have a default value available with no need for them to do anything, when actually it's set to nothing. Other times when you run the program, the entry widget is clear (the true state of the variable, which is actually empty), and you need to pick something from the list to make something appear in the widget and give the variable a value.

What's even weirder is that, as far as I can tell, these two behaviors alternate on alternating runs of the program. What I mean is, you run the program, the entry starts off clear, then you run it again, and it's pre-populated with the first array item. Then you run it again, it's clear. Then you run it again, and it's pre-populated with the first array item. This program doesn't have a config file yet, and doesn't save anything to disk, so I don't know how it would even know whether the last run pre-populated the entry widget or not, as there shouldn't be a way for subsequent invocations of the program to even know that. But wouldn't there have to be some state being saved somewhere outside the program for it to be able to predictably alternate behaviors on successive runs like that?

Anyway, what I would like is for the entry widget contents to match the actual value of the variable at all times, and I don't care whether that's in the form of the entry being clear, or being pre-populated with the first item in the array. Either is fine, but the user should not be given the impression that they have a default setting selected when actually the variable contains nothing. A solution that makes the entry widget clear would be fine, or one that pre-populates it with a value is fine, as long as that's what's really in the variable.

Here's my code. Thanks if anyone can help with this:

my $dropdwnMidiPort = $midiCtrls->BrowseEntry( -choices => \@midi_outdevs, -state => 'readonly', -justify => 'right', -variable => \$midi_outdev, -browsecmd => sub { $midi_indev = $midi_outdev; MidiConSetup('out'); MidiConSetup('in'); }, -font => 'Helvetica -12 bold', -disabledforeground => 'black')->grid( -row => 0, -column => 0); $dropdwnMidiPort->Subwidget('slistbox')->configure( -selectbackground => 'IndianRed4', -selectforeground => 'white');

In reply to Confusing behavior from Tk::BrowseEntry by DcmbrAgnt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.