A graphical user interface (GUI) for the excellent console program 'gnuplot' in the context of using the SHELL and EXEC instructions is presented.
Figure 21.3.6.1: GUI for the console programme 'gnuplot'.
The project is fully developed and oriented towards solving the task of how to develop and code a graphical user interface for a (console) program in Gambas.
The following requirements are implemented:
If the console programme 'gnuplot' is installed, then after a click on the button 'Start GNUPLOT' a process is started and the start script is processed. This results in this graphical representation for the start example:
Figure 21.3.6.2: Console programme 'gnuplot' in a separate window
You can then write and save your own GnuPlot scripts or edit and modify saved scripts. Also have a look at the supplied collection of GnuPlot scripts in the GnuPlot project to get ideas for your own GnuPlot graphics.Interesting is the solution of the subtask to install the console program 'gnuplot', which is not one of the standard programs of every Linux distribution. The basic idea of the solution is to start a programme that tests whether the programme 'gnuplot' is installed. If the test is positive, the start programme is terminated and the graphical user interface (GUI) starts immediately. In the other case, it offers to install the missing programme 'gnuplot':
Illustration 21.3.6.3: Option: Installation of 'gnuplot'
If you decide to install 'gnuplot', you still have to enter the required password and the installation will be started:
Figure 21.3.6.4: Installation of 'gnuplot'.
After successful installation, the GUI → Figure 21.3.6.1 opens and you can work. The source code for the start programme is given in full and then briefly commented:
[1] ' Gambas class file [2] [3] Private sProgrammName As String [4] [5] Public Sub Form_Open() [6] Dim sText As String [7] [8] FStart.Center [9] FStart.Resizable = False [10] FStart.Caption = "+ + + + ATTENTION! + + + +" [11] [12] ' sPaketName = Lower("gnuplot") ' Option [13] sProgrammName = Lower("gnuplot") [14] [15] If CheckProgram(sProgrammName) = True Then [16] Start() [17] Else [18] sText = "<center>The application '" & Application.Name & "' requires the program '" & \ [19] sProgrammName & "',<br>" [20] sText &= "um Kurven und Flächen abzubilden!<br><br>" [21] sText &= "<font color='red'>" [22] sText &= "The application '" & sProgrammName & "' is <u>not</u> installed." [23] sText &= "</font></center>" [24] txlHinweis.Text = sText [25] Endif [26] [27] End [28] [29] Public Sub btnInstall_Click() [30] Dim sShellCommand, sMessage As String [31] [32] If GetTerminalList().Count > 0 Then [33] sShellCommand = "echo 'Installation " & sProgrammName & " ';" [34] sShellCommand &= "echo '---------------------------';" [35] sShellCommand &= "sudo apt-get install " & sProgrammName [36] GoToTerminal(GetTerminalList()[0], sShellCommand) [37] Start() [38] Else [39] sMessage = "No terminal programme was found!<br>" [40] sMessage &= "The mapping of curves and surfaces is not possible!<br><br>" [41] sMessage &= "The program " & Application.Name & " will be terminated!" [42] Message.Warning(sMessage) [43] FStart.Close() [44] Endif [45] End [46] [47] Public Sub btnCancel_Click() [48] Dim sMessage As String [49] [50] sMessage = "<font color='red'>" [51] sMessage &= "The Application '" & Application.Name & "' will be terminated!" [52] sMessage &= "</font></center>" [53] Message.Warning(sMessage) [54] FStart.Close() [55] End [56] '************************************************************************************* [57] Public Sub Start() [58] FStart.Delete() '-- Start program [59] FMain.Show() '-- GUI [60] End [61] [62] Private Function GetTerminalList() As String[] [63] Dim hWhich As Process [64] Dim aCurrentTerminalList, aTerminals As String[] [65] Dim sTerminal As String [66] [67] aCurrentTerminalList = New String[] [68] ' Ubuntu Kubuntu Xubuntu Lubuntu XSystem [69] aTerminals = ["gnome-terminal", "konsole", "xfce4-terminal", "lxterminal", "xterm"] [70] For Each sTerminal In aTerminals [71] hWhich = Exec ["which", sTerminal] Wait For Read [72] If hWhich.Value = 0 Then ' Terminal-Programm gefunden [73] aCurrentTerminalList.Add(sTerminal) [74] Endif [75] Next [76] [77] Return aCurrentTerminalList [78] [79] End [80] [81] Private Function CheckProgram(sProgram As String) As Boolean [82] Dim hWhich As Process [83] [84] sProgrammName = Lower(sProgram) [85] hWhich = Exec ["which", sProgrammName] Wait For Read [86] If hWhich.Value = 0 Then Return True '-- 0 = The specified program is installed [87] [88] End [89] [90] Public Sub GoToTerminal(sTerminal As String, sCommandLine As String) [91] Dim sShellCommand As String [92] [93] sShellCommand = "echo; sudo -k; " '-- The password input is forced! [94] sShellCommand &= sCommandLine [95] sShellCommand &= "; echo; echo 'Continue with ENTER or CTRL+C !'; read dummy" [96] File.Save("/tmp/command.sh", sShellCommand) [97] Exec ["chmod", "+x", "/tmp/command.sh"] [98] [99] Select Case Lower(sTerminal) [100] Case "gnome-terminal" [101] Shell sTerminal & " --geometry 77x12+100+100 -t 'ROOT-TERMINAL' -e " & "'/tmp/command.sh'" Wait [102] Case "konsole" [103] Shell sTerminal & " --geometry 75x10+100+100 -t 'ROOT-TERMINAL' -e " & "'/tmp/command.sh'" Wait [104] Case "xfce4-terminal" [105] Shell sTerminal & " --geometry 75x10+100+100 -T 'ROOT-TERMINAL' -e " & "'/tmp/command.sh'" Wait [106] Case "lxterminal" [107] Shell sTerminal & " --geometry 75x10+100+100 -T 'ROOT-TERMINAL' -e " & "'/tmp/command.sh'" Wait [108] Case "xterm" [109] Shell sTerminal & " -bg blue -fg white -cr gray -b 15 -bc -u8 -geometry 75x11+100+100 \ [110] -title 'ROOT-TERMINAL' -e " & "'/tmp/command.sh'" Wait [111] End Select ' sTerminal [112] [113] End
Comments:
Note:
The GUI - like all the example programmes in this chapter 21 - starts a process that is accessed read and write. This becomes clear in the following source code excerpt. A special feature characterises the process hGnuPlot started here, as all data output by the process (data or error messages) can only be read via channel 2:
Public hGnuPlot As Process '-- hGnuPlot is a process variable (for the SHELL command, among others). .. Private Sub GnuPlotStart() GnuPlotStop hGnuPlot = Shell "gnuplot" For Read Write As "hGnuPlot" If Not IsNull(hGnuPlot) Then If hGnuPlot.State = hGnuPlot.Running Then Print #hGnuPlot, txaPlotCode.Text btnGnuPlotStop.Enabled = True btnRedrawGraph.Enabled = True btnOpenScriptFile.Enabled = True Endif Endif End Private Sub GnuPlotStop() If Not IsNull(hGnuPlot) Then If hGnuPlot.State = hGnuPlot.Running Then Print #hGnuPlot, "q" ' q = quit btnGnuPlotStop.Enabled = False btnRedrawGraph.Enabled = False Endif Endif '-- Alternative: System-Aufruf: '-- IF NOT IsNull(hGnuPlot) THEN '-- IF hGnuPlot.State = 1 THEN SHELL "kill -s 9 " & hGnuPlot.Id '-- ENDIF End Public Sub hGnuplot_Error(sError_OR_Result As String) txaPlotCode.Height = 160 txaErrorAndFit.Background = Color.RGB(255, 223, 223) txaErrorAndFit.Visible = True txaErrorAndFit.SetFocus ' Channel 2 is used for *all* GnuPlot outputs! If InStr(sError_OR_Result, "Iteration 0") <> 0 Then sError_OR_Result = gb.NewLine & "|==== RESULTATE ==========================================|" & sError_OR_Result btnResultSave.Visible = True Endif txaErrorAndFit.Text &= sError_OR_Result '-- Alternative: txaErrorAndFit.Insert(sError_OR_Result) Error.Clear() End Public Sub hGnuplot_Read() '-- *No* data arrives here at gnuplot. '-- Only via channel 2 (standard error output) *all* output comes from GnuPlot! End
Project