-
hello world in gtk# problems
Hello there,
Ive begun poking my nose into gtk#...Not for any commercial reason, just because i'm bored
Ive written a hello world program that should also count the number of times a button has been pressed as well as be able to reset the counter. Rather straight forward, right?
It compiles fine, but when go to run it, absolutely nothing, no window, no error output, nothing.
Edit --
I should note that the functionality(none) is the same with or without the commented parts being commented.
I've searched around the mono documentation and nothing looks blatantly wrong to me..
Here are some references that I've used as well
http://www.mono-project.com/GtkSharpBeginnersGuide
http://www.mono-project.com/GtkSharp:_Hello_World
http://www.go-mono.com/docs/index.as...=ecmaspec%3a17
Heres the C# source code
Code:
using System;
using Gtk;
using Glade;
public class hello_world
{
// Widget declarations
[Widget] Gtk.Label helloLbl;
[Widget] Gtk.Label countLbl;
// Unsure if these need to be declared or not
// [Widget] Gtk.Button whoBtn;
// [Widget] Gtk.Button resetBtn;
string updatedHelloText = "Hello World!";
string origonalText = "Hello...?";
string counterText = "This button has been clicked ";
string counterEnd = " times";
int count = 0;
public static void Main (string[] args)
{
new hello_world (args);
}
public hello_world (string[] args)
{
Application.Init();
Glade.XML gxml = new Glade.XML ("hello_world.glade", "mainWindow", null);
gxml.Autoconnect(this);
//Add the events
//I'm unsure if these events need to be added, seeing how they're declared in glade?
// whoBtn.Clicked += on_whoBtn_clicked;
// resetBtn.Clicked += on_resetBtn_clicked;
Application.Run();
}
public void on_resetBtn_clicked( object o, EventArgs e)
{
count = 0;
countLbl.Text = concatenateText();
helloLbl.Text = origonalText;
}
public void on_whoBtn_clicked( object o, EventArgs e)
{
count++;
countLbl.Text = concatenateText();
helloLbl.Text = updatedHelloText;
}
public void on_mainWindow_delete( object o, DeleteEventArgs args)
{
Application.Quit();
args.RetVal = true;
}
string concatenateText()
{
string returnString;
returnString = counterText;
returnString += count.ToString();
returnString += counterEnd;
return returnString;
}
}
Here's the glade file
Code:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE glade-interface SYSTEM "glade-2.0.dtd">
<!--Generated with glade3 3.2.0 on Tue Apr 3 06:42:31 2007 by darkbolt@avalon-->
<glade-interface>
<widget class="GtkWindow" id="mainWindow">
<property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
<property name="title" translatable="yes">Hello World</property>
<property name="resizable">False</property>
<child>
<widget class="GtkLayout" id="layout1">
<property name="visible">True</property>
<property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
<child>
<widget class="GtkLabel" id="helloLbl">
<property name="width_request">150</property>
<property name="height_request">30</property>
<property name="visible">True</property>
<property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
<property name="label" translatable="yes">Hello....?</property>
</widget>
</child>
<child>
<widget class="GtkLabel" id="countLbl">
<property name="width_request">280</property>
<property name="height_request">30</property>
<property name="visible">True</property>
<property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
<property name="label" translatable="yes">This button has been pressed 0 times</property>
</widget>
<packing>
<property name="x">151</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="whoBtn">
<property name="width_request">100</property>
<property name="height_request">32</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
<property name="label" translatable="yes">Who's There
</property>
<signal name="clicked" handler="on_whoBtn_clicked"/>
</widget>
<packing>
<property name="x">41</property>
<property name="y">73</property>
</packing>
</child>
<child>
<widget class="GtkButton" id="resetBtn">
<property name="width_request">100</property>
<property name="height_request">32</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
<property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
<property name="label" translatable="yes">Reset</property>
<signal name="clicked" handler="on_resetBtn_clicked"/>
</widget>
<packing>
<property name="x">247</property>
<property name="y">73</property>
</packing>
</child>
<child>
<widget class="GtkHSeparator" id="hseparator1">
<property name="visible">True</property>
<property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
</widget>
<packing>
<property name="x">93</property>
<property name="y">18</property>
</packing>
</child>
</widget>
</child>
</widget>
</glade-interface>
the command used to compile
Code:
mcs -pkg:glade-sharp-2.0 -resource:hello_world.glade hello_world.cs
the command used to run
Code:
mono hello_world.exe
Last edited by Darkbolt; 04-03-2007 at 12:31 PM.
-
looks like the glade version used to generate the file is too new for your glade-sharp. try generating the xml file using glade-2 instead of glade-3.
try this glade-2 file instead...
Code:
<?xml version="1.0" standalone="no"?> <!--*- mode: xml -*-->
<!DOCTYPE glade-interface SYSTEM "http://glade.gnome.org/glade-2.0.dtd">
<glade-interface>
<widget class="GtkWindow" id="mainWindow">
<property name="visible">True</property>
<property name="title" translatable="yes">window</property>
<property name="type">GTK_WINDOW_TOPLEVEL</property>
<property name="window_position">GTK_WIN_POS_NONE</property>
<property name="modal">False</property>
<property name="resizable">True</property>
<property name="destroy_with_parent">False</property>
<property name="decorated">True</property>
<property name="skip_taskbar_hint">False</property>
<property name="skip_pager_hint">False</property>
<property name="type_hint">GDK_WINDOW_TYPE_HINT_NORMAL</property>
<property name="gravity">GDK_GRAVITY_NORTH_WEST</property>
<property name="focus_on_map">True</property>
<property name="urgency_hint">False</property>
<child>
<widget class="GtkVBox" id="vbox1">
<property name="border_width">20</property>
<property name="visible">True</property>
<property name="homogeneous">False</property>
<property name="spacing">20</property>
<child>
<widget class="GtkHBox" id="hbox1">
<property name="visible">True</property>
<property name="homogeneous">False</property>
<property name="spacing">0</property>
<child>
<placeholder/>
</child>
<child>
<widget class="GtkLabel" id="helloLbl">
<property name="width_request">150</property>
<property name="height_request">30</property>
<property name="visible">True</property>
<property name="label" translatable="yes">Hello....?</property>
<property name="use_underline">False</property>
<property name="use_markup">False</property>
<property name="justify">GTK_JUSTIFY_LEFT</property>
<property name="wrap">False</property>
<property name="selectable">False</property>
<property name="xalign">0.5</property>
<property name="yalign">0.5</property>
<property name="xpad">0</property>
<property name="ypad">0</property>
<property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
<property name="width_chars">-1</property>
<property name="single_line_mode">False</property>
<property name="angle">0</property>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">True</property>
<property name="fill">True</property>
</packing>
</child>
<child>
<placeholder/>
</child>
<child>
<widget class="GtkLabel" id="countLbl">
<property name="width_request">280</property>
<property name="height_request">30</property>
<property name="visible">True</property>
<property name="label" translatable="yes">This button has been pressed 0 times</property>
<property name="use_underline">False</property>
<property name="use_markup">False</property>
<property name="justify">GTK_JUSTIFY_LEFT</property>
<property name="wrap">False</property>
<property name="selectable">False</property>
<property name="xalign">0.5</property>
<property name="yalign">0.5</property>
<property name="xpad">0</property>
<property name="ypad">0</property>
<property name="ellipsize">PANGO_ELLIPSIZE_NONE</property>
<property name="width_chars">-1</property>
<property name="single_line_mode">False</property>
<property name="angle">0</property>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">True</property>
<property name="fill">True</property>
</packing>
</child>
<child>
<placeholder/>
</child>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">False</property>
<property name="fill">False</property>
</packing>
</child>
<child>
<widget class="GtkHBox" id="hbox2">
<property name="visible">True</property>
<property name="homogeneous">False</property>
<property name="spacing">0</property>
<child>
<placeholder/>
</child>
<child>
<widget class="GtkButton" id="whoBtn">
<property name="width_request">100</property>
<property name="height_request">32</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="label" translatable="yes">Who's There
</property>
<property name="use_underline">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
<signal name="clicked" handler="on_whoBtn_clicked"/>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">False</property>
<property name="fill">False</property>
</packing>
</child>
<child>
<placeholder/>
</child>
<child>
<widget class="GtkButton" id="resetBtn">
<property name="width_request">100</property>
<property name="height_request">80</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="label" translatable="yes">Reset</property>
<property name="use_underline">True</property>
<property name="relief">GTK_RELIEF_NORMAL</property>
<property name="focus_on_click">True</property>
<signal name="clicked" handler="on_resetBtn_clicked"/>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">False</property>
<property name="fill">False</property>
</packing>
</child>
<child>
<placeholder/>
</child>
</widget>
<packing>
<property name="padding">0</property>
<property name="expand">False</property>
<property name="fill">False</property>
</packing>
</child>
<child>
<placeholder/>
</child>
</widget>
</child>
</widget>
</glade-interface>
Last edited by tucolino; 04-03-2007 at 06:29 PM.
Reason: wrong code shown
-
The other possibility might be that since your newly-created hello_world instance (in your static Main) is never actually used, the compiler may be optimizing out the call to the constructor. And if the constructor is never called, the Application.Run() call will never be made, and of course nothing will happen.
Otherwise, you may need to add a call to Show() (or ShowAll()) on your Window object. I'm not sure whether libglade does that for you or not.
-
tucolino ftw!
bwkaz, what you said made sense to me, and I tried moving the code into Main() I got a compile error on gxml.Autoconnect(this);
http://www.go-mono.com/docs/monodoc....error%3aCS0026
Anyway, I tried out the glade file generated by glade2.0 (I had used 3.0 to create my file), and it goes off without a hitch.
Thank you both for your time and gray matter
-
good to know it helped...
if you got a few lines of C# to spare, you can always give a hand to burro
http://burro.sourceforge.net
-
Oh -- yeah, "this" doesn't work from inside a static function. There is no "this" -- static functions are shared between all instances of your class.
But the glade-version thing makes sense to me too (the version of glade that you use to write out the XML file should match the version of libglade used by Gtk#). Good that at least one of our suggestions worked.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|