如何在不使用子窗口的情况下调用另一个Tk窗口?

时间:2021-10-07 07:16:49

I am making GUI (login window). When the password is correct, the login window must call other window. Is there a way in PerlTk to call another window rather than using subwindow?

我正在制作GUI(登录窗口)。密码正确时,登录窗口必须调用其他窗口。在PerlTk中有没有办法调用另一个窗口而不是使用子窗口?

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");

1 个解决方案

#1


1  

Do you just want separate MainWindows? Once you construct each MainWindow, you construct the various widgets to reference the right variables. Here's a short program that has a button in one MainWindow and a button-press counter in the other MainWindow:

你想要单独的MainWindows吗?构建每个MainWindow之后,构造各种小部件以引用正确的变量。这是一个简短的程序,在一个MainWindow中有一个按钮,在另一个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;

#1


1  

Do you just want separate MainWindows? Once you construct each MainWindow, you construct the various widgets to reference the right variables. Here's a short program that has a button in one MainWindow and a button-press counter in the other MainWindow:

你想要单独的MainWindows吗?构建每个MainWindow之后,构造各种小部件以引用正确的变量。这是一个简短的程序,在一个MainWindow中有一个按钮,在另一个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;