PDA

View Full Version : graphical app



yureka
5th July 2012, 08:55 AM
hi all, how to make some graphical application in linux? Like, when it is executed, a window showing some message like "welcome" should appear with some good background and all. is there any language or tool with which we can do it?

george_toolan
5th July 2012, 10:05 AM
Try http://docs.oracle.com/javase/tutorial/getStarted/cupojava/unix.html

secipolla
5th July 2012, 12:03 PM
gtkdialog http://www.murga-linux.com/puppy/viewtopic.php?p=276729
yad http://code.google.com/p/yad/
I'm not a programmer so I don't know if these is what you're looking for.

RupertPupkin
5th July 2012, 07:58 PM
I'd go with Tcl/Tk (http://tcl.tk/). You'll need to install the tk package (as root):

yum install tk
As an example of how simple Tcl/Tk is, save this code in a text file called tkwindow:

#!/usr/bin/wish
wm title . "Tk Window Example"
wm geometry . 400x150
. configure -bg "darkslateblue"
label .msg -text "Welcome!" -font {-size 32} -bg "darkslateblue" -fg "white"
pack .msg -pady 10
button .ok -text "OK" -command exit
pack .ok -pady 20
Then make the file executable and run it. In a terminal window you'd do:

chmod 755 tkwindow
./tkwindow
The result will look like this:

ah7013
6th July 2012, 05:27 AM
Similar to the one above by Rupert, but with Perl/Tk. For this one you need the perl-Tk package.


yum install perl-Tk


Here it is:


#!/usr/bin/perl

use Tk;

my $mw = MainWindow->new(-title => "Perl/Tk Window Example", -background => "darkslateblue");
$mw->geometry("400x150");
my $msg = $mw->Label(-text => "Welcome!", -font => [-size => 32], -background => "darkslateblue", -foreground => "white")->pack(-pady => 10);
my $ok = $mw->Button(-text => "OK", -command => sub { exit(0) })->pack(-pady => 20);
MainLoop;


And as you can see pretty much the same result (but with Perl/Tk of course) -

RupertPupkin
6th July 2012, 07:37 AM
Similar to the one above by Rupert, but with Perl/Tk.
Cool! For completeness we should include the Python/Tkinter version. For this you need the tkinter package to be installed:

yum install tkinter
Then use this code, which works with Python 2.x:

#!/usr/bin/python
from Tkinter import *
from tkFont import Font
root = Tk()
root.title("Tkinter Window Example")
root.geometry("400x150")
root.configure(bg="darkslateblue")
msg = Label(root, text="Welcome!", font=Font(size=32), bg="darkslateblue", fg="white")
msg.pack(pady=10)
ok = Button(root, text="OK", command=root.quit)
ok.pack(pady=20)
root.mainloop()
The result looks the same as the Tcl and Perl versions.

I did notice that the Tcl version uses the least amount of memory and the Perl version uses the most:

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
rpupkin 3297 0.4 0.6 67256 12628 pts/0 S 02:10 0:00 /usr/bin/perl /home/code/perl/perltkwindow
rpupkin 3298 0.2 0.3 47752 7620 pts/0 S 02:10 0:00 /usr/bin/wish /home/code/tcl/tkwindow
rpupkin 3299 0.7 0.5 63008 11984 pts/0 S 02:10 0:00 /usr/bin/python /home/code/python/pytkwindow
For something using a compiled language (e.g. C) instead of a scripting language, there are lots of GUI toolkits. I gave one example in this thread (http://forums.fedoraforum.org/showthread.php?p=1568461#post1568461) using the XForms toolkit for C.

Here's a screenshot of the Python/Tkinter version:

exiledangel420
6th July 2012, 08:22 AM
I'm not sure with fedora 17 but I found the easiest language for me on ubuntu to be python. The main reason is you can run the code right from the idle and see instant results. But it really depends on how far you want to with it.

ah7013
6th July 2012, 11:43 AM
Cool! For completeness we should include the Python/Tkinter version.

Excellent! I wonder why the Perl version consumes the most memory. anyway, one more from me, because that's all I know. :( This one is in Ruby. On Mageia it requires the ruby-tk rpm but on Fedora I think it requires the ruby-tcktk rpm. As with the other examples the result looks exactly the same.


#!/usr/bin/ruby
require 'tk'

root = TkRoot.new do
title "Ruby/Tk Window Example"
background "darkslateblue"
geometry "400x150"
end
msg = TkLabel.new(root) do
text 'Welcome!'
background "darkslateblue"
foreground "white"
font TkFont.new("size" => 32)
pack("pady" => 10)
end
ok = TkButton.new(root) do
text "OK"
command proc { exit }
pack("pady" => 20)
end
Tk.mainloop

And this is what it looks like (btw on my other laptop which has WindowMaker 0.95.2 instead of KDE :) ) -

Camberwell
6th July 2012, 06:59 PM
I think python QT (PyQt) is good method to use



import sys
from PyQt4 import QtCore, QtGui

