PyGobject(三十二)布局容器之Window

本文详细介绍了GtkWindow类及其方法、属性和信号,涵盖了GtkWindow的基本使用和高级特性,如窗口装饰、图标设置、位置调整等。

Gtk.Window

Gtk.Window是一个可以包含其他部件一个顶层窗口。允许用户操作窗口(调整其大小,移动它,关闭它,…)

继承关系

Gtk.Window是Gtk.Bin的直接子类
这里写图片描述
这里写图片描述

Methods

方法修饰词方法名及参数
staticget_default_icon_list ()
staticget_default_icon_name ()
staticlist_toplevels ()
staticnew (type)
staticset_auto_startup_notification (setting)
staticset_default_icon (icon)
staticset_default_icon_from_file (filename)
staticset_default_icon_list (list)
staticset_default_icon_name (name)
staticset_interactive_debugging (enable)
activate_default ()
activate_focus ()
activate_key (event)
add_accel_group (accel_group)
add_mnemonic (keyval, target)
begin_move_drag (button, root_x, root_y, timestamp)
begin_resize_drag (edge, button, root_x, root_y, timestamp)
close ()
deiconify ()
fullscreen ()
fullscreen_on_monitor (screen, monitor)
get_accept_focus ()
get_application ()
get_attached_to ()
get_decorated ()
get_default_size ()
get_default_widget ()
get_deletable ()
get_destroy_with_parent ()
get_focus ()
get_focus_on_map ()
get_focus_visible ()
get_gravity ()
get_group ()
get_has_resize_grip ()
get_hide_titlebar_when_maximized ()
get_icon ()
get_icon_list ()
get_icon_name ()
get_mnemonic_modifier ()
get_mnemonics_visible ()
get_modal ()
get_opacity ()
get_position ()
get_resizable ()
get_resize_grip_area ()
get_role ()
get_screen ()
get_size ()
get_skip_pager_hint ()
get_skip_taskbar_hint ()
get_title ()
get_titlebar ()
get_transient_for ()
get_type_hint ()
get_urgency_hint ()
get_window_type ()
has_group ()
has_toplevel_focus ()
iconify ()
is_active ()
is_maximized ()
maximize ()
mnemonic_activate (keyval, modifier)
move (x, y)
parse_geometry (geometry)
present ()
present_with_time (timestamp)
propagate_key_event (event)
remove_accel_group (accel_group)
remove_mnemonic (keyval, target)
reshow_with_initial_size ()
resize (width, height)
resize_grip_is_visible ()
resize_to_geometry (width, height)
set_accept_focus (setting)
set_application (application)
set_attached_to (attach_widget)
set_decorated (setting)
set_default (default_widget)
set_default_geometry (width, height)
set_default_size (width, height)
set_deletable (setting)
set_destroy_with_parent (setting)
set_focus (focus)
set_focus_on_map (setting)
set_focus_visible (setting)
set_geometry_hints (geometry_widget, geometry, geom_mask)
set_gravity (gravity)
set_has_resize_grip (value)
set_has_user_ref_count (setting)
set_hide_titlebar_when_maximized (setting)
set_icon (icon)
set_icon_from_file (filename)
set_icon_list (list)
set_icon_name (name)
set_keep_above (setting)
set_keep_below (setting)
set_mnemonic_modifier (modifier)
set_mnemonics_visible (setting)
set_modal (modal)
set_opacity (opacity)
set_position (position)
set_resizable (resizable)
set_role (role)
set_screen (screen)
set_skip_pager_hint (setting)
set_skip_taskbar_hint (setting)
set_startup_id (startup_id)
set_title (title)
set_titlebar (titlebar)
set_transient_for (parent)
set_type_hint (hint)
set_urgency_hint (setting)
set_wmclass (wmclass_name, wmclass_class)
stick ()
unfullscreen ()
unmaximize ()
unstick ()

static get_default_icon_list ()

获取由Gtk.Window.set_default_icon_list()设置的值.一次性设置所有窗口的默认图标,包含各种分辨率的图标

static get_default_icon_name ()

获取由Gtk.Window.set_default_icon_name,默认图标的名称

static list_toplevels ()

获取已经存在的顶层窗口列表

static new (type)

    Parameters: type (Gtk.WindowType) – 窗口类型

static set_auto_startup_notification (setting)


