How to set a fixed window size using Perl Tkx - perl

I am trying to create a GUI window using perl.
But the created GUI window size differs in different system.
Could anyone please tell me, how to set the fixed size, so that, it won't change depends on the following factors,
- Any resolution we set in our desktop
- Any system we use
- Any OS we use
Code Snippet i used in Perl:
use Tkx;
my $MainWindow = Tkx::widget -> new(".");
$MainWindow -> g_wm_geometry("600x15+340+612");
$MainFrame = $MainWindow -> new_ttk__frame();
$ExtractionTypeLabel = $MainFrame -> new_ttk__label(-text => <<TEXT>>,
-padding => 10,
);
$ExtractionTypeSingle = $MainFrame -> new_ttk__radiobutton(-text =>$ConfMainSingleExtractionText,
-variable => \$ExtractionTypeVal,
-value => $ConfMainSingleExtractionText,
-command => \&SINGLE_RADIO_BUTTON_LISTENER,
);
##### ALIGNMENT IN GRID LAYOUT #####
$ExtractionTypeLabel -> g_grid(-row => 3, -column => 0, -pady => 1);
$ExtractionTypeSingle -> g_grid(-row => 4, -column => 0, -ipadx => 4);
##### PACK #####
$MainFrame -> g_pack(-in => $MainWindow);
Tkx::MainLoop();
exit;

Try this to set a particular window size.
$MainWindow ->configure( -width=> 100, -height=> 100 );

Related

Update an entry in Perl Tk

I just start Perl Tk and I had a look on some tutorials but I have a problem. When I click on a button it displays on the entry widget the scalar that I want. It works but when I click an other time it keeps what was written on the entry. So I have two hello. I know that it comes from insert(0, "Hello") but I don't what to put instead of 0.
#!/usr/local/bin/perl
use Tk;
my $mw = MainWindow->new;
$mw->geometry("500x350+0+0");
$mw->title("Report Information about a Protein of Interest");
my ($bite) = $mw -> Label(-text=>"Enter the uniprot accession number:")->grid(-row => 0, - column => 0);
my ($ent) = $mw->Entry()->grid(-row => 0, - column => 1, -columnspan => 2, -sticky => 'nsew');
$ent2 = $mw->Button(-text=> "Search", -command => \&push_button)->grid(-row => 1, - column => 0);
MainLoop;
#This is executed when the button is pressed
sub push_button {
$ent -> insert(0,"Hello, ");
}
The insert method for a Tk::Entry widget inserts text after the current insertion cursor position; to delete the existing text in the widget before inserting you can do:
sub push_button {
$ent -> delete(0, 'end'); # clears the widget
$ent -> insert(0,"Hello, ");
}

Perl Tkx entry validation not working

