Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Perl/Tk Calculator

Empfohlene Antworten

Veröffentlicht

Hallo,

Ich stecke gerade mitten in der Ausbildung zum Fachinformatiker Systemintegration. Habe jetzt aber in einem Praktikum die Aufgabe mit Perl und Tk zu arbeiten.

Als Übung für mich selbst, wollte ich mal einen kleinen Taschenrechner programmieren, der mich vor folgendes Problem stellt:

Das GUI ist soweit fertig. Ich bräuchte aber jetzt einen Algorhytmus der mir die eingegebenen Tasten so in einen Skalar überträgt, das dieser erstens Live auf dem Label (was als Display dienen soll) erscheint und zweitens Ich damit rechnen kann und somit das Ergebnis dann auch auf dem Display ausgeben kann.

Und für alle die jetzt gleich das Flamen anfangen das dieses ding einen Funktionsumfang von einer Handgranate hat, es dient lediglich zum Verständnis. :P

Hier noch mein Code den ich bis jetzt umgesetzt habe (Ich weiß ich hätte die Buttons auch über eine schleife generieren können, mir fehlt halt noch ein wenig die Routine):

#!perl

use strict;

use warnings;

use Tk;

# *** Globale Variablen ***

my $entry = 0;

# *** Hauptfenster ***

my $mw = MainWindow->new;

$mw->title("Chilli Calculator");

# *** Rahmen ***

my $frame_display = $mw->Frame(

-relief => 'groove',

-background => 'white',

-borderwidth => 2,

)->pack(

-side => 'top',

-fill => 'x'

);

my $frame_keyboard = $mw->Frame(

)->pack(

-side => 'left',

);

my $frame_keyboard2 = $mw->Frame(

)->pack(

-side => 'right',

);

# *** Anzeige ***

my $display = $frame_display->Label(

-textvariable => \$entry,

-background => 'white',

)->pack( -side => 'right', );

# *** Tastatur ***

my $keyboard = $frame_keyboard->Button(

-text => "1",

-command => sub { print "1" }

)->grid(

$frame_keyboard->Button(

-text => "2",

-command => sub { print "2" }

),

$frame_keyboard->Button(

-text => "3",

-command => sub { print "3" }

)

);

$frame_keyboard->Button(

-text => "4",

-command => sub { print "4" }

)->grid(

$frame_keyboard->Button(

-text => "5",

-command => sub { print "5" }

),

$frame_keyboard->Button(

-text => "6",

-command => sub { print "6" }

)

);

$frame_keyboard->Button(

-text => "7",

-command => sub { print "7" }

)->grid(

$frame_keyboard->Button(

-text => "8",

-command => sub { print "8" }

),

$frame_keyboard->Button(

-text => "9",

-command => sub { print "9" }

)

);

$frame_keyboard->Button(

-text => "0",

-command => sub { print "0" }

)->grid(

$frame_keyboard->Button(

-text => "C",

-command => sub { print "Clear" }

),

"-",

-sticky => "nsew"

);

$frame_keyboard2->Button(

-text => "+",

-command => sub { print "+" },

-width => '5',

)->pack(

-side => 'top',

);

$frame_keyboard2->Button(

-text => "-",

-command => sub { print "-" },

-width => '5',

)->pack(

-side => 'top',

);

$frame_keyboard2->Button(

-text => "*",

-command => sub { print "*" },

-width => '5',

)->pack(

-side => 'top',

);

$frame_keyboard2->Button(

-text => "/",

-command => sub { print "/" },

-width => '5',

)->pack(

-side => 'top',

);

MainLoop();

ich weiß nicht was TK ist, aber es gibt sachen, die macht man einfach nicht :old

perl = arbeitstier für dateimanipulation. gui und gedöns, man nehme irgend was wo man nur noch den grid fertig macht.

Du codest dir einen wolf mit dem ding, schon alleine der view, sollte es einer sein, ist kaum leserlich.

vielleicht bin ich aber auch komplett neben der spur ;)

Ich kann ja auch nicht 's für das die dass im Praktikum von mir verlangen. :hells:

Mal abgesehen davon das Tcl/Tk einiges an Kritik vertragen muss, mir bereitet es eigentlich schon Spaß damit zu arbeiten. Und Perl ist ja wohl total genial. Wenn ich das Programm auf Konsolenbasis schreiben müsste, wär das ein Vierzeiler (der Handgranatentaschenrechner).

Leider fehlt mir (noch) das Verständnis um das auch in Tk umzusetzen, was auch eigentlich der Grund für dieses Topic war. :D

Vielleicht findet sich ja doch noch wer, der die Lösung mal eben aus dem Ärmel schüttelt (wenn ich es bis dahin nicht selbst geschafft habe). :cool:

Danke schon mal für eure Mühe. Grüße.

Hab das ganze jetzt mit ein wenig spielerei und rauchendem Kopf gelöst.

Hier mein Ergebnis:

#!perl

use strict;

use warnings;

use Tk;

use Math::Complex;

# *** Globale Variablen ***

my $wert;

my $eingabe = "0";

# *** Hauptfenster ***

my $mw = MainWindow->new;

$mw->title("Chilli Calculator");

# *** Rahmen ***

my $frame_display = $mw->Frame(

-relief => 'groove',

-background => 'white',

-borderwidth => 2,

)->pack(

-side => 'top',

-fill => 'x',

);

my $frame_keyboard = $mw->Frame()->pack( -side => 'left', );

my $frame_keyboard2 = $mw->Frame()->pack( -side => 'left', );

my $frame_keyboard3 = $mw->Frame()->pack( -side => 'left', );

# *** Anzeige ***

my $display = $frame_display->Label(

-textvariable => \$eingabe,

-background => 'white',

)->pack( -side => 'right', );

# *** Tastatur ***

my $keyboard = $frame_keyboard->Button(

-text => "1",

-command => sub { nummer("1") },

-activebackground => 'NavyBlue',

-activeforeground => 'white',

)->grid(

$frame_keyboard->Button(

-text => "2",

-command => sub { nummer("2") },

-activebackground => 'NavyBlue',

-activeforeground => 'white',

),

$frame_keyboard->Button(

-text => "3",

-activebackground => 'NavyBlue',

-activeforeground => 'white',

-command => sub { nummer("3") },

)

);

$frame_keyboard->Button(

-text => "4",

-command => sub { nummer("4") },

-activebackground => 'NavyBlue',

-activeforeground => 'white',

)->grid(

$frame_keyboard->Button(

-text => "5",

-command => sub { nummer("5") },

-activebackground => 'NavyBlue',

-activeforeground => 'white',

),

$frame_keyboard->Button(

-text => "6",

-command => sub { nummer("6") },

-activebackground => 'NavyBlue',

-activeforeground => 'white',

)

);

$frame_keyboard->Button(

-text => "7",

-command => sub { nummer("7") },

-activebackground => 'NavyBlue',

-activeforeground => 'white',

)->grid(

$frame_keyboard->Button(

-text => "8",

-command => sub { nummer("8") },

-activebackground => 'NavyBlue',

-activeforeground => 'white',

),

$frame_keyboard->Button(

-text => "9",

-command => sub { nummer("9") },

-activebackground => 'NavyBlue',

-activeforeground => 'white',

)

);

$frame_keyboard->Button(

-text => "0",

-command => sub { nummer("0") },

-activebackground => 'NavyBlue',

-activeforeground => 'white',

)->grid(

$frame_keyboard->Button(

-text => ".",

-command => sub { nummer(".") },

-activebackground => 'NavyBlue',

-activeforeground => 'white',

),

"-",

-sticky => "nsew"

);

$frame_keyboard2->Button(

-text => "+",

-command => sub { nummer("+") },

-activebackground => 'NavyBlue',

-activeforeground => 'white',

-width => '5',

)->pack( -side => 'top', );

$frame_keyboard2->Button(

-text => "-",

-command => sub { nummer("-") },

-activebackground => 'NavyBlue',

-activeforeground => 'white',

-width => '5',

)->pack( -side => 'top', );

