首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用于wifi热点的GUI切换键(开/关)

用于wifi热点的GUI切换键(开/关)
EN

Ask Ubuntu用户
提问于 2014-04-25 13:22:41
回答 1查看 727关注 0票数 1

我希望为运行一组命令创建一个GUI小部件,如on/off开关。

例如,使用ap-hotspot start/stop打开和关闭wifi热点。

有人帮我..。

EN

回答 1

Ask Ubuntu用户

回答已采纳

发布于 2014-04-27 19:42:23

您可以实践一种通用的方法来为设置、连接等创建切换脚本/功能。此外,您可能能够在不同的情况下重用大部分代码。然而,一个简单的“全在一体”的解决方案,适用于不同的情况,没有任何知识,或对编码的感觉,是很难给出。您取决于脚本必须切换的两种状态的性质、它们对应的命令以及您可以使用(或不使用)检查当前状态的方法。

尽管如此,考虑到您的切换热点示例,下面是三个准备使用安装程序的版本,从相对简单到更复杂。前两种是最“普遍”的,第三种仅用于统一。

切换脚本的一般“解剖”如下:

代码语言:javascript
复制
> check what is the current_status
> if current_status = A:
      (switch icon to icon_b)
      run command to change state to B
      (check if toggle command was successful, if not > switch icon back)
> else:
      (switch launcher icon to icon_a)
      run command to change to change to A
      (check if toggle command was successful, if not > switch icon back)

切换设置;三个示例

  1. 在桌面上切换单个启动程序(桌面文件)。
  2. 同上,但也切换图标,以显示当前状态。
  3. 从中的图标切换,用切换图标显示当前状态

备注:

  • 因为您将被要求输入密码,所以如果gksu不在您的系统上,则需要安装它。例3不应在12.04或更早版本上使用(按原样)。
  • 请记住,请求管理员许可的脚本是潜在的安全风险。如果您怀疑是谁在您的计算机上做什么,请将其存储在安全目录中。

1.在桌面

上切换单个启动程序(桌面文件)

最简单的一个:通过桌面上的(固定)启动器进行切换。

使用说明:

图标:

从下面(右击>安全as)下载其中的一个图标,将其作为toggle_icon.png保存在您选择的位置。

剧本:

复制下面的文本,将其粘贴到一个空文件中,并将其保存为您选择的位置的hotspot.py。

代码语言:javascript
复制
#!/usr/bin/python3

import subprocess

# identifying string to look for when "pstree" is run
running_id = "ap-hotspot"

def check_ifrunning(): 
    # check if hotspot is up or down
    get_pstreeinfo = subprocess.Popen(["pstree"], stdout=subprocess.PIPE)
    output = (get_pstreeinfo.communicate()[0].decode("utf-8"))
    if running_id in output:
        return "running"
    else:
        return "not_running"

def toggle_connection():
    runcheck = check_ifrunning()  
    if runcheck == "not_running":
    # command to start hotspot (spaces replaced by ",")
        subprocess.Popen(["gksu", "ap-hotspot", "start"])
    else:
        # idem, stop hotspot
        subprocess.Popen(["gksu", "ap-hotspot", "stop"])

toggle_connection()

创建桌面文件:

复制下面的文本,粘贴到一个空的文本文件中。将正确的路径添加到Exec=行中的脚本中,在Icon=行中添加正确的路径,并将其作为hotspot_toggle.desktop安全地保存到桌面上。使其可执行,您的设置应该可以工作。

代码语言:javascript
复制
[Desktop Entry]
Name=Hotspot toggle
Comment=Hotspot toggle
Categories=Accessories
Exec=python3 /path/to/script/hotspot.py
Icon=/path/to/icon/toggle_icon.png
Terminal=false
Type=Application
StartupNotify=true

2.在桌面上切换单个启动程序(桌面文件),并使用图标更改效果

这是第一个示例的增强版本:图标将更改为桌面上的toggle_officon.png / toggle_onicon.png,这取决于hotspot是否打开或关闭。

使用说明:

图标:

从第一个示例下载两个图标,将它们作为

代码语言:javascript
复制
toggle_officon.png (the grey one)
toggle_onicon.png (the green one) 

在你选择的地方。

剧本:

复制下面的文本,将其粘贴到一个空文件中,并将其保存为您选择的位置的hotspot.py。将正确的路径添加到以path_todtfile = (桌面文件的路径,请参阅下文)、icon_offpath = (到toggle_officon.png的路径)和icon_onpath = (到toggle_onicon.png的路径)开头的行。注意:桌面文件的“真实”名称是保存它时的命名方式。您在接口中看到的名称在桌面文件的Name=行中定义。

代码语言:javascript
复制
#!/usr/bin/python3

import subprocess
import time

wait = 10

# identifying difference on pstree command on / off
running_id = "ap-hotspot"

# pathto_desktop file
path_todtfile = "/path/to/desktop_file/toggle.desktop"
# paths to icons
icon_offpath = "/path/to/toggle_off_icon/toggle_officon.png"
icon_onpath = "/path/to/toggle_on_icon/toggle_onicon.png"

def set_icon(set_mode, state):
    if state == "running":
        iconset = [icon_onpath, icon_offpath]
    else:
        iconset = [icon_offpath, icon_onpath]
    if set_mode == "set_current":
        appropriate_iconpath = iconset[0]
    else:
        appropriate_iconpath = iconset[1]
    with open(path_todtfile, "r") as editicon:
        editicon = editicon.readlines()
    line_toedit = [editicon.index(line) for line in editicon if\
                   line.startswith("Icon=")][0]
    if not editicon[line_toedit] == "Icon="+appropriate_iconpath+"\n":
        editicon[line_toedit] = "Icon="+appropriate_iconpath+"\n"
        with open(path_todtfile, "wt") as edited_icon:
            for line in editicon:
                edited_icon.write(line)
    else:
        pass

