Simple windowed application: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
Line 148: Line 148:
end
end


==[[Tcl]]==
=={{header:Tcl}}==
{{library:Tk}}
[[Category:Tcl]]


pack [label .l -text "There have been no clicks yet."]
pack [label .l -text "There have been no clicks yet."]

Revision as of 17:19, 21 September 2007

Task
Simple windowed application
You are encouraged to solve this task according to the task description, using any language you may know.

This tasks asks to create a window with a label that says "There have been no clicks yet" and a button that says "click me". Upon clicking the button with the mouse, the label should change and show the number of times the button has been clicked.

Delphi

Compiler: 5.0

Creating a "basic windowed application" in Delphi is quite simple. Start up the IDE and the default application is a single windowed app. This will have two files, the main project file "project1" and a unit file, "unit1". What I a going to do here is going to be somewhat different. I will do it all dynamically from the main "program file". Create this in any text editor and you can then either open it in the IDE to build it, or invoke the command line compiler. There is quite a bit going on behind the sceens with the VCL and the RTL.


Filename = SingleWinApp.dpr

NOTE: The project name here must match the name of the file.


-- begin file --

   Program SingleWinApp ;
 
   // This is the equivilet of the C #include
   Uses Forms, Windows, Messages, Classes, Graphics, Controls, StdCtrls ;

   
   type
     
     // The only reason for this declaration is to allow the connection of the
     // on click method to the forms button object.  This class declaration adds
     // a procedure.

     TMainForm class(tform) 
       Procedure AddClicks(sender : tObject);
     end;


   // Use these globals.  
   var
  
     MainForm : tForm ;
     aLabel   : tLabel ;
     aButton  : tButton ;
     i        : integer = 0 ;


    // This is the Method call that we connect to the button object
    // to start counting the clicks.
    Procedure tMainForm.AddClicks(sender :tObject)
    begin
      inc(i);
      aLabel.Caption := IntToStr(i) + ' Clicks since startup' ;
    end;


    Begin
      // Do all the behind the scenes stuff that sets up the Windows environment 
      Application.Initialize ;

      // Creat the form
      
      // Forms can either be created with an owner, like I have done here, or with
      // the owner set to Nil.  In pascal ( all versions of Borland ) '''NIL''' is a
      // reserverd, (the eqivilent of '''NULL''' in Ansi C) word and un-sets any pointer 
      // variable. Setting the owner to the application object will ensure that the form is
      // freed by the application object when the application shuts down.  If I had set
      // the owner to NIL then i would have had to make sure I freed the form explicitly
      // or it would have been orphaned, thus creating a memory leak.

      // I must direct your attention to the CreateNew constructor.  This is 
      // a non standard useage.  Normally the constructor Create() will call this
      // as part of the initialization routine for the form. Normaly as you drop
      // various components on a form in deign mode, a DFM file is created with 
      // all the various initial states of the controls.  This bypasses the 
      // DFM file altogether although all components AND the form are created
      // with default values.  ( see the Delphi help file ).

      MainForm          := tMainForm.CreateNew(Application);
      MainForm.Parent   := Application ;
      MainForm.Position := poScreenCenter ;
      MainForm.Caption  := 'Single Window Application' ;

      // Create the Label, set its owner as MaiaForm
      aLabel          := tLabel.Create(mainForm);
      aLabel.Parent   := MainForm;
      aLabel.Caption  := IntToStr(i) + ' Clicks since startup' ;
      aLabel.Left     := 20 ;
      aLabel.Top      := MainForm.ClientRect.Bottom div 2 ;

      // Create the button, set its owner to MainForm
      aButton         := tButton.Create(MainForm);
      aButton.Parent  := MainForm ;
      aButton.Caption := 'Click Me!';
      aButton.Left    := (MainForm.ClientRect.Right div 2)-(aButton.Width div 2 );
      aButton.Top     := MainForm.ClientRect.Bottom - aButton.Height - 10 ;
      aButton.OnClick := AddClicks ;

      // Show the main form, Modaly.  The ONLY reason to do this is because in this
      // demonstration if you only call the SHOW method, the form will appear and
      // disapear in a split second.
      MainForm.ShowModal ;
      
      Application.Run ;

   end. // Program 

E

Java AWT/Swing

Implementation: E-on-Java

when (currentVat.morphInto("awt")) -> {
    var clicks := 0
    def w := <swing:makeJFrame>("Rosetta Code 'Simple Windowed Application'")
    w.setContentPane(JPanel`
        ${def l := <swing:makeJLabel>("There have been no clicks yet.")} $\
            ${def b := <swing:makeJButton>("Click Me")}
    `)
    b.addActionListener(def _ {
        to actionPerformed(_) { 
            clicks += 1
            l.setText(`Number of clicks: $clicks`)
        }
    })
    w.pack()
    w.show()
}

IDL

pro counter, ev
  widget_control, ev.top, get_uvalue=tst
  tst[1] = tst[1]+1
  widget_control, tst[0], set_value="Number of clicks: "+string(tst[1],format='(i0)')
  widget_control, ev.top, set_uvalue=tst
end
 
id = widget_base(title = 'Window Title',column=1)
ld = widget_label(id, value = 'There have been no clicks yet.')
widget_control, /realize, id, set_uvalue=[ld,0]
dummy = widget_button(id,value=' Click Me ',event_pro='counter')
xmanager, "Simple", Id
 
end

Template:Header:Tcl

Template:Library:Tk

pack [label .l -text "There have been no clicks yet."]
set count 0
pack [button .b -text " Click Me " -command upd]
proc upd {} {.l configure -text "Number of clicks: [incr ::count]."}

Python

Library
This is an example of a library. You may see a list of other libraries used on Rosetta Code at Category:Solutions by Library.
from Tkinter import Tk, Label, Button

def update_label():
    global n
    n += 1
    l["text"] = "Number of clicks: %d" % n

w = Tk()
n = 0
l = Label(w, text="There have been no clicks yet")
l.pack()
Button(w, text="click me", command=update_label).pack()
w.mainloop()