Recent

Author Topic: Creating a simple GUI  (Read 13869 times)

rnfpc

  • Full Member
  • ***
  • Posts: 101
Creating a simple GUI
« on: June 21, 2019, 05:35:08 pm »
What is the easiest way to create a GUI application in freepascal, say just a window with one button? I want to do it with code in a text file, rather than with a visual IDE. How is it possible?

There is no mention of GUI applications on this reference page: https://www.freepascal.org/docs-html/current/ref/ref.html

I want something similar to lines of Tkinter in Python:

Code: Pascal  [Select][+][-]
  1.  from tkinter import *
  2.  window = Tk()
  3.  window.title("Welcome to LikeGeeks app")
  4.  window.geometry('350x200')
  5.  lbl = Label(window, text="Hello")
  6.  lbl.grid(column=0, row=0)
  7.  btn = Button(window, text="Click Me")
  8.  btn.grid(column=1, row=0)
  9.  window.mainloop()
Above code is from: https://likegeeks.com/python-gui-examples-tkinter-tutorial/#Adding-a-button-widget
« Last Edit: June 21, 2019, 05:44:23 pm by rnfpc »

RAW

  • Hero Member
  • *****
  • Posts: 870
Re: Creating a simple GUI
« Reply #1 on: June 21, 2019, 05:47:40 pm »
LAZARUS IDE
https://sourceforge.net/projects/lazarus/files/


Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$mode objfpc}{$H+}
  3.  
  4. uses
  5.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  6.    cthreads,
  7.   {$ENDIF}{$ENDIF}
  8.    Interfaces,
  9.    Forms, StdCtrls;
  10.   {$R *.res}
  11.  
  12. procedure MyWnd;
  13. var
  14.  wnd: TForm;
  15.  btn: TButton;
  16. begin
  17.   wnd         := TForm.Create(Application);
  18.   wnd.Height  := 300;
  19.   wnd.Width   := 400;
  20.   wnd.Position:= poDesktopCenter;
  21.   wnd.Caption := 'LAZARUS WND';
  22.  
  23.   btn          := TButton.Create(wnd);
  24.   btn.SetBounds(0, 0, 100, 50);
  25.   btn.Caption  := 'Click Me';
  26.   btn.Parent   := wnd;
  27.  
  28.   wnd.ShowModal;
  29. end;
  30.  
  31. begin
  32.   Application.Initialize;
  33.   MyWnd;
  34. end.

Maybe this ...  :)
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$mode objfpc}{$H+}
  3.  
  4. uses
  5.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  6.    cthreads,
  7.   {$ENDIF}{$ENDIF}
  8.    Interfaces,
  9.    Forms, Classes, StdCtrls, Dialogs;
  10.   {$R *.res}
  11.  
  12. procedure ShowMainWnd(x, y, w, h: Integer);
  13. var       // main form as one proc, no units, no type...
  14.  wnd : TForm;
  15.  btn1, btn2: TButton;
  16.  clk1, clk2: TNotifyEvent;
  17.  
  18.  // all events inside the proc
  19.  procedure Button1Click(Sender: TObject);
  20.  begin
  21.    ShowMessage('Button 1');
  22.  end;
  23.  
  24.  procedure Button2Click(Sender: TObject);
  25.  begin
  26.    ShowMessage('Button 2');
  27.  end;
  28.  
  29. begin
  30.   wnd          := TForm.Create(Application);
  31.   wnd.SetBounds(x, y, w, h);
  32.   wnd.Caption  := 'My Window Proc';
  33.  
  34.   TMethod(clk1).Code:= @Button1Click;
  35.   TMethod(clk2).Code:= @Button2Click;
  36.  
  37.   btn1          := TButton.Create(wnd);
  38.   btn1.Caption  := 'Button 1';
  39.   btn1.SetBounds(0, 0, 100, 50);
  40.   btn1.OnClick  := clk1;
  41.   btn1.Parent   := wnd;
  42.  
  43.   btn2          := TButton.Create(wnd);
  44.   btn2.Caption  := 'Button 2';
  45.   btn2.SetBounds(0, 80, 100, 50);
  46.   btn2.OnClick  := clk2;
  47.   btn2.Parent   := wnd;
  48.  
  49.   wnd.ShowModal;
  50. end;
  51.  
  52. begin
  53.   RequireDerivedFormResource:=True;
  54.   Application.Scaled:=True;
  55.   Application.Initialize;
  56.   Application.TaskBarBehavior:= tbSingleButton;
  57.   ShowMainWnd(500, 500, 400, 300);
  58. end.