class MyFirstWindow(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.resize(200,100)
label = QtGui.QLabel("Welcome")
label.setStyleSheet("color: white; font-size: 32px")
button = QtGui.QPushButton("OK")
button.clicked.connect(self.close)
box = QtGui.QVBoxLayout()
box.addWidget(label,1,QtCore.Qt.AlignHCenter)
box.addWidget(button,1,QtCore.Qt.AlignHCenter)
self.setLayout(box)

app = QtGui.QApplication(sys.argv)
window = MyFirstWindow()
window.show()
app.exec_()

jpollard
6th July 2012, 07:38 PM
Minor nit in that pyqt - "label.setStyleSheet("color: white;..." line causes the Welcome message to be white on white... :)

Camberwell
6th July 2012, 07:45 PM
Minor nit in that pyqt - "label.setStyleSheet("color: white;..." line causes the Welcome message to be white on white... :)

Ok, no problem, my background color is defaulted to grey, here I'll set the background color to the blue used in the others examples...



import sys
from PyQt4 import QtCore, QtGui

class MyFirstWindow(QtGui.QWidget):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.resize(200,100)
pal = QtGui.QPalette()
pal.setColor(QtGui.QPalette.Background, QtGui.QColor("#483D8B"))
self.setAutoFillBackground(1)
self.setPalette(pal)
label = QtGui.QLabel("Welcome")
label.setStyleSheet("color: white; font-size: 32px")
button = QtGui.QPushButton("OK")
button.clicked.connect(self.close)
box = QtGui.QVBoxLayout()
box.addWidget(label,1,QtCore.Qt.AlignHCenter)
box.addWidget(button,1,QtCore.Qt.AlignHCenter)
self.setLayout(box)

app = QtGui.QApplication(sys.argv)
window = MyFirstWindow()
window.show()
app.exec_()


Martin

RupertPupkin
6th July 2012, 10:49 PM
Here is the now obligatory Java version (javawindow.java):

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class javawindow {
public static void main(String[] args) {
JFrame f = new JFrame("Java Window Example");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new BorderLayout());
JLabel msg = new JLabel("Welcome!", JLabel.CENTER);
msg.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 32));
msg.setOpaque(true);
msg.setBackground(new Color(72,61,139));
msg.setForeground(Color.WHITE);
f.add(msg, BorderLayout.CENTER);
JPanel bottom = new JPanel();
bottom.setBackground(new Color(72,61,139));
JButton ok = new JButton("OK");
ok.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
System.exit(0);
}
});
bottom.add(ok);
f.add(bottom, BorderLayout.SOUTH);
f.getContentPane().setPreferredSize(new Dimension(400,150));
f.pack();
f.setVisible(true);
}
}
Compile and run it:

$ javac javawindow.java
$ java -Dawt.useSystemAAFontSettings=on -Dswing.aatext=true javawindow

Gareth Jones
7th July 2012, 12:50 AM
hi all, how to make some graphical application in linux? Like, when it is executed, a window showing some message like "welcome" should appear with some good background and all. is there any language or tool with which we can do it?

