|
|
楼主 |
发表于 2026-6-27 16:57:52
|
显示全部楼层
本帖最后由 zzz19760225 于 2026-6-30 20:45 编辑
模仿cmd界面的perl问答输入输出页面
- #!/usr/bin/perl
- use strict;
- use warnings;
- use utf8;
- use Encode;
- use Tk;
- use Tk::ROText;
- # Linux系统使用UTF-8编码
- binmode(STDOUT, ':encoding(utf8)');
- binmode(STDIN, ':encoding(utf8)');
- binmode(STDERR, ':encoding(utf8)');
- my $mw = MainWindow->new;
- $mw->title("智能命令行 - 支持图片显示");
- $mw->geometry("800x600");
- my $main_frame = $mw->Frame->pack(-fill => 'both', -expand => 1);
- my $output = $main_frame->Scrolled('ROText',
- -scrollbars => 'e',
- -wrap => 'word',
- -font => ['Courier', 10],
- -background => 'black',
- -foreground => 'lime',
- )->pack(-fill => 'both', -expand => 1, -side => 'top');
- my $input_frame = $main_frame->Frame->pack(-fill => 'x', -side => 'bottom');
- my $prompt = $input_frame->Label(
- -text => '> ',
- -font => ['Courier', 10],
- -background => 'black',
- -foreground => 'lime',
- )->pack(-side => 'left');
- my $input = $input_frame->Entry(
- -font => ['Courier', 10],
- -background => 'black',
- -foreground => 'lime',
- -insertbackground => 'lime',
- -width => 80,
- )->pack(-side => 'left', -fill => 'x', -expand => 1);
- my $send_btn = $input_frame->Button(
- -text => '发送',
- -command => \&process_command,
- -background => 'gray30',
- -foreground => 'white',
- )->pack(-side => 'right');
- $input->bind('<Return>' => \&process_command);
- my @history;
- my $history_index = -1;
- sub insert_chinese {
- my ($text) = @_;
- $output->insert('end', $text);
- }
- # 显示欢迎信息
- insert_chinese("=" x 60 . "\n");
- insert_chinese("智能命令行系统 v1.0\n");
- insert_chinese("支持命令:\n");
- insert_chinese(" help - 显示帮助\n");
- insert_chinese(" clear - 清屏\n");
- insert_chinese(" image - 显示示例图片\n");
- insert_chinese(" text [内容] - 显示文本\n");
- insert_chinese(" date - 显示当前日期时间\n");
- insert_chinese(" calc [表达式] - 计算数学表达式\n");
- insert_chinese(" echo [内容] - 回显文本\n");
- insert_chinese("=" x 60 . "\n\n");
- # 关键修复:确保滚动到顶部
- $output->see('1.0');
- # 多个延迟确保显示正确
- $mw->after(50, sub { $output->see('1.0'); });
- $mw->after(200, sub { $output->see('1.0'); });
- sub process_command {
- my $command = $input->get();
- $input->delete(0, 'end');
-
- push @history, $command if $command ne '';
- $history_index = -1;
-
- insert_chinese("\n> $command\n");
-
- if ($command eq 'help') {
- show_help();
- }
- elsif ($command eq 'clear') {
- $output->delete('1.0', 'end');
- insert_chinese("屏幕已清空\n");
- }
- elsif ($command eq 'image') {
- show_demo_image();
- }
- elsif ($command =~ /^text\s+(.+)/) {
- insert_chinese("$1\n");
- }
- elsif ($command eq 'date') {
- insert_chinese(scalar(localtime) . "\n");
- }
- elsif ($command =~ /^calc\s+(.+)/) {
- eval_calculation($1);
- }
- elsif ($command =~ /^echo\s+(.+)/) {
- insert_chinese("$1\n");
- }
- else {
- insert_chinese("未知命令: $command (输入 'help' 查看帮助)\n");
- }
-
- $output->see('end');
- }
- sub show_help {
- insert_chinese("\n可用命令:\n");
- insert_chinese(" help - 显示此帮助信息\n");
- insert_chinese(" clear - 清空输出区域\n");
- insert_chinese(" image - 显示示例图片\n");
- insert_chinese(" text <内容> - 在输出区域显示文本\n");
- insert_chinese(" date - 显示当前日期和时间\n");
- insert_chinese(" calc <表达式> - 计算数学表达式\n");
- insert_chinese(" echo <内容> - 回显文本\n");
- insert_chinese("\n");
- }
- sub show_demo_image {
- my $canvas_window = $mw->Toplevel();
- $canvas_window->title("图片显示窗口");
- $canvas_window->geometry("400x300");
-
- my $canvas = $canvas_window->Canvas(
- -background => 'white',
- -width => 400,
- -height => 300,
- )->pack(-fill => 'both', -expand => 1);
-
- $canvas->createRectangle(50, 50, 150, 150,
- -fill => 'red',
- -outline => 'black',
- -width => 2,
- );
-
- $canvas->createOval(200, 50, 300, 150,
- -fill => 'blue',
- -outline => 'black',
- -width => 2,
- );
-
- $canvas->createPolygon(50, 200, 150, 200, 100, 100,
- -fill => 'green',
- -outline => 'black',
- -width => 2,
- );
-
- $canvas->createText(250, 200,
- -text => '示例图形',
- -font => ['Arial', 16],
- -fill => 'purple',
- );
-
- insert_chinese("已在新窗口中显示示例图片\n");
- }
- sub eval_calculation {
- my ($expr) = @_;
- $expr =~ s/\s+//g;
-
- if ($expr !~ /^[\d+\-*\/\(\)\.]+$/) {
- insert_chinese("错误: 表达式包含非法字符\n");
- return;
- }
-
- eval {
- my $result = eval $expr;
- if ($@) {
- insert_chinese("计算错误: $@\n");
- } else {
- insert_chinese("$expr = $result\n");
- }
- };
- }
- $input->bind('<Up>' => sub {
- if ($history_index < @history - 1) {
- $history_index++;
- $input->delete(0, 'end');
- $input->insert(0, $history[-1 - $history_index]);
- }
- });
- $input->bind('<Down>' => sub {
- if ($history_index > 0) {
- $history_index--;
- $input->delete(0, 'end');
- $input->insert(0, $history[-1 - $history_index]);
- } elsif ($history_index == 0) {
- $history_index = -1;
- $input->delete(0, 'end');
- }
- });
- $input->focus();
- # 最终修复:在窗口完全显示后滚动到顶部
- $mw->after(500, sub {
- $output->see('1.0');
- $mw->update();
- });
- MainLoop;
复制代码 运行后:
- ============================================================
- 智能命令行系统 v1.0
- 支持命令:
- help - 显示帮助
- clear - 清屏
- image - 显示示例图片
- text [内容] - 显示文本
- date - 显示当前日期时间
- calc [表达式] - 计算数学表达式
- echo [内容] - 回显文本
- ============================================================
- > 发送
复制代码 deepseek这个作品,形式和颜色是满足需求的。就是中文显示的行里,中文头部被掩盖了一部分。怎么将病症,证候,治法,人物信息装进去,加上增减量和存量,奖罚什么的。老毛病,依然对程序内容看都不看,有了就好了。这样和官僚有什么差别呢,不知道有什么。
要有文字和图片,可能用perl Tk text,Tk模块和text组件。 |
|