如何在Tcl / Tk中自动在标签中换行?

迭戈·亨里克

有没有人做过或不知道如何进行自动[ label]换行?还是自我智能地认识到到达每行末尾(即,在边距的边缘)的信息仅限于小部件本身?

码:

package require Img

# Container
frame .fr -borderwidth 2 -bg white 

# Text Variable
set description "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris"

# Thumbnail
image create photo pic -file /home/tc/webvideo/galinha_pitadinha/seu_lobato.jpg
button .fr.bt -relief flat -bg white -image pic

# Description
label .fr.lb -bg white -textvariable description -wraplength 250 -width 30 -justify left

pack .fr .fr.bt .fr.lb -side left -fill x -padx .5c -pady .5c

代码结果:

在此处输入图片说明

从程序上来说,我希望它看起来像这样:

在此处输入图片说明

我想打破句子,以及上面的说明性图片。

请注意,缺少正则表达式来寻找第二行的末尾在末尾嫁给小罐子(椭圆)。当然,请隐藏其余的文本。

将鼠标悬停在label小部件上时,将显示所有文本tooltip

我一直在想两个假设。他们是:

  • 1)显示Text变量中“字符”的总数

    tk_messageBox -message [string length $description]

  • 2)显示Text变量中“单词”的总数

    tk_messageBox -message [llength $description]

我在这里停了下来:

在此-wraplength 250属性设置中,每两行10个单词的变化基于此,我可以应用一个条件,并将单词字符的数量作为确定因素,使其仅显示到第二行。

# If it were to quantify Characters and not words, this would be
if {[string length $description] < 40} {
pack [label .b -textvariable description -wraplength 250 -width 30 -justify left] -side top -fill x
}

或者比较一定数量的单词,在这种情况下为10个单词。如果为true,请执行正则表达式的操作

 # If it were to quantify words and not characters, this would be
 if {[llength $description] <10} {
  ... RegExp code here ...
 }
迭戈·亨里克

我在黎明时所做的这种编码对我来说似乎是一种可能的解决方案。但是,我尚未测试与Tk 相关的编码如果成功或失败,我将进行测试以及所有新闻。我将更新此答案。

我在这里要做的基本上是在下面的代码中创建一个文件作为指针,从这里我将开始查找要显示在标签上的两行。请遵循源代码中包含的注释:

#!/usr/bin/env tclsh

# Opening a nonexistent file (empty)
set file [open "input.txt" w]

# Writing on it ..
puts $file "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris"

# Close. This saves a few bytes.
close $file

# Opening it again, now let's read.
set file [open "input.txt" r]

# Read me.
set read [read $file]

# Now I need to replace multiple spaces with '+' as a reference for
# we quantify and subsequently break the phrase into the variable at this point (+)
set add [string map -nocase {{ } {+}} $read]

# Just an index, indicator that starts counting the for loop
set count 0
for {set value 0} {$value<10} {incr value} {

# In this variable I will extract line-by-line
set number [lindex $add $count]

# Now I create a list
set data [list]

# It's time to break the text by the '+' signs, saving the result (a list of words) in the data variable
lappend data [split $number +]

# Just an index, indicator that starts counting the while loop
set indice 0

# Loop running until 10 words are exhausted
while {$indice<10} {

# Is this variable , wich show result
# shows us the words the loop went through until the set value ended: 10
   set result [lindex $data 0 $indice]

    # See them now on screen, detail!
        # The variable phrase, which was once a single line, was broken and passed to the list.
        # Thus, every word followed by its comma (,) and / or period (.)
        # are in a single column, you will need to format them again to be displayed in "a single row"
        # We enter the argument "-nonewline" getting all words extracted by on one line
        # The space purposely placed in front of the variable "$ result"
        # Is required to have space between words again       
          puts -nonewline "$result "
   incr indice
}
        incr count
      puts ".."
   break
}

close $file 

结果输出:

打印屏幕

在此处输入图片说明

