Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8057021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:53:47+00:00 2026-06-05T08:53:47+00:00

I want to create labels that the text in them can be selected for

  • 0

I want to create labels that the text in them can be selected for copy/paste. To do this I tried to use entries that are read-only. But I can’t seem to initialize the text value in them. The labels are generated inside a loop and the number of labels and their content is unknown. The code to produce the labels is:

proc test_labels {} {
    toplevel .labels
    # Main Frame
    frame       .labels.main_frame -relief "groove" -bd 2
    pack        .labels.main_frame

    set r 1

    foreach t [list banana apple grapes orange lemon peach] {
        set lbl  [label .labels.main_frame.lbl_$r   -text "fruit $r:"]
        set lbl2 [label .labels.main_frame.val_$r -text $t]

        grid $lbl  -row $r -column 1 -sticky nse
        grid $lbl2 -row $r -column 2 -sticky nsw

        incr r
    }
    set ok_btn [button .labels.main_frame.ok_b -text "OK" -command {prop_menu_ok_button}]
    grid $ok_btn -row [expr $r+2] -column 1 -columnspan 2 -sticky nsew

    grab release .
    grab set .labels
    center_the_toplevel .labels
    bind .labels <Key-Return> {test_labels_ok_button}
}

And it creates the fallowing window:

enter image description here

Then I try to replace the line set lbl2 [label .labels.main_frame.val_$r -text $t] with the lines:

eval "set text_val_$r $t"
eval "set lbl2 [entry .labels.main_frame.val_$r -relief flat -state readonly -textvar text_val_$r]"

But this only creates empty lines:

enter image description here

How can I put default values to entry widgets?

Related to the question How to make the text in a Tk label selectable?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T08:53:48+00:00Added an answer on June 5, 2026 at 8:53 am

    These lines are almost certainly not what you want! (If you’re using eval, you should always ask whether it’s really necessary; from 8.5 onwards, the likely answer is “it’s not necessary”.)

    eval "set text_val_$r $t"
    eval "set lbl2 [entry   .labels.main_frame.val_$r -relief flat -state readonly -textvar \$\{text_var_$r\}]"
    

    The key problem — apart from the use of eval — is that the -textvariable option takes the name of a variable. Let’s fix that by using an array to hold the values:

    set text_val($r) $t
    set lbl2 [entry .labels.main_frame.val_$r -relief flat -state readonly \
            -textvariable text_val($r)]
    

    Also, be aware that the text_val array needs to be global (or in a namespace, if you fully qualify the name when giving it to the -textvariable option). This is because it is accessed from places which are outside the scope of any procedure.

    Of course, it turns out that if we are keeping values constant then we can avoid using a variable at all and just insert the value manually.

    set lbl2 [entry .labels.main_frame.val_$r -relief flat]
    $lbl2 insert 0 $t
    $lbl2 configure -state readonly
    

    If you’re never changing the value, that will work fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create labels in MFC (Static text) that have both bold and
I want to create a pairs plot in R that has labels on the
I know that after I create a ggplot graph I can use theme_get() to
I want to know how to create a label that contains two icons, one
I want to create a fading effect of text in Label control. I change
I want to create an InputDialog with custom labels for the OK/Cancel buttons. I'm
I want to know if this is possible in c# winform. create control when
I want to create labels based on an array, but i always get only
I want to create a scroll view with a massive contentSize that will display
I have a panel sitting in a div, and I want to use that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.