I am having an issue getting my text validation to work properly. Basically I have 2 entry boxes, one to enter the name of a branch in CVS and the other to enter a directory name. I want my program to validate the text in each box whenever it is changed.
To do this, the documentation online says to use the "key" option to validate the entry whenever a keystroke changes the entry box's contents. The problem is, when I use the "key" option and then run the program, when I type into the entry boxes no text appears.
My code is shown below:
use strict;
use warnings;
use Tkx;
# Initialize BRANCH and DIRECTORY
my ($BRANCH, $DIRECTORY) = ();
# DEFINE DISPLAY OBJECTS ###########################################################################
# Define main window
my $main_window = Tkx::widget->new('.');
# Define content frame
my $content_frame = $main_window->new_ttk__frame(-padding => '5 5 5 5');
# Define labels
my $branch_label = $content_frame->new_ttk__label(-text => 'Branch' );
my $directory_label = $content_frame->new_ttk__label(-text => 'Directory');
# Define entry boxes
my $branch_entry = $content_frame->new_ttk__entry(-width => 20, -textvariable => \$BRANCH , -validate => 'key', -validatecommand => \&check_state);
my $directory_entry = $content_frame->new_ttk__entry(-width => 20, -textvariable => \$DIRECTORY, -validate => 'key', -validatecommand => \&check_state);
# Define buttons
my $generate_list_button = $content_frame->new_ttk__button(-text => 'Generate', -state => 'disabled', -command => \&generate_list);
# POSITION DISPLAY OBJECTS #########################################################################
# Position content frame
$content_frame->g_grid(-column => 0, -row => 0);
# Position labels
$branch_label ->g_grid(-column => 0, -row => 0);
$directory_label->g_grid(-column => 0, -row => 1);
# Position entry boxes
$branch_entry ->g_grid(-column => 1, -row => 0);
$directory_entry->g_grid(-column => 1, -row => 1);
# Position buttons
$generate_list_button->g_grid(-column => 0, -row => 2, -columnspan => 2);
# Add padding
for my $child (Tkx::SplitList($content_frame->g_winfo_children)) {
Tkx::grid_configure($child, -padx => 5, -pady => 5);
}
# Check the state of the program
sub check_state {
# Check conditions are met to enable generate_list
if ($BRANCH && $DIRECTORY) {
if (-d $DIRECTORY) {
$generate_list_button->state('!disabled');
} else {
$generate_list_button->state('disabled');
}
} else {
$generate_list_button->state('disabled');
}
return 0;
}
Tkx::MainLoop();
If I change "key" to something else (like focusout) it appears to work correctly. But I would really like it to validate after every keystroke instead of just when the focus is taken out of the entry box. Why doesn't this work?
Your check_state subroutine always returns 0 which means "validation failed" and prevents
the text from being entered. Since you aren't really validating the text -- just using the validation mechanism to trigger state updates to related widgets -- you should return 1 (unconditionally) instead. See validatecommand in the (Tcl) Tk documentation for more details.
The subroutines returning 0 turned out to be only half the problem. Once I fixed that the entry validation was not acting properly. What was happening was every time it tried to validate the entry it would actually be validating the previous entry.
ex:
If you typed in "/somedirectory" it would try to validate on every keystroke, after the last keystroke of "y" it would get the value of $DIRECTORY and validate against it. The problem was that at this point $DIRECTORY would be equal to "/somedirector"
To solve this issue I had to do a little digging so I wanted to post my findings in case someone else ran into this same issue.
The solution was to use Tkx::Ev() to get the "current" value of the entry as it was being entered.
(Tkx::Ev(%P) gets the newly entered value so the validation will work properly)
# Define the entry box
my $directory_entry = $content_frame->new_ttk__entry(
-width => 20,
-textvariable => \$DIRECTORY,
-validate => 'key',
-validatecommand => [\&check_dir, Tkx::Ev('%P')],
);
# Validate the entry box
sub check_dir {
# Unpack input arguments
my ($P) = #_;
if (-d $P) {
# Do something here
} else {
# Do something else here
}
return 1;
}

Perl/Curses event handling and I/O

So, I just started trying to use the perl curses module for a project I'm working on. The documentation seems to be extremely lacking, what little I can find on cpan seems to be half-finished and assumes previous curses library experience, which I don't have. I have two issues I am trying to solve, my code so far:
#!/usr/bin/perl
use strict;
use Curses::UI;
use Term::ReadKey;
my ($cols, $rows, $wp, $hp) = GetTerminalSize();
my $cui = new Curses::UI( -color_support => 1);
sub eDialog {
my $return = $cui->dialog(
-message => "Are you sure?",
-title => "Really quit?",
-buttons => ['yes', 'no']
);
exit(0) if $return;
}
sub entryUpdate {
my $mainentry = shift;
if($mainEntry->get() =~ m/.*\n$/)
{
print STDERR $mainEntry->get();
}
}
$cui->set_binding( \&eDialog , "\cQ");
my $mainWin = $cui->add(
'viewWin', 'Window',
-border => 1,
-height => ($rows - 3),
-bfg => 'green'
);
my $mainView = $mainWin->add(
"viewWid", "TextViewer",
-wrapping => 1
);
my $entryWin = $cui->add(
'entryWin', 'Window',
-border => 1,
-y => ($rows - 3),
-height => 1,
-bfg => 1
);
my $mainEntry = $entryWin->add(
"entryWid", "TextEntry",
-onchange => \&entryUpdate()
);
$mainEntry->focus();
$cui->mainloop();
I managed to get the UI set up how I want it, but actually making it work is proving problematic.
1). I want to be able to, when text is typed into the $mainEntry widget, detect when enter/return is pressed, and execute a subroutine to do stuff with the text typed into the widget, then clear it out. (I tried accomplishing this with the entryUpdate subroutine, but that isn't working at all, no matter how I've tried to do it.)
2). I want to be able to periodically (Say, every 1 second or 500ms), execute another subroutine, and have the string it returns added to the $mainView widget.
Getting either or both of these to work has proven to be a huge issue thus far, I just dont know enough about how curses works and I haven't been able to find the information I need anywhere. Any help would be much appreciated.
1) You can simply bind the return key to a subrouting using set_binding:
use Curses qw(KEY_ENTER);
$mainEntry->set_binding(sub {
$mainView->text($mainView->text . $mainEntry->get . "\n");
$mainView->draw;
$mainEntry->text("");
}, KEY_ENTER);
2) It seems that there are timer methods (found them by grepping the Curses-UI source code), but they are not documented, which is probably an issue. Here's how it's used:
$cui->set_timer('timer_name', sub {
$mainView->text($mainView->text . scalar(localtime)."\n");
$mainView->draw;
}, 1);

