2008-11-04 20 views
2

我在製作GUI(登錄窗口)。當密碼正確時,登錄窗口必須調用其他窗口。在PerlTk中有沒有辦法調用另一個窗口而不是使用子窗口?如何在不使用子窗口的情況下調用另一個Tk窗口?

use strict; 

use Tk; 

my $mw = MainWindow->new; 
$mw->geometry("300x150"); 
$mw->configure(-background=>'gray',-foreground=>'red'); 
$mw->title("PLEASE LOGIN"); 

my $main_frame=$mw->Frame(
    -background=>"gray",-relief=>"ridge",)->pack(-side=>'top',-fill=>'x'); 
my $left_frame=$main_frame->Frame(
    -background=>"gray")->pack(-side=>'left',-fill=>'x'); 
my $bottom_frame1=$mw->Frame(
    -background=>"gray")->pack(-side=>'bottom',-fill=>'x'); 
my $right_frame1=$mw->Frame(
    -background=>"gray")->pack(-side=>'left',-fill=>'x'); 


my $button=$bottom_frame1->Button(-text=>"OK",-command=>\&push_button); 
$button->pack(-side=>'left'); 
my $cancel=$bottom_frame1->Button(-text=>"CANCEL",-command=>sub{$mw->destroy}); 
$cancel->pack(-side=>'right'); 
my $entry2=$mw->Entry(-width=>20,-relief=>"ridge")->place(-x=>100,-y=>75); 

sub push_button{ 
    ... 
    } 

my $mw=MainWindow->new; 
$mw->geometry("900x690"); 
+0

這段代碼是很奇怪的,因爲你有這麼多的用途和要求陳述。 – 2008-11-04 06:56:47

回答

1

你只是想單獨的MainWindows?一旦構建了每個MainWindow,就可以構造各種小部件來引用正確的變量。下面是有一個主窗口的按鈕,並在其他主窗口的按鈕按下計數器短節目:

 
#!/usr/local/bin/perl 

use Tk; 

# The other window as its own MainWindow 
# It will show the number of times the button 
# in the other window is pressed 
my $other_window = MainWindow->new; 
$other_window->title("Other Window"); 
my $other_frame = $other_window->Frame->pack(
    -fill => 'both' 
    ); 

my $other_label = $other_frame->Label(
    -text => 'Pressed 0 times', 
    )->pack(
     -side => 'top', 
     -fill => 'x', 
    ); 

# The login window as its own MainWindow 
my $login_window = MainWindow->new; 
$login_window->title("Login Window"); 
my $login_frame = $login_window->Frame->pack(
    -fill => 'both' 
    ); 


my $login_label = $login_frame->Label(
    -text => 'Press the button', 
    )->pack(
     -side => 'top', 
     -fill => 'x', 
    ); 


my $pressed = 0; 
my $login_button = $login_frame->Button(
    -text => 'Button', 
    -command => sub { # references $other_label 
     $pressed++; 
     $other_label->configure(-text => "Pressed $pressed times"); 
     }, 
    )->pack(
     -side => 'top', 
     -fill => 'both', 
    ); 


MainLoop; 
相關問題