需要可以插入其他小部件并删除整行的表格 [英] Need table that can insert other widgets and delete whole rows

查看:27
本文介绍了需要可以插入其他小部件并删除整行的表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Perl Tk 中设计零件.每个部分都有几个属性,每个属性都有十几个属性.一些属性,如属性名称、属性描述,只是在字段中输入的文本,其他属性是从特定值列表中选择的,等等.

Working on a part designer in Perl Tk. Each part has several properties, and each property has over a dozen attributes. Some attributes, like property name, property description, are just text entered in a field, others are chosen from a list of specific values, etc.

我对程序进行了配置,以便每个属性都是表中的一个新行.每行有几列对应每个属性的可配置属性.

I have the program configured so that each property is a new row in a table. Each row has several columns corresponding to the configurable attributes of each property.

这是我的问题.

使用 Tk::Table 我能够配置一个表格,我可以在其中插入单选按钮、复选按钮、条目小部件、选项菜单等到任何给定的单元格中,以便配置不同类型的属性可以如下完成尽可能轻松.

Using Tk::Table I was able to configure a table where I could insert radio buttons, check buttons, entry widgets, option menus, etc, into any given cell, so that configuring the different types of attributes could be done as easily as possible.

据我所知,Tk:Table 的问题在于没有删除整行的选项.我需要这个选项,如果可以避免的话,我不想为它写一个完整的算法.

The problem with Tk:Table is that there is no option to delete an entire row, as far as I can tell. I need this option and don't want to write a whole algorithm for it if I can avoid it.

然后我尝试了 Tk:TableMatrix,它确实可以像电子表格一样删除整行.但是,据我所知,我无法在 TableMatrix 单元格中插入小部件,因此只能输入每个单元格,这对我不起作用.

I then tried Tk:TableMatrix, which does have the option to delete an entire row like a spreadsheet does. However, as far as I can tell, I cannot insert widgets into the TableMatrix cells, thus each cell can only be typed in, which does not work for me.

我的问题是:

  1. 有没有办法将其他小部件类型插入到 TableMatrix 单元格中?如果是这样,我的问题就解决了.

  1. Is there a way to insert other widget types into the TableMatrix cells? If so, my problem is solved.

如果没有,有谁知道我如何在 Perl Tk 中制作一个表格,该表格既可以采用不同的小部件类型并将它们插入到单元格中,还可以选择轻松删除整行?

If not, does anyone know how I can make a table in Perl Tk that can both take different widget types and insert them into cells, and that also has the option to delete entire rows easily?

推荐答案

如果将来有人发现这篇文章并陷入同样的​​困境,这里是我如何使用 Tk::Table 并实现一种方法删除行.

In case anyone in the future finds this post and is stuck in the same predicament, here is how I went about using Tk::Table and implementing a method to delete rows.

要删除的行是用复选框选中的.算法遍历每个复选框并将所有信息保存在未选中的行中.然后该表被销毁并使用保存的信息重建.

Rows to be deleted are selected with a checkbox. An algorithm goes through each checkbox and saves all information in the rows that are not checked. The table is then destroyed and rebuilt with the saved information.

这只是一个我在实现之前测试功能的虚拟测试程序.

This is just a dummy test program I made to test functionality before implementing it.

#!usr/bin/perl -w

use strict;
use Tk;
use Tk::Table;

my $mw = MainWindow->new;

my $table;
my $teststring = "test123";


my @options = ["A","B","C","D"];
my $choice = "C";
my $bool = 0;
my @rowstokeep;
my $storedrows;


my $tb = $mw->Text(
-background => "white",
-height     => 13.5,
-width      => 48.5
)->pack;

$table = $tb->Table(
-scrollbars     => "sw",
-fixedrows      => 1,
-takefocus      => 1,
-background     => "white",
-rows           => 6,
-columns        => 3
);




$tb->windowCreate('1.0 lineend', -window=>$table);
$tb->configure(-state=>'disabled');

my $printButton = $mw->Button(
-text       => "Print All",
-command    => \&printAll
)->pack;

my $newRowButton = $mw->Button(
-text       => "New Row",
-command    => [\&newRow,$table,$teststring,\@options,$choice,$bool]
)->pack;

my $deleteRowButton = $mw->Button(
-text       => "Delete Selected Rows",
-command    => [\&deleteRow]
)->pack;

my $rowsNumButton = $mw->Button(
-text       => "How Many Rows",
-command    => [\&printNumRows,$table]
)->pack;