Or like this if you like to use some other procedures and a taskbar button ...
Normally in Lazarus everybody uses units !!!
Code: Pascal  [Select][+][-]
  1. program Project1;
  2. {$mode objfpc}{$H+}
  3.  
  4. uses
  5.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  6.    cthreads,
  7.   {$ENDIF}{$ENDIF}
  8.    Interfaces,
  9.    Forms, Classes, StdCtrls, Dialogs;
  10.   {$R *.res}
  11.  
  12. type
  13.   TMyForm = Class(TForm)
  14.     private
  15.      procedure OnClick(Sender: TObject);
  16.     public
  17.      constructor Create(AOwner: TComponent); override;
  18.   end;
  19.  
  20. constructor TMyForm.Create(AOwner: TComponent);
  21. var
  22.  btn: TButton;
  23.  lbl: TLabel;
  24. begin
  25.   inherited Create(AOwner);
  26.     Height  := 300;
  27.     Width   := 400;
  28.     Position:= poDesktopCenter;
  29.     Caption := 'LAZARUS WND';
  30.  
  31.     btn          := TButton.Create(Self);
  32.     btn.SetBounds(0, 20, 100, 50);
  33.     btn.Caption  := 'Click Me';
  34.     btn.OnClick  := @OnClick;
  35.     btn.Parent   := Self;
  36.  
  37.     lbl          := TLabel.Create(Self);
  38.     lbl.Caption  := 'Hello I Am A Label';
  39.     lbl.SetBounds(0, 0, 100, 50);
  40.     lbl.Parent   := Self;
  41. end;
  42.  
  43. procedure TMyForm.OnClick(Sender: TObject);
  44. begin
  45.   ShowMessage('Hello World');
  46. end;
  47.  
  48. procedure StartMyApp;
  49. var
  50.  wnd: TMyForm;
  51. begin
  52.   Application.Initialize;
  53.   Application.CreateForm(TMyForm, wnd);
  54.   Application.Run;
  55. end;
  56.  
  57. begin
  58.   StartMyApp;
  59. end.
« Last Edit: June 23, 2019, 06:05:50 am by RAW »

Handoko

  • Hero Member
  • *****
  • Posts: 5458
  • My goal: build my own game engine using Lazarus
Re: Creating a simple GUI
« Reply #2 on: June 21, 2019, 06:39:48 pm »
... I want to do it with code in a text file, rather than with a visual IDE. How is it possible?

Here, you can find RAW's, Thaddy's and jc99's examples for creating form and showing message without using Lazarus:

https://forum.lazarus.freepascal.org/index.php/topic,37425.msg251442.html#msg251442

Your life can be easier if you use Lazarus.
« Last Edit: June 21, 2019, 06:55:15 pm by Handoko »

rnfpc

  • Full Member
  • ***
  • Posts: 101
Re: Creating a simple GUI
« Reply #3 on: June 21, 2019, 07:24:09 pm »
Thanks @RAW and @Handoko . This is exactly what I was looking for. I find they do not work with "fpc" command but compile, build and run from within Lazarus. I am getting used to Lazarus now and finding it very useful.

Handoko

  • Hero Member
  • *****
  • Posts: 5458
  • My goal: build my own game engine using Lazarus
Re: Creating a simple GUI
« Reply #4 on: June 21, 2019, 07:32:46 pm »
... I find they do not work with "fpc" command

I haven't tried but they should work. You didn't fully read Thaddy's comment in the post, here I quote it for you:
"You have to set a lot of the correct paths, but it works."

You can set paths for fpc via command line options:
https://www.freepascal.org/docs-html/user/userap1.html
« Last Edit: June 21, 2019, 07:40:16 pm by Handoko »

rnfpc

  • Full Member
  • ***
  • Posts: 101
Re: Creating a simple GUI
« Reply #5 on: June 21, 2019, 07:46:49 pm »
Well, now I find Lazarus to be simpler. But I will go through these options also.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Creating a simple GUI
« Reply #6 on: June 22, 2019, 01:29:49 am »
As a side note: Most of the Lazarus examples in the folder "{Lazarus}/examples/" create everything dinamicaly (i.e. without a .lfm resource) so those are also good ... well,  examples ;)

And there are makefiles from which you can grok how to build applications like that.
« Last Edit: June 22, 2019, 04:35:06 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

RAW

  • Hero Member
  • *****
  • Posts: 870
Re: Creating a simple GUI
« Reply #7 on: June 22, 2019, 01:45:00 am »
Quote
create everything dinamicaly
???? I cannot see any folder named like that ... where exactly ?

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Creating a simple GUI
« Reply #8 on: June 22, 2019, 02:16:34 am »
Quote
create everything dinamicaly
???? I cannot see any folder named like that ... where exactly ?

