I have a Raspberry Pi 3 B+. I would like to have the hostname displayed in the top bar (LXPanel), similar to a panel applet/plugin.
How can this be done?
I have a Raspberry Pi 3 B+. I would like to have the hostname displayed in the top bar (LXPanel), similar to a panel applet/plugin.
How can this be done?
There are several ways to display the hostname on the lxpanel.
(#1) A simple way is to write a program to post the hostname. An example would be a python program which is in a file $HOME/bin/dsphost1.py
with execute permisson (chmod +x dsphost1.py
). This program will read the hostname and open a window with the hostname on an exit button (or label). This window is placed three fifths on the lxpanel (and can be placed any where on the display). If the bin
director was just created, the PATH
variable will need to be modified, export PATH="$HOME/bin:$PATH"
or reboot (see .profile).
#!/usr/bin/env python
# Program name is dsphost1.py
import Tkinter as tk # For python3, use tkinter.
mywindow = tk.Tk()
fp = open("/etc/hostname", "r")
hn = fp.read().strip()
fp.close()
w = str((len(hn) + 1) * 14)
x = str(mywindow.winfo_screenwidth() / 5 * 3)
whxy = w + "x36+" + x + "+1" # wh = width/height, xy = coordinates
mywindow.geometry(whxy)
tk.Button(text = hn, font=("Courier", 18), command=quit).pack()
#tk.Label(text = hn, font=("Courier", 18)).pack()
mywindow.overrideredirect(1)
mywindow.mainloop()
To test type dsphost1.py &
and to stop, click on the button or use the kill ...
command with pid provided for the background process. Adjust width, height and coordinates for your needs. To run the program on reboot/boot, append @dsphost1.py
to the end of $HOME/.config/lxsession/LXDE-pi/autostart
file.
(#2) Same as #1 using label with known hostname at different coordinates.
#!/usr/bin/env python
# Program name is dsphost2.py
import Tkinter as tk
mywindow = tk.Tk()
mywindow.geometry("50x16+0+36")
tk.Label(text="RPi4", font=("Arial 14 bold")).pack() #works better
#tk.Label(text="RPi4", font=("Arial Bold", 14)).pack()
mywindow.overrideredirect(1)
mywindow.mainloop()
(#3) The default panel ($HOME/.config/lxpanel/LXDE-pi/panels/panel
) has three sections. The left is menu/launch, center is running window programs selection/iconification and right is task/applets. Another way to use the panel is to open a window program.
#!/usr/bin/env python
# Program name is dsphost3.py.
import Tkinter as tk
mywindow = tk.Tk()
fp = open('/etc/hostname', 'r')
hn = fp.read().strip()
fp.close()
img = tk.PhotoImage(file = '/usr/share/lxpanel/images/my-computer.png')
mywindow.tk.call('wm', 'iconphoto', mywindow._w, img)
mywindow.title(hn)
mywindow.geometry('-2+36')
#mywindow.geometry('1x1-1-1') # For hidden window.
tk.Button(text = hn, font=('Courier', 18), command=quit).pack()
#label = tk.Label(mywindow, text = hn, font=('Courier', 18)).pack()
mywindow.mainloop()
(#4) The lxpanel has a GUI to add and delete items that are predefined (right click on the panel). Sometimes it is easier to make your own. Three things are needed to create a launch button, a program to launch, a .desktop
file, and configuration in the panel
file. First, the program in $HOME/bin/dsphost4.py
.
#!/usr/bin/env python
# Program name is dsphost4.py.
import Tkinter as tk
def exit():
quit()
mywindow = tk.Tk()
mywindow.title('RPi4')
x = str(mywindow.winfo_screenwidth() / 4)
mywindow.geometry('+' + x + '+36')
tk.Button(text = 'RPi4', font=('Courier', 18), command=quit).pack()
mywindow.overrideredirect(1)
mywindow.after(5000, exit)
mywindow.mainloop()
Second, the .desktop
file in $HOME/.local/share/applications/PiHost4.desktop
. If mouse pointer on icon, the Name will hover. If you have access to make a .png or .ico (like Windows Paint), a 64x64 pixel or less can be used for Icon=/home/pi/.local/share/your_icon.ico
(full path required - $HOME
will not work).
[Desktop Entry]
Name=RPi4
TryExec=dsphost4.py
Exec=dsphost4.py
Icon=my-computer
Terminal=false
Type=Application
Categories=Utility
StartupNotify=true
Third, modify the $HOME/.config/lxpanel/LXDE-pi/panels/panel
file, by adding the PiHost4.desktop
stanza at the end of the lanuchbar
stanza. There may be two different kind of stanza.
Plugin {
type=launchbar
Config {
Button {
id=lxde-x-www-browser.desktop
}
Button {
id=pcmanfm.desktop
}
...
Button {
id=PiHost4.desktop
}
...
OR
Plugin {
type=launchbar
Config {
Button {
id=chromium-browser.desktop
}
}
}
...
Plugin {
type=launchbar
Config {
Button {
id=PiHost4.desktop
}
}
}
The command lxpanelctl restart
will use the new changes to the panel. Use a different directory to back up the panel
as multiple files here are multiple panels.
EDIT: On Python 2.7 with TKinter the format of font=("Times 14 bold")
works better than the orginal post.
I have wanted to do something similar, but can find no existing code, so I decided to write a LXpanel plugin
#include <lxpanel/plugin.h>
#include <stdio.h>
#define LabelSize 32
GtkWidget *test_constructor(LXPanel *panel, config_setting_t *settings)
{
/* panel is a pointer to the panel and
settings is a pointer to the configuration data
since we don't use it, we'll make sure it doesn't
give us an error at compilation time */
(void)panel;
(void)settings;
// make a label out of the hostname
char cIdBuf[LabelSize+1] = {'\0'};
FILE *fp;
fp = fopen("/etc/hostname", "r");
fgets(cIdBuf, LabelSize, fp);
fclose(fp);
// create a label widget instance
GtkWidget *pLabel = gtk_label_new(cIdBuf);
// set the label to be visible
gtk_widget_show(pLabel);
// need to create a container to be able to set a border
GtkWidget *p = gtk_event_box_new();
// our widget doesn't have a window...
// it is usually illegal to call gtk_widget_set_has_window() from application but for GtkEventBox it doesn't hurt
gtk_widget_set_has_window(p, FALSE);
// set border width
gtk_container_set_border_width(GTK_CONTAINER(p), 1);
// add the label to the container
gtk_container_add(GTK_CONTAINER(p), pLabel);
// set the size we want
// gtk_widget_set_size_request(p, 100, 25);
// success!!!
return p;
}
FM_DEFINE_MODULE(lxpanel_gtk, test)
/* Plugin descriptor. */
LXPanelPluginInit fm_module_init_lxpanel_gtk = {
.name = "HostnamePlugin",
.description = "Display Hostname",
.one_per_system = 1,
// assigning our functions to provided pointers.
.new_instance = test_constructor
};
To compile from source
sudo apt install lxpanel-dev
gcc -Wall `pkg-config --cflags gtk+-2.0 lxpanel` -shared -fPIC hostname.c -o hostname.so `pkg-config --libs lxpanel`
For Raspberry Pi OS-Bullseye (with GTK3)
gcc -Wall `pkg-config --cflags gtk+-3.0 lxpanel` -shared -fPIC hostname.c -o hostname.so `pkg-config --libs lxpanel`
The source and compiled versions Raspberry Pi OS-Buster
(GTK2 or earlier) Raspberry Pi OS-Bullseye
32 or 64 bit can be downloaded Hostname
@Milliways Hi is it also possible to show the current IP from the Raspberry (Bullseye 32-bit)? I saw a file called get_ip.c in your source Thanks