这里值得一提的是,我包装在我的机器(微型计算机)中的是,Tcl 8.4和8.5的软件包还没有8.6。这是我在Tcl / Tk中编程工作的环境。

但是我编写了代码,并在运行Tcl 8.6的此在线平台上运行了测试


My concern and work now was to adapt the code above with the Tk library.

I had the task of capturing the output and retrieving it. All at runtime.

I tried several times to pass the output of the put command into another variable that could retrieve the new content.

But unfortunately, i dot not how haver sucess. Because i m new from language programing Tcl.

So, I made this capture using Shell Script Unix with Script Tcl. See the part(s):

  • Here recover output of command put:
    set txt [exec cat ./output.txt]

  • Here put output: -textvariable txt
    label .fr.lb -bg white -textvariable txt -wraplength 250 -width 30 -justify left

Now puts code in wrapper this procedure all terminates a call do the Tcl script process and transfers it to the Bourn Shell script which, in turn, returns a window with the already formatted of label text. Like this:

proc test {} { 
     .. The code .. 
  put "output some text"
}

# invoke in file end 
test

Without further explanation, just analyze and draw conclusions.

Now see the structure

Let's redirect the output of the puts command to an external file output.txt.

We have to create 3 files, two of them are Tcl and another Bourne Shell. Look:

test.tcl: Script Tcl main

#!/usr/bin/env sh
# \
exec wish "$0" "$@"

package require Img

set txt [exec cat ./output.txt]

# Container
frame .fr -borderwidth 2 -bg white 

# Thumbnail
image create photo pic -file thumb.jpg
button .fr.bt -relief flat -bg white -image pic

# Description
label .fr.lb -bg white -textvariable txt -wraplength 250 -width 30 -justify left

# The help texts, as we said, stay in an array:

set subtitle(.fr.lb) [exec cat ./input.txt]

# The frame that will contain the help text itself:

frame .help -bd 1 -bg black
label .help.lab -wraplength 200 -width 30 -text "Texto da subtitle" -bg lightyellow -justify left
pack .help.lab

# Note that we do not materialize the frame as we do not want it to appear now.

# Now comes the most important part. When the mouse cursor enters a button, we should start the process of "Show in the future" the help frame.

bind .fr.lb <Enter> {+
    set subtitle(win) %W
    .help.lab config -text $subtitle(%W)
    set subtitle(id) [after 500 place .help \
        -x [expr %x + [winfo x %W]] \
        -y [expr %y + [winfo y %W] + 20] ]
}

# 

bind .fr.lb <Leave> {+
    if [info exists subtitle(id)] {
        after cancel $subtitle(id)
        unset subtitle(id)
    }
    place forget .help
}

pack .fr .fr.bt .fr.lb -side left -fill x -padx .5c -pady .5c

test.sh: Invoke script test.tcl e transfer the output of command put

#!/usr/bin/env sh
# \
exec tclsh "$0" "$@"

set file [open "input.txt" w]

puts $file "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris"

close $file

proc test {} {

set file [open "input.txt" r]

set read [read $file]

set add [string map -nocase {{ } {+}} $read]

set value 0

for {set value 0} {$value<10} {incr value} {

set number [lindex $add $value]

set data [list]

lappend data [split $number +] 

set indice 0

while {$indice<10} {
    set result [lindex $data 0 $indice]
    puts -nonewline "$result "
    incr indice
    }    
  incr value
  puts ".."
  break
  }
 close $file
}

# Invoke
test

main.tcl: Insert the out of command put in file output.txt

#!/usr/bin/env sh
# \
exec tclsh "$0" "$@"

exec ./test.sh > ./output.txt 
exec ./test.tcl

I didn't want code to be in separate files, but this was well organized.

当将代码与Tk库一起使用时,改编是并行开发的..保持不变:在同一平面上,不要互相割裂。

我将保留脚本和图片,并从我的Google云端硬盘帐户中下载-Acess获取

响应代码太长了,因为它包括一起的tooltip代码。

打印屏幕:

在此处输入图片说明

如果我现在或以后升级代码,我会发布更改。感谢所有人,以贡献

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章