Settings in pdf::table perl

I'm trying to create a table in PDF format using PDF::Table in perl.
However, it seem to not read my header/cols/rows settings.
Here's my code for the table:
use PDF::API2;
use PDF::Table;
my $pdftable = new PDF::Table;
my $pdf = PDF::API2->new();
my $page = $pdf->page;
#data
my $some_data =[
["1","1","1","1","1","1","1"],
["2","2","2","2","2","2","2"],
["2","2","2","2","2","2","2"],
["2","2","2","2","2","2","2"],
["2","2","2","2","2","2","2"],# x 100 time to have pages
];
#build the table layout
$pdftable->table(
$pdf,
$page,
$some_data,
x => 5,
w => 600,
start_y => 750,
next_y => 750,
start_h => 700,
next_h => 700,
# some optional params
font_size => 8,
border => 0,
background_color_odd => "white",
background_color_even => "lightblue",
header_props => $hdr_props, # see section HEADER ROW PROPERTIES
);
$hdr_props =
{
# This param could be a pdf core font or user specified TTF.
# See PDF::API2 FONT METHODS for more information
font => $pdf->corefont("Times", -encoding => "utf8"),
font_size => 10,
font_color => '#006666',
bg_color => 'yellow',
repeat => 1, # 1/0 eq On/Off if the header row should be repeated to every new page
};
print "Content-Type: application/pdf;\n\n";
binmode(STDOUT);
print $pdf->stringify;
It should make the first row as header by default but the output shows no header properties being set on the first row. And there is no header shown for all pages.
Any help will be appreciated.
I did not run your code.
You are referencing $hdr_props before the variable is filled. Perl does not work that way, you need to order definitions properly.
Add use strict; use warnings FATAL => 'all'; to the top of your programs and Perl will alert you about mistakes like this.
theres no warnings at all, and i followed daxim to put the $hdr_props to top first but it still would not read in the header settings.
The documentation says that header_props needs to be a hash reference, so:
header_props => \$hdr_props, # see section HEADER ROW PROPERTIES
I ran into a similar problem. However, daxim is right, you should also order your code as he suggested.

perl win32::gui remove elements

I have a dialogbox, to which i populate elements(labels) on activate event.
I want to remove these elements(labels) when the window is being deactivated. something like:[its erroneous fragment of main code but explanatory]
my $wchRW = Win32::GUI::DialogBox->new(
-name => "wchR",
-title => "whed",
-left => CW_USEDEFAULT,
-size => [300, 130],
-parent => $mw,
);
$wchRW->AddGroupbox(
-name => "wchR_gb",
-text => "being watched",
-width => $wchRW->ScaleWidth() - $padding,
-height => 100,
-left => $padding/2,
);
sub wchR_Activate {
my $wchtxt = "sample";
# lbleft, lbtop are calculated here
$wchRW->AddLabel(
-name => "wchR_lb0",
-text => $wchTxt,
-left => $lbLeft,
-top => $lbTop,
);
}
sub gitni_wchR_Deactivate {
print "Here\n";
Win32::GUI::DestroyWindow($wchRW->wchR_lb0); #this is line n
}
#i have a button in main window $mw.
#onclick of this button this dialogbox is shown.
#sub b1_Click { $wchRW->DoModal(); return 0; }
But the problem is, "Here" in deactivate is called many times and an exception is thrown thereafter [can't locate auto/wchR_.al in #INC .... line n]. Freezing the main window and only option is to end process through task-manager.
Please help me out.
Why "here" is printed many times?
How to remove element permanently?
Thanks
The sub wchR_Deactivate will be invoked many times because it happens every time when dialog lost focus.
Normally you should not do AddLabel in _Activeate() event handler. All the thing should be created ahead. You could do Win32::GUI::Hide or Win32::GUI::Show to manipulate the control display status, and using $wchRW->wchR_lb0->Text($new_message) to change the message.