Well, in this machine they are at:
/usr/share/lazarus/2.0.2/examples/*
in Linux and at:
C:\lazarus\examples\*.*
in Windows.

This is, for example, HelloForm.pp
Code: Pascal  [Select][+][-]
  1. {
  2.  ***************************************************************************
  3.  *                                                                         *
  4.  *   This source is free software; you can redistribute it and/or modify   *
  5.  *   it under the terms of the GNU General Public License as published by  *
  6.  *   the Free Software Foundation; either version 2 of the License, or     *
  7.  *   (at your option) any later version.                                   *
  8.  *                                                                         *
  9.  *   This code is distributed in the hope that it will be useful, but      *
  10.  *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
  11.  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
  12.  *   General Public License for more details.                              *
  13.  *                                                                         *
  14.  *   A copy of the GNU General Public License is available on the World    *
  15.  *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
  16.  *   obtain it by writing to the Free Software Foundation,                 *
  17.  *   Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1335, USA.   *
  18.  *                                                                         *
  19.  ***************************************************************************
  20. }
  21. unit HelloForm;
  22.  
  23. {$mode objfpc}
  24. {$H+}
  25.  
  26. interface
  27.  
  28. uses SysUtils, Classes, Forms, Buttons, StdCtrls;
  29.  
  30. type
  31.    THello = class(TForm)
  32.      button1 : TButton;
  33.    public
  34.      constructor Create(AOwner: TComponent); override;
  35.      procedure button1Click(Sender : TObject);
  36.    end;
  37.  
  38. var
  39.    Hello : THello;
  40.  
  41. implementation
  42.  
  43. constructor THello.Create(AOwner: TComponent);
  44. begin
  45.    inherited CreateNew(AOwner, 1);
  46.    Caption := 'Hello World';
  47.    Width := 200;
  48.    Height := 75;
  49.    Left := 200;
  50.    Top := 200;
  51.  
  52.    button1 := TButton.Create(Self);
  53.    button1.OnClick := @button1click;
  54.    button1.Parent := Self;
  55.    button1.left := (width - 75) div 2 ;
  56.    button1.top := (height - 32) div 2;
  57.    button1.width := 75;
  58.    button1.height := 32;
  59.    button1.caption := 'Close';
  60.    button1.Show;
  61.    
  62.    Self.Constraints.MaxWidth:= 500;
  63. end;
  64.  
  65. procedure THello.button1Click(Sender : TObject);
  66. begin
  67.   close;
  68. end;
  69.  
  70. end.

And lots of them are single-file programs, i.e. programs with the form declaration/definition inside the main (only) source file. Like e.g. loadpicture.pas
« Last Edit: June 22, 2019, 02:26:18 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

RAW

  • Hero Member
  • *****
  • Posts: 870
Re: Creating a simple GUI
« Reply #9 on: June 22, 2019, 04:16:11 am »
Aah... ok, I thought there is really a folder named like "create xyz dynamically ..." or something like that, I thought I overlooked something...
Yeah, the examples are a nice starting point and often people don't take a look inside the Lazarus folder structure.  :)

by the way: Google + "Delphi" and "stackoverflow" is always another good starting point ... not everything works with Lazarus, but a lot of examples do.

giahung1997

  • Full Member
  • ***
  • Posts: 113
Re: Creating a simple GUI
« Reply #10 on: June 22, 2019, 07:35:25 am »
What is the easiest way to create a GUI application in freepascal, say just a window with one button? I want to do it with code in a text file, rather than with a visual IDE. How is it possible?

There is no mention of GUI applications on this reference page: https://www.freepascal.org/docs-html/current/ref/ref.html

I want something similar to lines of Tkinter in Python:

Code: Pascal  [Select][+][-]
  1.  from tkinter import *
  2.  window = Tk()
  3.  window.title("Welcome to LikeGeeks app")
  4.  window.geometry('350x200')
  5.  lbl = Label(window, text="Hello")
  6.  lbl.grid(column=0, row=0)
  7.  btn = Button(window, text="Click Me")
  8.  btn.grid(column=1, row=0)
  9.  window.mainloop()
Above code is from: https://likegeeks.com/python-gui-examples-tkinter-tutorial/#Adding-a-button-widget

Best lightweight, this Free Pascal has a Windows unit which provide Windows API. Consult online tutorial about how to do GUI programming with WinAPI using plain c and convert the syntax to Pascal.

This Pascal also has GTK+ binding despite not the latest. Consult online tutorial about how to do GTK GUI in plain C, Glade and convert the syntax to Pascal.

...

But why going through all of this masturbation? Want a Single Diaglog with only a Click me button? Fire up Lazarus and use the form designer, copy the generated code and save it as a template on your favorite self masterbation text editor and done. Why people border which such a thread? Why no one care about my thread even when I serious? Only because hate me? Fuck!  8-) 8-) 8-) 8-)

 

TinyPortal © 2005-2018