my $closeButton = $mw->Button(
-text       => "Close Window",
-command    => sub {exit}
)->pack;



&makeHeaders;


&newRow($table,$teststring,\@options,$choice,$bool);




MainLoop;

sub getTableDim{
  my $tempheight = $table->height;
  my $tempwidth = $table->width;

  print "Table height is $tempheight\n";
  print "Table width is $tempwidth\n";
} #end sub getTableDim

sub makeHeaders{

  $table->put(0,0,"Entry");
  $table->put(0,1,"Option Menu");
  $table->put(0,2,"Check Button");
} #end sub makeHeaders

sub newEntry{
  my ($t,$r,$c,$text) = @_;
  my $e = $t->Entry(
    -textvariable   => \$text,
    -background     => "white"
);
  $t->put($r,$c,$e);
} #end sub newEntry

sub newOptionMenu{
  my ($t,$r,$c,$arr,$disp) = @_;
  my $om = $t->Optionmenu(
    -textvariable   => \$disp,
    -options        => @$arr,
    -background     => "white"
);
  $t->put($r,$c,$om);

} #end sub newOptionMenu

sub newCheckBox{
  my ($t,$r,$c,$v) = @_;
  my $cb = $t->Checkbutton(
    -variable       => \$v,
    -background     => "white"
);
  $t->put($r,$c,$cb);
} #end sub newCheckBox

sub printAll{

my $numRows = $table->totalRows;

for (my $i=1;$i<$numRows;$i++){
  my $out1 = $table->get($i,0)->cget(-textvariable);
  my $out2 = $table->get($i,1)->cget(-textvariable);
  my $out3 = $table->get($i,2)->cget(-variable);

  print "\n\nEntry in row $i is $$out1\n";
  print "Optionmenu in row $i is $$out2\n";
  print "Checkbox in row $i is $$out3\n";
} #end for
} #end sub printAll


sub newRow{

  my ($t,$entrystr,$optionarr,$optionchoice,$checkboxval) = @_;

  my $numRows = $t->totalRows;

  &newEntry($t,$numRows,0,$entrystr);
  &newOptionMenu($t,$numRows,1,$optionarr,$optionchoice);
  &newCheckBox($t,$numRows,2,$checkboxval);

} # end sub newRow


sub printNumRows{
  my ($t) = @_;
  my $numrows = $t->totalRows;
  print "There are now $numrows total rows\n";
} #end sub printNumRows

sub deleteRow{

  @rowstokeep = ();
  @$storedrows = ();

  my $numRows = $table->totalRows;


  for (my $i = 1;$i<$numRows;$i++){
    my $bool = $table->get($i,2)->cget(-variable);
      if (!($$bool)){

    push (@rowstokeep,$i); #push row number into array if checkbox not checked
      } #end if $$bool
  } #end for


  my $size = @rowstokeep;


  if ($size > 0){
  for (my $i = 0;$i<$size;$i++){

    my $entry = $table->get($rowstokeep[$i],0)->cget(-textvariable);
    $storedrows->[$i]->{'Entry'} = $$entry;

    my $option = $table->get($rowstokeep[$i],1)->cget(-textvariable);
    $storedrows->[$i]->{'Option'} = $$option;

    my $cbstatus = $table->get($rowstokeep[$i],2)->cget(-variable);
    $storedrows->[$i]->{'Check'} = $$cbstatus;
  } #end for
  } #end if



  $table->clear;
  $table->destroy; 
  #table needs to be destroyed due to strange bug found with table->clear

  $table = $tb->Table(
  -scrollbars       => "sw",
  -fixedrows        => 1,
  -takefocus        => 1,
  -background       => "white",
  -rows         => 6,
  -columns      => 3
  );



  $tb->windowCreate('1.0 lineend', -window=>$table);
  $tb->configure(-state=>'disabled');

  $newRowButton->configure(
  -command=>[\&newRow,$table,$teststring,\@options,$choice,$bool]);
    #whenever table is destroyed and recreated, all buttons that call it are still pointing 
    #to table's old hash value and need to be reconfigured 
  $rowsNumButton->configure(
  -command=>[\&printNumRows,$table]);


  &makeHeaders;

  my $newsize = @$storedrows;

  if ($newsize>0){
    for (my $i = 0; $i<$newsize;$i++){
      &newRow($table,$storedrows->[$i]->{'Entry'},\@options,
      $storedrows->[$i]->{'Option'},$storedrows->[$i]->{'Check'});
    } #end for
  } #end if
} #end sub deleteRow

这篇关于需要可以插入其他小部件并删除整行的表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