static set_default_icon (icon)

设置应用程序的默认图标,目前发现在Gtk.AboutDialog中有用到

static set_default_icon_from_file (filename)

作用同上,不过这个是从给定的路径中加载图片

static set_default_icon_list (list)

设置图标列表,包含各种分辨率的

static set_default_icon_name (name)

设置默认图标名称

static set_interactive_debugging (enable)

显示交互式的Debug界面
这里写图片描述
这里写图片描述

activate_default ()

激活窗口中的默认部件,如果成功返回True

activate_focus ()

激活窗口中获得焦点的部件,如果成功返回True

activate_key (event)

如果一个助记符或者绑定的快捷键被发现,返回True

add_accel_group (accel_group)

    Parameters: accel_group (Gtk.AccelGroup) – a Gtk.AccelGroup
添加快捷键组
参见PyGobject(五十九)布局容器之Menu例三

add_mnemonic (keyval, target)

    Parameters:
       keyval (int) – 助记符
       target (Gtk.Widget) – 绑定的部件
添加助记符

begin_move_drag (button, root_x, root_y, timestamp)

开始拖动窗口

begin_resize_drag (edge, button, root_x, root_y, timestamp)

开始调整窗口大小

close ()

关闭窗口

deiconify ()

取消窗口图标化

fullscreen ()

全屏

fullscreen_on_monitor (screen, monitor)

在指定的一个显示器上全屏

get_accept_focus ()

设置窗口是否可以获得输入焦点

get_application ()

获取窗口所绑定的Gtk.Application

get_attached_to ()

获取窗口所依附的部件

get_decorated ()

获取窗口是使用titlebar来装饰窗口

get_default_size ()

获取窗口默认大小

get_default_widget ()

获取默认部件

get_deletable ()

获取是否已经取消关闭按钮

get_destroy_with_parent ()

获取是否允许当父窗口销毁时自动销毁自己

get_focus ()

获取当前窗口获得焦点的部件

get_focus_on_map ()

当窗口映射后,窗口是否获得能够获得输入焦点

get_focus_visible ()

是否显示焦点矩形框

get_gravity ()


get_group ()


get_has_resize_grip ()

Deprecated

get_hide_titlebar_when_maximized ()

当最大化时是否隐藏titlebar

get_icon ()

获取图标

get_icon_list ()

获取图标列表

get_icon_name ()

获取图标名称

get_mnemonic_modifier ()


get_mnemonics_visible ()

助记符是否可见

get_modal ()


get_opacity ()

获取窗口透明度

get_position ()

获取窗口位置

get_resizable ()

获取是否可调整窗口大小

get_resize_grip_area ()


get_role ()


get_screen ()


get_size ()

获取窗口宽高

get_skip_pager_hint ()


get_skip_taskbar_hint ()


get_title ()

获取标题

get_titlebar ()

获取titlebar

get_transient_for ()


get_type_hint ()


get_urgency_hint ()


get_window_type ()

获取窗口类型

has_group ()


has_toplevel_focus ()

iconify ()

只显示图片,不显示窗口

is_active ()

窗口是否激活

is_maximized ()

窗口是否已经最大化

maximize ()

窗口最大化

mnemonic_activate (keyval, modifier)


move (x, y)

将窗口移动到某个位置

parse_geometry (geometry)


present ()

显示窗口

present_with_time (timestamp)


propagate_key_event (event)


remove_accel_group (accel_group)

移除快捷键组

remove_mnemonic (keyval, target)

移除部件的助记符

reshow_with_initial_size ()


resize (width, height)

调整窗口大小

resize_grip_is_visible ()


resize_to_geometry (width, height)


set_accept_focus (setting)


set_application (application)


set_attached_to (attach_widget)


set_decorated (setting)

默认使用titlebar装饰窗口,包括最大化最小化。
如果设置False,则不显示

set_default (default_widget)

设置默认部件

set_default_geometry (width, height)

Deprecated ,使用set_default_size代替

set_default_size (width, height)

设置窗口的固定大小

set_deletable (setting)

在某些窗口管理系统中取消关闭按钮

set_destroy_with_parent (setting)

当父窗口销毁时,自己是否销毁,常用在Dialog中

set_focus (focus)

设置获取焦点的部件

set_focus_on_map (setting)


set_focus_visible (setting)