$frame_keyboard2->Button(

-text => "*",

-command => sub { nummer("*") },

-activebackground => 'NavyBlue',

-activeforeground => 'white',

-width => '5',

)->pack( -side => 'top', );

$frame_keyboard2->Button(

-text => "/",

-command => sub { nummer("/") },

-activebackground => 'NavyBlue',

-activeforeground => 'white',

-width => '5',

)->pack( -side => 'top', );

$frame_keyboard3->Button(

-text => "DEL",

-command => sub { $eingabe = substr $eingabe, 0, ( length($eingabe) - 1 ) },

-activebackground => 'NavyBlue',

-activeforeground => 'white',

-width => '10',

)->pack( -side => 'top', );

$frame_keyboard3->Button(

-text => "SQRT",

-command => sub { $eingabe = sqrt($eingabe) },

-activebackground => 'NavyBlue',

-activeforeground => 'white',

-width => '10',

)->pack( -side => 'top', );

$frame_keyboard3->Button(

-text => "CLEAR",

-command => sub { $eingabe = "0" },

-activebackground => 'NavyBlue',

-activeforeground => 'white',

-width => '10',

)->pack( -side => 'top', );

$frame_keyboard3->Button(

-text => "CALCULATE",

-command => sub { $eingabe = eval($eingabe); },

-activebackground => 'NavyBlue',

-activeforeground => 'white',

-width => '10',

)->pack( -side => 'top', );

# *** Funktionen ***

sub nummer {

$wert = shift;

if ( ( $eingabe eq "0" ) && ( $wert ne "." ) ) {

$eingabe = $wert;

}

else {

$eingabe = "$eingabe" . "$wert";

}

}

MainLoop();

Und noch ein kleines Bild von dem guten Stück.

Liebe Grüße.

post-75257-14430448824709_thumb.jpg

perl = arbeitstier für dateimanipulation. gui und gedöns, man nehme irgend was wo man nur noch den grid fertig macht.

Du codest dir einen wolf mit dem ding, schon alleine der view, sollte es einer sein, ist kaum leserlich.

Frei nach Dieter Nuhr: Wenn man keine Ahnung hat, einfach mal die Schnauze halten. :)

@19_reaper_87:

Mit Moose haettest du das Ganze noch ein wenig aufpeppen koennen. Via augment oder BUILD haettest du die GUI aufbauen koennen. Die Callbacks der verschiedenen Elemente haetten dann auf anonyme Subroutinen, die Methoden deiner Klasse aufrufen werden koennen.

Aber wenn es sowieso nur so eine kleine Anwendung ist, braucht man auch nicht mit Kanonen auf Spatzen zu schiessen.

Ich meine ich hatte grade im Code einen eval gesehen. Vorsicht damit. Bei solchen Sachen wie 42 / 0 koennte das schief gehen. Auch bei besonders grossen Zahlen koennte das daneben gehen.

Math::Pari kann komplexe mathematische Fragestellungen beantworten, ohne dass man eval bemuehen muss. :)

Huhu,

erst mal Danke für die Tipps!

Hatte die Zahlen und Operatoren erst versucht durch parsing abzufangen, und sie dann verschiedenen Skalaren zu zuordnen. Mit einer while Schleife dann auf mehrere Operatoren und Skalare erweitern.

Hat eigentlich auch Funktioniert, aber irgendwie hatte ich dann den Geistesblitz die Funktion und Eval einzubauen.

Sicherlich ist das Programm nicht perfekt, aber es ist ja auch mein erstes Skript. :D

Grüße, Reaper.

Jetzt hab ich noch 'ne anderer Frage, gibt es Tools die .pl zu einer .exe zu kompilieren?

Habe es mal mit dem Activestate Tool gemacht, das kostet aber leider einiges und die trial läuft nur 21 Tage.

Gibt es kostenfreie Alternativen? Muss nicht unbedingt eine Executable sein, sollte aber Stand alone ausführbar sein.

Was auch stört, das Dos Fenster popppt jedes mal mit auf wenn ich die Executable ausführe.

Grüße. Reaper.

Ja den hab ich auch gefunden, aber bin irgendwie zu doof ihn einzusetzen. :rolleyes:

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.