def check_ifrunning():
    # check if hotspot is up or down
    get_pstreeinfo = subprocess.Popen(["pstree"], stdout=subprocess.PIPE)
    output = (get_pstreeinfo.communicate()[0].decode("utf-8"))
    if running_id in output:
        return "running"
    else:
        return "not_running"

def toggle_connection():
    runcheck = check_ifrunning()
    set_icon("set_alter", runcheck)
    if runcheck == "not_running":
        subprocess.call(["gksu", "ap-hotspot", "start"])
    else:
        subprocess.call(["gksu", "ap-hotspot", "stop"])
    time.sleep(wait)
    runcheck = check_ifrunning()
    set_icon("set_current", runcheck)

toggle_connection()

桌面文件:

创建类似于示例1的桌面文件。将正确的路径添加到Exec=行的脚本中,将路径添加到Icon=中的两个图标之一(在第一次使用时将被纠正),并将其作为toggle.desktop安全地保存到桌面上。使其可执行,您的设置应该可以工作。

3.从Unity中的图标切换,用切换图标显示当前状态

倒/跑

(此示例不应用于12.04或更早版本。)

图标:

从第一个示例下载两个图标,将它们作为

代码语言:javascript
复制
toggle_officon.png (the grey one)
toggle_onicon.png (the green one) 

在你选择的地方。

剧本:

复制下面的文本。将其粘贴到一个空文件中,将其保存为hotspot.py,位于适合您的位置。

代码语言:javascript
复制
#!/usr/bin/python3

import subprocess
import getpass
import time

# time to wait, to check if hotspot was established (set correct icon)
wait = 10
# identifying difference on pstree command
running_id = "ap-hotspot"
# location of the launcher restore script
backup_copy = "/home/"+getpass.getuser()+"/.restore_currentlauncher.sh"
# name of the desktop file if hotspot is down
mention_ifdown = 'application://hotspot_off.desktop'
# name of the desktop file if hotspot is running
mention_ifup = 'application://hotspot_on.desktop'

def check_ifrunning():
    # check if hotspot is up or down
    get_pstreeinfo = subprocess.Popen(["pstree"], stdout=subprocess.PIPE)
    output = (get_pstreeinfo.communicate()[0].decode("utf-8"))
    if running_id in output:
        return "running"
    else:
        return "not_running"

def read_currentlauncher():
    # read the current launcher contents
    get_launcheritems = subprocess.Popen([
        "gsettings", "get", "com.canonical.Unity.Launcher", "favorites"
        ], stdout=subprocess.PIPE)
    return eval((get_launcheritems.communicate()[0].decode("utf-8")))

def set_current_launcher(current_launcher):
    # before editing the launcher, create restore script
    backup_data = read_currentlauncher()
    with open(backup_copy, "wt") as create_backup:
        create_backup.write(
            "#!/bin/sh\n\n"\
            "gsettings set com.canonical.Unity.Launcher favorites "+\
            '"'+str(backup_data)+'"'
            )
    # preparing subprocess command string
    current_launcher = str(current_launcher).replace(", ", ",")
    subprocess.Popen([
        "gsettings", "set", "com.canonical.Unity.Launcher", "favorites",
        current_launcher,
        ]) 

def set_icon(change_mode):
    # defines the appropriate icon in the launcher
    state = check_ifrunning()
    if state == "running":
        if change_mode == "set_current":
            iconset = [mention_ifup, mention_ifdown]
        else:
            iconset = [mention_ifdown, mention_ifup]
    elif state == "not_running":
        if change_mode == "set_current":
            iconset = [mention_ifdown, mention_ifup]
        else:
            iconset = [mention_ifup, mention_ifdown]
    # set the defined icon
    current_launcher = read_currentlauncher()
    if iconset[0] in current_launcher:
        pass
    else:
        index = current_launcher.index(iconset[1])
        current_launcher.pop(index)
        set_current_launcher(current_launcher)
        time.sleep(1)
        current_launcher.insert(index, iconset[0])
        set_current_launcher(current_launcher)

def toggle_connection():
    set_icon("set_alter")
    runcheck = check_ifrunning()
    if runcheck == "not_running":
        subprocess.call(["gksu", "ap-hotspot", "start"])
    else:
        subprocess.call(["gksu", "ap-hotspot", "stop"])
    time.sleep(wait)
    set_icon("set_current")

toggle_connection()

两个桌面文件,将在启动程序中切换:

以下是您需要的两个桌面文件。打开一个空文本文件,粘贴下面的代码(在单独的文件中),将路径替换为指向上面所保存的图标(S)的实际路径,以及脚本的路径,并将它们保存在~/.local/share/applications中,作为hotspot_off.desktophotspot_on.desktop

hotspot_off.desktop:

代码语言:javascript
复制
[Desktop Entry]
Name=Hotspot off
Exec=python3 /path/to/script/hotspot.py
Icon=/path/to/toggle_officon/toggle_officon.png
Terminal=false
Type=Application
NoDisplay=true

hotspot_on.desktop:

代码语言:javascript
复制
[Desktop Entry]
Name=Hotspot on
Exec=python3 /path/to/script/hotspot.py
Icon=/path/to/toggle_officon/toggle_onicon.png
Terminal=false
Type=Application
NoDisplay=true

最后,将其中一个桌面文件拖到启动程序上。不要担心,如果你选择了正确的或没有,它会得到纠正在第一次运行。

票数 5
EN
页面原文内容由Ask Ubuntu提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://askubuntu.com/questions/455201

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档