So many options! Choose your preferred language (C/C++/C#/Java/Perl/Python/Ruby/Tcl/... any number of options here), then your preferred GUI tool-kit (plain X/Athena, Tk, LessTif, GTK+, Qt, Fltk...), find a binding for your preferred language (e.g. Gtkmm for GTK+ with C++, Gtk# for GTK+ with C#, etc...).

There's no right or wrong answer to this question, and without more information about the kind of app you want to right it's impossible to do more than list your options. If you literally just want to show a "welcome" window, you could do that with a shell script. The most commonly used (and most "native" as far as most desktop environments are concerned) combinations are GTK+ with C, or Gtkmm or Qt with C++, but these aren't the simplest to start with. For a simple starting point, I'd look at Python with PyGTK (for GNOME, LXDE or Xfce etc.) or PyQt (for KDE).

RupertPupkin
7th July 2012, 01:34 AM
This one is in Ruby ... on Fedora I think it requires the ruby-tcktk rpm
That's great! I vaguely remembered there being some Ruby bindings to Tk, but I had no idea how to use them. And yes, it does require the ruby-tcltk package.

Here's one more, in C using the XForms toolkit (yum install xforms-devel):

#include <forms.h>

int main(int argc, char *argv[]) {
FL_FORM *form;
FL_OBJECT *box, *msg, *ok, *all;
fl_initialize(&argc, argv, "XFormsWindow", 0, 0);
form = fl_bgn_form(FL_UP_BOX, 400, 150);
box = fl_add_box(FL_FLAT_BOX, 0, 0, 400, 150, "");
fl_set_object_color(box, FL_DARKSLATEBLUE, FL_DARKSLATEBLUE);
msg = fl_add_text(FL_NORMAL_TEXT, 140, 20, 120, 40, "Welcome!");
fl_set_object_color(msg, FL_DARKSLATEBLUE, FL_DARKSLATEBLUE);
fl_set_object_lsize(msg, FL_HUGE_SIZE);
fl_set_object_lcol(msg, FL_WHITE);
fl_set_object_lalign(msg, FL_ALIGN_CENTER);
ok = fl_add_button(FL_NORMAL_BUTTON, 175, 110, 50, 30, "OK");
fl_end_form();
fl_show_form(form, FL_PLACE_MOUSE, FL_TRANSIENT, "XForms Window Example");
while ((all = fl_do_forms()) != ok)
/* empty */ ;
fl_finish();
return 0;
}

$ gcc -o xformswindow xformswindow.c -lforms
$ ./xformswindow
No 32pt font or antialiasing, unfortunately. :(

ah7013
7th July 2012, 11:52 AM
Here's one more, in C using the XForms toolkit (yum install xforms-devel):


Sweet! When it comes to C i'm a complete newbie (still haven't got my head around things like pointers) though one day it would be good to start learning so I can try out things like Perl's XS.

RupertPupkin
7th July 2012, 04:30 PM
Choose your preferred language (C/C++/C#/Java/Perl/Python/Ruby/Tcl/... any number of options here), then your preferred GUI tool-kit (plain X/Athena, Tk, LessTif, GTK+, Qt, Fltk...)
One language that tends to gets overlooked in Linux is Objective-C, the object-oriented superset of C that is an alternative to C++. It's mostly associated with Apple's OS X and its Cocoa GUI, but it can be used to create GUI applications in Linux with the GNUstep (http://gnustep.org) toolkit.

Do this to install GNUstep in Fedora 17:

yum install gcc-objc libobjc gnustep-back gnustep-base-devel gnustep-gui-devel

Here's the GNUstep version of the example we've been using (save in a file called gswindow.m):

#include <AppKit/AppKit.h>

@interface MyDelegate : NSObject
{
NSWindow *win;
}
- (void) createWindow;
- (void) applicationWillFinishLaunching: (NSNotification *)not;
- (void) applicationDidFinishLaunching: (NSNotification *)not;
@end

@implementation MyDelegate : NSObject
- (void) dealloc
{
[super dealloc];
}

- (void) createWindow
{
win = [NSWindow alloc];
win = [win initWithContentRect: NSMakeRect (400, 300, 400, 150)
styleMask: (NSTitledWindowMask | NSClosableWindowMask | NSMiniaturizableWindowMask)
backing: NSBackingStoreBuffered
defer: NO];
NSColor *bg = [NSColor colorWithCalibratedRed:0.282f green:0.239f blue:0.545f alpha:1.0f];
[win setBackgroundColor: bg];

NSTextField *msg = [[NSTextField alloc] initWithFrame: NSMakeRect(90,85,220,50)];
[msg setSelectable: NO];
[msg setBezeled: NO];
[msg setDrawsBackground: YES];
[msg setStringValue: @"Welcome!"];
[msg setTextColor: [NSColor whiteColor]];
[msg setBackgroundColor: bg];
[msg setFont: [NSFont fontWithName:@"DejaVu Sans" size:42.0]];

NSButton *ok = [[NSButton alloc] initWithFrame: NSMakeRect(180,15,40,28)];
[ok setTitle: @"OK"];
[ok setTarget: win];
[ok setAction: @selector (terminate:)];

[[win contentView] addSubview: msg];
[msg release];
[[win contentView] addSubview: ok];
[ok release];
[win setTitle: @"GNUstep Example Window"];
[win center];
}

- (void) applicationWillFinishLaunching: (NSNotification *)not
{
[self createWindow];
}

- (void) applicationDidFinishLaunching: (NSNotification *)not;
{
[win makeKeyAndOrderFront: nil];
}
@end

int main (int argc, const char **argv)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
id app = [NSApplication sharedApplication];
[NSApp setDelegate: [MyDelegate new]];
[app run];
[pool release];
return 0;
}
Definitely more verbose than the other examples (which is one of the complaints about Objective-C), but if you're interested in writing apps for OS X or for iOS-based devices like the iPad and iPhone then it's worth learning.

The traditional way of compiling GNUstep applications is to use a makefile, resulting in a special <application>.app directory being created, and then you run "openapp <application>". But a simple application such as the one above can be compiled and run directly like this:

$ gcc -fconstant-string-class=NSConstantString -o gswindow gswindow.m -lobjc -lgnustep-base -lgnustep-gui
$ ./gswindow
The result looks like this:

Camberwell
7th July 2012, 04:37 PM
I'm sorry but If I were out to learn a new language, I would take one look at that and move straight on.

I find python very readable, and the pythonic way of doing things just seems right to me.

Martin :)

RupertPupkin
11th July 2012, 09:18 PM
Sure, it's a matter of preference. For example, I know plenty of developers who go running and screaming from seeing Python code, with its lack of braces and forced indentation for code blocks, syntactic whitespace, excessive underscores, and so on. Everyone will have different tastes; there's no one language that will suit all people.