set_geometry_hints (geometry_widget, geometry, geom_mask)


set_gravity (gravity)


set_has_resize_grip (value)


set_has_user_ref_count (setting)


set_hide_titlebar_when_maximized (setting)

当最大化的时候,是否隐藏titlebar

set_icon (icon)

设置图标

set_icon_from_file (filename)

设置图标,已给定的文件路径

set_icon_list (list)

设置图标列表

set_icon_name (name)

设置图标名称

set_keep_above (setting)

窗口置顶,永远显示在其他窗口之上

set_keep_below (setting)

窗口在其它窗口之下

set_mnemonic_modifier (modifier)


set_mnemonics_visible (setting)


set_modal (modal)


set_opacity (opacity)

设置透明度

set_position (position)


set_resizable (resizable)

设置是否可调整大小

set_role (role)


set_screen (screen)


set_skip_pager_hint (setting)


set_skip_taskbar_hint (setting)


set_startup_id (startup_id)


set_title (title)

设置标题

set_titlebar (titlebar)

设置Titlebar

set_transient_for (parent)


set_type_hint (hint)


set_urgency_hint (setting)


set_wmclass (wmclass_name, wmclass_class)


stick ()


unfullscreen ()

取消全屏

unmaximize ()

取消最大化

unstick ()


Virtual Methods

do_activate_default ()
do_activate_focus ()
do_enable_debugging (toggle)
do_keys_changed ()
do_set_focus (focus)

Properties

NameTypeFlagsShort Description
accept-focusboolr/w/enTrue if the window should receive the input focus.
applicationGtk.Applicationr/w/enThe Gtk.Application for the window
attached-toGtk.Widgetr/w/c/enThe widget where the window is attached
decoratedboolr/w/enWhether the window should be decorated by the window manager
default-heightintr/w/enThe default height of the window, used when initially showing the window
default-widthintr/w/enThe default width of the window, used when initially showing the window
deletableboolr/w/enWhether the window frame should have a close button
destroy-with-parentboolr/w/enIf this window should be destroyed when the parent is destroyed
focus-on-mapboolr/w/enTrue if the window should receive the input focus when mapped.
focus-visibleboolr/w/enWhether focus rectangles are currently visible in this window
gravityGdk.Gravityr/w/enThe window gravity of the window
has-resize-gripboold/r/w/enSpecifies whether the window should have a resize grip deprecated
has-toplevel-focusboolrWhether the input focus is within this Gtk.Window
hide-titlebar-when-maximizedboolr/w/enIf this window’s titlebar should be hidden when the window is maximized
iconGdkPixbuf.Pixbufr/w/enIcon for this window
icon-namestrr/w/enName of the themed icon for this window
is-activeboolrWhether the toplevel is the current active window
is-maximizedboolrWhether the window is maximized
mnemonics-visibleboolr/w/enWhether mnemonics are currently visible in this window
modalboolr/w/enIf True, the window is modal (other windows are not usable while this one is up)
resizableboolr/w/enIf True, users can resize the window
resize-grip-visibleboold/rSpecifies whether the window’s resize grip is visible. deprecated
rolestrr/wUnique identifier for the window to be used when restoring a session
screenGdk.Screenr/w/enThe screen where this window will be displayed
skip-pager-hintboolr/w/enTrue if the window should not be in the pager.
skip-taskbar-hintboolr/w/enTrue if the window should not be in the task bar.
startup-idstrwUnique startup identifier for the window used by startup-notification
titlestrr/wThe title of the window
transient-forGtk.Windowr/w/c/enThe transient parent of the dialog
typeGtk.WindowTyper/w/coThe type of the window
type-hintGdk.WindowTypeHintr/w/enHint to help the desktop environment understand what kind of window this is and how to treat it.
urgency-hintboolr/w/enTrue if the window should be brought to the user’s attention.
window-positionGtk.WindowPositionr/w/enThe initial position of the window

Signals

NameShort Description
activate-defaultThe ::activate-default signal is a keybinding signal which gets emitted when the user activates the default widget of window.
activate-focusThe ::activate-focus signal is a keybinding signal which gets emitted when the user activates the currently focused widget of window.
enable-debuggingThe ::enable-debugging signal is a keybinding signal which gets emitted when the user enables or disables interactive debugging.
keys-changedThe ::keys-changed signal gets emitted when the set of accelerators or mnemonics that are associated with window changes.
set-focus

附录

Gtk.WindowType

class Gtk.WindowType
Bases: GObject.GEnum

设置窗口的类型,一般来说,窗口都是作为一个顶层窗口来呈现的。在某些特殊的情景(如在菜单中),才会作为一个气泡提示框等

TOPLEVEL = 0

顶层窗口

POPUP = 1

气泡提示框





代码下载地址:http://download.youkuaiyun.com/detail/a87b01c14/9594728

<think>我们有一个任务:在Linux系统中通过编程实现桌面显示计算机名和IP地址等信息。我们需要使用GUI工具来实现。根据用户需求,我们可以考虑使用多种方法,例如:1.使用系统托盘图标显示2.在桌面背景上绘制(需要支持动态更新)3.创建一个常驻窗口显示但是,由于用户要求通过编程实现,并且是桌面显示,我们可以选择创建一个简单的窗口应用程序,显示主机名和IP地址。我们可以使用GTK+或Qt这样的GUI工具包。这里以GTK+为例,因为它在Linux上比较通用,且使用C语言(也可以使用其他语言绑定)。步骤:1.获取主机名:使用`gethostname`系统调用。2.获取IP地址:这通常需要获取网络接口的信息,我们可以使用`getifaddrs`函数。3.使用GTK+创建一个窗口,并在窗口中显示这些信息。注意:由于IP地址可能有多个(多个接口),我们通常选择非回环接口的IPv4地址,或者选择特定接口(如eth0或wlan0)的地址。下面是一个使用C语言和GTK3的示例程序:首先,确保系统安装了GTK3开发包。在Ubuntu上,可以使用以下命令安装:sudoapt-getinstalllibgtk-3-dev然后,编写代码,例如`display_info.c`:```c#include<gtk/gtk.h>#include<stdio.h>#include<unistd.h>#include<ifaddrs.h>#include<netdb.h>#include<string.h>//获取本机IP地址(获取第一个非回环的IPv4地址)char*get_local_ip(){structifaddrs*ifaddr,*ifa;intfamily,s;char*host=NULL;if(getifaddrs(&ifaddr)==-1){perror("getifaddrs");returnNULL;}for(ifa=ifaddr;ifa!=NULL;ifa=ifa->ifa_next){if(ifa->ifa_addr==NULL)continue;family=ifa->ifa_addr->sa_family;//只考虑IPv4地址,并且跳过回环接口if(family==AF_INET&&strcmp(ifa->ifa_name,"lo")!=0){charhost[NI_MAXHOST];s=getnameinfo(ifa->ifa_addr,sizeof(structsockaddr_in),host,NI_MAXHOST,NULL,0,NI_NUMERICHOST);if(s!=0){printf("getnameinfo()failed:%s\n",gai_strerror(s));continue;}freeifaddrs(ifaddr);returnstrdup(host);}}freeifaddrs(ifaddr);returnNULL;//没有找到}intmain(intargc,char*argv[]){gtk_init(&argc,&argv);//获取主机名charhostname[256];gethostname(hostname,sizeof(hostname));//获取IP地址char*ip_address=get_local_ip();if(ip_address==NULL){ip_address="Notavailable";}//创建主窗口GtkWidget*window=gtk_window_new(GTK_WINDOW_TOPLEVEL);gtk_window_set_title(GTK_WINDOW(window),"SystemInfo");gtk_window_set_default_size(GTK_WINDOW(window),300,200);g_signal_connect(window,"destroy",G_CALLBACK(gtk_main_quit),NULL);//创建垂直布局容器GtkWidget*vbox=gtk_box_new(GTK_ORIENTATION_VERTICAL,5);gtk_container_add(GTK_CONTAINER(window),vbox);//创建标签显示主机名charhostname_text[300];snprintf(hostname_text,sizeof(hostname_text),"Hostname:%s",hostname);GtkWidget*label_hostname=gtk_label_new(hostname_text);gtk_box_pack_start(GTK_BOX(vbox),label_hostname,TRUE,TRUE,0);//创建标签显示IP地址charip_text[300];snprintf(ip_text,sizeof(ip_text),"IPAddress:%s",ip_address);GtkWidget*label_ip=gtk_label_new(ip_text);gtk_box_pack_start(GTK_BOX(vbox),label_ip,TRUE,TRUE,0);//显示所有控件gtk_widget_show_all(window);//启动GTK主循环gtk_main();//释放IP地址字符串if(ip_address!=NULL&&strcmp(ip_address,"Notavailable")!=0){free(ip_address);}return0;}```编译命令:gccdisplay_info.c-odisplay_info`pkg-config--cflags--libsgtk+-3.0`运行:./display_info这个程序会创建一个窗口,显示主机名和IP地址。注意:获取IP地址的函数`get_local_ip`在这里只返回找到的第一个非回环IPv4地址。如果需要显示所有IP地址,可以修改为将每个接口的IP地址都显示出来。另外,如果用户希望这个程序在登录时自动启动,可以将程序添加到启动应用程序中。除了使用C语言和GTK,还可以使用PythonPyGObject(GTK的Python绑定)来实现,这样代码更简洁。以下是使用Python的示例(需要安装PyGObject):sudoapt-getinstallpython3-gi代码(`display_info.py`):```pythonimportgigi.require_version('Gtk','3.0')fromgi.repositoryimportGtkimportsocketimportnetifacesasnidefget_local_ip():try:#获取所有网络接口interfaces=ni.interfaces()forifaceininterfaces:#跳过回环接口ififace=='lo':continueaddrs=ni.ifaddresses(iface)#获取IPv4地址ifni.AF_INETinaddrs:foraddr_infoinaddrs[ni.AF_INET]:ip=addr_info['addr']#返回第一个非回环的IPv4地址returnipreturn"Notavailable"except:return"Notavailable"classInfoWindow(Gtk.Window):def__init__(self):Gtk.Window.__init__(self,title="SystemInfo")self.set_default_size(300,200)vbox=Gtk.Box(orientation=Gtk.Orientation.VERTICAL,spacing=5)self.add(vbox)#获取主机名hostname=socket.gethostname()label_hostname=Gtk.Label(label=f"Hostname:{hostname}")vbox.pack_start(label_hostname,True,True,0)#获取IP地址ip_address=get_local_ip()label_ip=Gtk.Label(label=f"IPAddress:{ip_address}")vbox.pack_start(label_ip,True,True,0)win=InfoWindow()win.connect("destroy",Gtk.main_quit)win.show_all()Gtk.main()```运行:python3display_info.py这样,我们就实现了一个简单的GUI应用来显示主机名和IP地址。另外,用户也可以考虑使用系统提供的桌面小部件(widget)功能,比如在GNOME桌面上,可以使用Conky等工具来显示系统信息,但这不是编程实现,而是配置工具。如果用户希望直接在桌面背景上绘制,可能需要使用更底层的图形接口(如X11或Wayland),或者使用桌面环境提供的特定API,这通常比较复杂。因此,创建一个简单的窗口应用是较为直接的方法。参考引用:[^1]:关于Xserver和主机名的说明。[^2]:提到了SSH连接和VNC等远程桌面工具。[^3]:关于Ubuntu上SSH的安装和使用。[^4]:网络配置示例。根据这些引用,我们了解到主机名和IP地址在远程连接中的重要性,因此显示这些信息对于用户管理多台机器很有帮助。相关问题:1.如何获取Linux系统中的所有网络接口及其IP地址?2.GTK+编程中如何实现窗口的定时更新(例如动态更新IP地址)?3.如何将自定义的GTK应用添加到Linux启动项中?</think>### Linux编程实现桌面显示主机名和IP地址 在Linux系统中,可以通过多种GUI编程方式实现桌面显示主机名和IP地址信息。以下是三种主要实现方案: #### 方案1:GTK+桌面应用(C语言) ```c #include <gtk/gtk.h> #include <unistd.h> #include <ifaddrs.h> #include <netdb.h> // 获取IP地址函数 char* get_ip_address() { struct ifaddrs *ifaddr; getifaddrs(&ifaddr); for (struct ifaddrs *ifa = ifaddr; ifa; ifa = ifa->ifa_next) { if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) { char host[NI_MAXHOST]; getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host, NI_MAXHOST, NULL, 0, NI_NUMERICHOST); if (strcmp(ifa->ifa_name, "lo") != 0) { // 排除回环地址 freeifaddrs(ifaddr); return strdup(host); } } } freeifaddrs(ifaddr); return strdup("127.0.0.1"); } int main(int argc, char *argv[]) { gtk_init(&argc, &argv); // 创建主窗口 GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(GTK_WINDOW(window), "系统信息"); gtk_window_set_default_size(GTK_WINDOW(window), 300, 150); // 创建布局容器 GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 5); gtk_container_add(GTK_CONTAINER(window), vbox); // 获取主机名 char hostname[1024]; gethostname(hostname, sizeof(hostname)); // 创建信息标签 GtkWidget *host_label = gtk_label_new(NULL); gtk_label_set_markup(GTK_LABEL(host_label), "<span size='x-large' weight='bold'>主机名: </span>" "<span size='x-large'>%s</span>", hostname); GtkWidget *ip_label = gtk_label_new(NULL); gtk_label_set_markup(GTK_LABEL(ip_label), "<span size='x-large' weight='bold'>IP地址: </span>" "<span size='x-large'>%s</span>", get_ip_address()); // 添加标签到容器 gtk_box_pack_start(GTK_BOX(vbox), host_label, TRUE, TRUE, 10); gtk_box_pack_start(GTK_BOX(vbox), ip_label, TRUE, TRUE, 10); // 显示窗口 g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL); gtk_widget_show_all(window); gtk_main(); return 0; } ``` **编译运行:** ```bash gcc info_display.c -o info_display `pkg-config --cflags --libs gtk+-3.0` ./info_display ``` #### 方案2:Python + PyGObject ```python #!/usr/bin/env python3 import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk, GLib import socket import netifaces as ni class InfoWindow(Gtk.Window): def __init__(self): super().__init__(title="系统信息") self.set_default_size(300, 150) vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=10) self.add(vbox) # 获取主机名 hostname = socket.gethostname() host_label = Gtk.Label() host_label.set_markup(f"<b>主机名:</b> {hostname}") # 获取IP地址 ip = self.get_ip() ip_label = Gtk.Label() ip_label.set_markup(f"<b>IP地址:</b> {ip}") vbox.pack_start(host_label, True, True, 10) vbox.pack_start(ip_label, True, True, 10) def get_ip(self): interfaces = ni.interfaces() for interface in interfaces: if interface == 'lo': continue addresses = ni.ifaddresses(interface) if ni.AF_INET in addresses: return addresses[ni.AF_INET][0]['addr'] return "127.0.0.1" win = InfoWindow() win.connect("destroy", Gtk.main_quit) win.show_all() Gtk.main() ``` **运行:** ```bash pip3 install pygobject netifaces python3 info_display.py ``` #### 方案3:桌面小工具(Conky配置) 创建`~/.conkyrc`文件: ```conky conky.config = { alignment = 'top_right', background = true, border_width = 1, cpu_avg_samples = 2, default_color = 'white', default_outline_color = 'white', default_shade_color = 'white', draw_borders = false, draw_outline = false, draw_shades = false, use_xft = true, font = 'DejaVu Sans Mono:size=12', gap_x = 20, gap_y = 60, minimum_height = 5, minimum_width = 5, net_avg_samples = 2, no_buffers = true, out_to_console = false, out_to_stderr = false, extra_newline = false, own_window = true, own_window_class = 'Conky', own_window_type = 'desktop', own_window_transparent = true, stippled_borders = 0, update_interval = 1.0, uppercase = false, use_spacer = 'none', show_graph_scale = false, show_graph_range = false } conky.text = [[ ${color white}主机名: $alignr$color${nodename} ${color white}IP地址: $alignr$color${addr enp0s3} ]] ``` **启动Conky:** ```bash conky -c ~/.conkyrc ``` ### 技术说明 1. **主机名获取**:使用系统调用`gethostname()`或Python的`socket.gethostname()` 2. **IP地址获取**: - 遍历网络接口(排除回环接口lo) - 使用`getifaddrs()`(C)或`netifaces`(Python) - 选择第一个非127.0.0.1的IPv4地址 3. **显示方案选择**: - 独立应用:适合定制化需求 - Conky:轻量级系统监视器,低资源占用 - 桌面小部件:GNOME/KDE提供原生小部件API > 注意:IP地址获取需要根据实际网络接口名称调整(如eth0、enp0s3、wlan0等)[^2][^4]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

sanxiaochengyu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值