忘记密码   免费注册 查看新帖 |

ChinaUnix.net

  平台 论坛 博客 文库 频道自动化运维 虚拟化 储存备份 C/C++ PHP MySQL 嵌入式 Linux系统
最近访问板块 发新帖
查看: 1760 | 回复: 1

Tk::Table控件的的数据如何使其可以编辑? [复制链接]

论坛徽章:
0
发表于 2018-02-28 15:19 |显示全部楼层
  1. #!/usr/bin/perl -w

  2. use Tk;
  3. use Tk::Canvas;
  4. use Tk::Table;
  5. use strict;

  6. my $mw = MainWindow->new;
  7. $mw->geometry("475x125");
  8. $mw->resizable(0,0);
  9. $mw->title("Table Example");

  10. my $canvas = $mw->Canvas()->pack();
  11. my $table = $canvas->Table(-columns => 8,
  12.                                 -rows => 4,
  13.                                 #-fixedrows => 1,
  14.                                 -scrollbars => 'oe',
  15.                                 -relief => 'raised');

  16. foreach my $col (1 .. 8)
  17. {
  18.   my $tmp_label = $table->Label(-text => "COL " . $col, -width => 8, -relief =>'raised');
  19.   $table->put(0, $col, $tmp_label);
  20. }

  21. foreach my $row (1 .. 8)
  22. {
  23.   foreach my $col (1 .. 8)
  24.   {
  25.     my $txt = $row . "," . $col;
  26.     my $tmp_label = $table->Label(-text => $txt,
  27.                                   #-padx => 2,
  28.                                   #-anchor => 'w',
  29.                                   #-background => 'white',
  30.                                   -relief => "groove");
  31.     $table->put($row, $col, $tmp_label);
  32.   }
  33. }
  34. $table->pack();

  35. my $button_frame = $mw->Frame( -borderwidth => 4 )->pack();
  36. $button_frame->Button(-text => "Exit", -command => sub {exit})->pack();

  37. MainLoop;
  38. exit;
复制代码


这是网上的代码,显示的数据只可以浏览,不可以编辑。

论坛徽章:
0
发表于 2018-03-03 21:39 |显示全部楼层
把$table->Label改为$table->Entry 即可以编辑并保存数据:

  1. #!/usr/bin/perl -w

  2. use Tk;
  3. use Tk::Table;
  4. use strict;

  5. my $mw = MainWindow->new;
  6. $mw->geometry("475x125");
  7. $mw->resizable(0,0);
  8. $mw->title("Table Example");

  9. my $table_frame = $mw->Frame()->pack();
  10. my $table = $table_frame->Table(-columns => 8,
  11.                                 -rows => 4,
  12.                                 -fixedrows => 1,
  13.                                 -scrollbars => 'oe',
  14.                                 -relief => 'raised');
  15.                                 
  16. my @onerow =("c1","c2","c3","c4","c5","c6","c7","c8",);                                
  17.                                 
  18. my @value = ([@onerow],[@onerow],[@onerow],[@onerow],[@onerow],[@onerow],[@onerow],[@onerow],);

  19. $value[3][4] = "red";
  20. $value[7][7] = "blue";

  21. foreach my $col (0 .. 7)
  22. {
  23.   my $tmp_label = $table->Label(-text => "COL ".$col, -width => 8, -relief =>'raised');
  24.   $table->put(0, $col, $tmp_label);
  25. }

  26. foreach my $row (1 .. 7)
  27. {
  28.   foreach my $col (0 .. 7)
  29.   {
  30.     my $tmp_Entry = $table->Entry(-textvariable => \$value[$row][$col],
  31.                                   #-padx => 2,
  32.                                   #-anchor => 'w',
  33.                                   -width => 8,
  34.                                   -background => 'white',
  35.                                   -relief => "groove");
  36.     $table->put($row, $col, $tmp_Entry);
  37.   }
  38. }
  39. $table->pack();

  40. print "$value[3][4]\n";

  41. my $button_frame = $mw->Frame( -borderwidth => 4 )->pack();
  42. $button_frame->Button(-text => "Exit", -command => sub {exit})->pack();
  43. $button_frame->Button(-text => "Print[3][4]",
  44.                        -command => sub {
  45.                                         print "$value[3][4]\n";#手动把red改为 blue
  46.                                         #点击按钮可见值已经修改
  47.                                         #$table->see(3,4);#该方法cpan 网站未做说明
  48.                                        }
  49.                           )->pack();

  50. MainLoop;
  51. exit;

  52. #以下是cpan上的说明文字
  53. #
  54. # $table = $parent->Table(-rows => number,
  55. #                          -columns => number,
  56. #                          -scrollbars => anchor,
  57. #                          -fixedrows => number,
  58. #                          -fixedcolumns => number,
  59. #                          -takefocus => boolean);
  60. #
  61. #  $widget = $table->Button(...);
  62. #
  63. #  $old = $table->put($row,$col,$widget);
  64. #  $old = $table->put($row,$col,"Text");  # simple Label
  65. #  $widget = $table->get($row,$col);
  66. #
  67. #  $cols = $table->totalColumns;
  68. #  $rows = $table->totalRows;
  69. #
  70. #  $table->see($widget);
  71. #  $table->see($row,$col);
  72. #
  73. #  ($row,$col) = $table->Posn($widget);

  74. #Tk::Table is an all-perl widget/geometry manager which allows a two dimensional table of arbitary perl/Tk widgets to be displayed.
  75. #
  76. #Entries in the Table are simply ordinary perl/Tk widgets. They should be created with the Table as their parent. Widgets are positioned in the table using:
  77. # $table->put($row,$col,$widget)
  78. #
  79. #If $widget is not a reference it is treated as a string, and a Lable widget is created with the string as its text.
  80. #
  81. #All the widgets in each column are set to the same width - the requested width of the widest widget in the column. Likewise, all the widgets in each row are set to the same height - the requested height of the tallest widget in the column.
  82. #
  83. #A number of rows and/or columns can be marked as 'fixed' - and so can serve as 'headings' for the remainder the rows which are scrollable.
  84. #
  85. #The requested size of the table as a whole is such that the number of rows specified by -rows (default 10), and number of columns specified by -columns (default 10) can be displayed.
  86. #
  87. #If the Table is told it can take the keyboard focus then cursor and scroll keys scroll the displayed widgets.
  88. #
  89. #-scrollbars. By default, scrollbars will be added nw. To disable scrollbars, set -scrollbars to an empty string:
  90. #  $table = $parent->Table(-scrollbars => '', ...);
  91. #
  92. #The table can be emptied using
  93. # $table->clear
  94. #
  95. #the widgets which were in the table are destroyed.
  96. #
  97. #The Tk::Table widget is derived from a Tk::Frame, so inherits all its configure options.
  98. #
  99. #The default focus traversal is giving the focus only to the table widget as a whole. To enable focus traversal into table cells (e.g. if there are embedded entry widgets), then the option -takefocus has to be set to 0.
  100. #
  101. #BUGS / Snags / Possible enhancements ^
  102. #?Very large Tables consume a lot of X windows.
  103. #?No equivalent of pack's -anchor/-pad etc. options
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号 北京市公安局海淀分局网监中心备案编号:11010802020122
广播电视节目制作经营许可证(京) 字第1234号 中国互联网协会会员  联系我们:wangnan@it168.com
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP