了解使用的库

Silk内置了一些GUI类库供使用者开发MacOS上的图形界面程序,只需引用gui.si即可

准备

首先要知道app需要什么功能,这里我要的是查询单词,可以听语音,还可以存储生词!

那么就要这两个库:"gui.si"和"sqlite.si"

GUI负责界面设计,sqlite负责生词存储(当然你也可以改为CMySql,链接服务器数据库)

上代码

#include "gui.si"
#include "sqlite.si" _word=null;
_textview=null;
_checkbox=null; class CWordDB(filename)
{
curdir=_fun("curdir");
native=CNative();
native.GrantPermission(curdir);
self.filename=curdir+filename; func createDB()
{
bCreated=false;
db=CSqlite();
if(db.Open(self.filename))
{
sql="select count(*) from wordlist";
result=db.Query(sql);
if(!result)
{
sql=sprintf("create table wordlist (id integer PRIMARY KEY,word TEXT,description TEXT,reserved TEXT)");
db.Exec(sql);
sql=sprintf("create INDEX index_word on wordlist (word) ");
db.Exec(sql); bCreated=true;
}
db.Query_Free(result); }
db.Close(); return bCreated;
}
func searchDB(word)
{
results=[]; if(self.createDB())
return results; db=CSqlite();
if(db.Open(self.filename))
{
if(word=="")
sql=sprintf("select * from wordlist");
else
sql=sprintf("select * from wordlist where word = '%s'",word);
result=db.Query(sql);
if(result)
{
count=db.RecordNum(result);
for(i=1;i<=count;i++)
{
item={};
item["word"]=db.GetByFieldName(result,i,"word");
item["description"]=db.GetByFieldName(result,i,"description");
results.append(item);
}
}
db.Query_Free(result);
} db.Close();
return results;
}
func insertDB(word,description)
{
self.createDB(); id=0;
db=CSqlite();
if(db.Open(self.filename))
{
sql=sprintf("INSERT INTO wordlist (word,description,reserved) VALUES ('%s','%s','')",word,description);
result=db.Exec(sql);
if(result)
{
sql="select max(id) from wordlist";
result=db.Query(sql);
if(result)
{
id=_int(db.GetByFieldNo(result,1,0));
}
db.Query_Free(result);
}
}
db.Close(); return id;
}
func deleteDB(word)
{
self.createDB(); result=null;
db=CSqlite();
if(db.Open(self.filename))
{
sql=sprintf("delete from wordlist where word='%s' ",word);
result=db.Exec(sql);
db.Close();
} return result; }
func updateDB(word,description)
{
self.createDB(); result=null;
db=CSqlite();
if(db.Open(self.filename))
{
sql=sprintf("select * from wordlist where word='%s' ",word);
res=db.Query(sql);
if(res)
{
count=db.RecordNum(res);
if(count>0)
{
sql=sprintf("Update wordlist set description='%s' where word='%s'",description,word);
result=db.Exec(sql);
}
}
db.Query_Free(res);
db.Close();
} return result; }
} func get_ok(response)
{
error=response.find("error");
if(error)
{
print(error);
return;
} html=response["body"];
tag1="<meta name=\"description\" content=\"";
tag2="\" />";
pos1=html.find(tag1);
if(pos1>=0)
{
pos1+=_len(tag1);
pos2=html.find(tag2,pos1);
description=html.substr(pos1,pos2-pos1);
prefix=sprintf("必应词典为您提供%s的释义,",_word);
description=description.replace(prefix,"");
_textview.SetText(description); is_check=_checkbox.GetStatus();
if(is_check && _word)
{
word=_fun("url_escape",_word);
description=_fun("url_escape",description);
db=CWordDB("wordlist.db");
id=db.insertDB(word,description);
if(id>0)
print(_word,"自动加入成功");
}
}
} func search_word(word)
{
global _word;
_word=word; header = {"content-type":"application/x-www-form-urlencoded",
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36",
"Connection":"keep-alive",
"Cache-Control":"max-age=0",
"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Accept-Encoding":"gzip, deflate",
"Accept-Language":"zh-CN,zh;q=0.9",
"Upgrade-Insecure-Requests":"1"
};
url=sprintf("http://cn.bing.com/dict/search?q=%s",word); request=CHttpRequest();
request.HttpGet(url,get_ok,null,header);
_textview.SetText("正在查询...");
} func download_ok(response)
{
error=response.find("error");
if(error)
{
print(error);
return;
} filePath=response["file_path"];
audio=CAudioPlay();
audio.PlayFile(filePath);
}
func get_voice(word)
{
header = {"content-type":"application/x-www-form-urlencoded",
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36",
"Connection":"keep-alive",
"Cache-Control":"max-age=0",
"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
"Accept-Encoding":"gzip, deflate",
"Accept-Language":"zh-CN,zh;q=0.9",
"Upgrade-Insecure-Requests":"1"
};
url=sprintf("https://api.vvhan.com/api/song?txt=%s",word); request=CHttpRequest();
save_path=_fun("curdir")+"temp.wav";
request.HttpGet(url,download_ok,save_path,header);
} func search_click(handle,param)
{
text1=param["text1"];
word=text1.GetText(); search_word(word);
}
func voice_click(handle,param)
{
text1=param["text1"];
word=text1.GetText(); get_voice(word);
} func save_click(handle,param)
{
text1=param["text1"];
word=text1.GetText();
text2=param["text2"];
description=text2.GetText();
word=_fun("url_escape",word);
description=_fun("url_escape",description); db=CWordDB("wordlist.db");
result=db.updateDB(word,description);
if(result)
{
table=param["table"];
row=table.GetCurSel();
data=table.GetData(row);
data["description"]=text2.GetText();
table.Refresh(); msgbox=CMessageBox();
msgbox.ShowMessage("提示","保存成功!");
} }
func add_click(handle,param)
{
text1=param["text1"];
word=text1.GetText();
description=_textview.GetText();
if(word)
{
word=_fun("url_escape",word);
description=_fun("url_escape",description);
db=CWordDB("wordlist.db");
if(db.searchDB(word))
{
msgbox=CMessageBox();
msgbox.ShowMessage("提示","该单词已经存在!");
return;
}
id=db.insertDB(word,description);
if(id>0)
{
msgbox=CMessageBox();
msgbox.ShowMessage("提示","加入成功!");
}
}
}
func edit_click(handle,param)
{
if(!param)
return; table=param["table"];
row=table.GetCurSel();
if(row<0)
{
msgbox=CMessageBox();
msgbox.ShowMessage("提示","请选择需要编辑的单词。");
return;
}
data=table.GetData(row); win=CWindow();
w=300;
h=300;
win.CreateWindow("编辑单词",w,h);
label=win.AddLabel("单词:");
text1=win.AddTextField(data["word"]);
text1.SetEditable(false);
rect=text1.GetRect();
rect.h=20;
rect.y=rect.y-rect.h-5;
win.AddLabel("解释:",rect);
text2=win.AddTextView(data["description"]); rect=text2.GetRect();
rect.h=25;
rect.y=rect.y-rect.h-10;
param2={"text1":text1, "text2":text2, "table":table};
button=win.AddButton("保存",rect,save_click,param2);
button.RoundedStyle(true); win.MainLoop();
}
func delete_click(handle,param)
{
if(!param)
return;
msgbox=CMessageBox();
ret=msgbox.ShowMessage("提示","你真的想删除这个单词吗?","Yes","No");
if(!ret)
return; table=param["table"];
row=table.GetCurSel();
data=table.GetData(row);
word=_fun("url_escape",data["word"]); db=CWordDB("wordlist.db");
result=db.deleteDB(word);
if(result)
{
table.Delete(row);
table.Refresh();
}
} func tableViewSelectionChanged(handle,parent,index,data,param)
{
}
func tableViewDoubleClicked(handle,parent,index,data,param)
{
table=CTableView(handle,parent);
param["table"]=table;
edit_click(null,param);
}
func wordlist_click(handle,param)
{
if(!param)
return; win=CWindow();
w=400;
h=400;
win.CreateWindow("我的生词库",w,h); db=CWordDB("wordlist.db");
res=db.searchDB(""); rect=CRect(10,10,w-20,h-40-20);
param={"win":win};
table=win.AddTableView(rect,tableViewSelectionChanged,tableViewDoubleClicked,param);
table.AddColumn("单词","word");
table.AddColumn("解释","description",400);
for(i=0;i<res.size();i++)
{
item={};
item["word"]=_fun("url_unescape",res[i]["word"]);
item["description"]=_fun("url_unescape",res[i]["description"]); table.AddData(item);
}
table.Refresh(); param={"win":win,"table":table};
w2=100;
h2=25;
rect=CRect(10,h-40,w2,h2);
button=win.AddButton("编辑",rect,edit_click,param);
button.RoundedStyle(true);
rect=CRect(10+w2+5,h-40,w2,h2);
button=win.AddButton("删除",rect,delete_click,param);
button.RoundedStyle(true); win.MainLoop();
} func about(handle,param)
{
box=CMessageBox();
box.ShowMessage("感谢","感谢bing、韩小韩api的支持!");
box.ShowMessage("作者","作者:吴定一");
} func dict_window()
{
global _textview, _checkbox; win=CWindow();
w=450;
h=420;
win.CreateWindow("jwy-dictionary",w,h);
win.LiveOutput(true); label=win.AddLabel("输入要查询的单词:");
rect=label.GetRect();
rect.x=10;
label.SetRect(rect);
rect2=CRect(rect.x,rect.y-30,300,25);
text1=win.AddTextField("",rect2);
rect=text1.GetRect();
rect.h=20;
rect.y=rect.y-rect.h-5;
label2=win.AddLabel("解释:",rect);
rect2=CRect(rect.x,rect.y-250,300,250);
_textview=win.AddTextView("",rect2); param={"text1":text1};
rect=text1.GetRect();
rect2=CRect(rect.x+rect.w+5,rect.y,100,rect.h);
button=win.AddButton("查询",rect2,search_click,param);
button.RoundedStyle(true); y=label2.GetRect().y;
rect2=CRect(rect.x+rect.w+5,y-rect.h,100,rect.h);
button=win.AddButton("读音",rect2,voice_click,param);
button.RoundedStyle(true);
rect2=CRect(rect.x+rect.w+5,y-(rect.h+15)*2,100,rect.h);
button=win.AddButton("加入生词库",rect2,add_click,param);
button.RoundedStyle(true);
rect2=CRect(rect.x+rect.w+5,y-(rect.h+15)*3,100,rect.h);
button=win.AddButton("我的生词库",rect2,wordlist_click,param);
button.RoundedStyle(true);
rect2=CRect(rect.x+rect.w+5,y-(rect.h+15)*4,120,rect.h);
_checkbox=win.AddButton("自动加入生词库",rect2);
_checkbox.SetCheckBox(true,1);
rect2=CRect(rect.x+rect.w+5,y-(rect.h+15)*5,100,rect.h);
button=win.AddButton("关于",rect2,about,param);
button.RoundedStyle(true); rect=_textview.GetRect();
rect2=CRect(rect.x,rect.y-45,250,40);
rect2.y=rect2.y-25;
rect2.h=25;
rect2.w=400;
link=win.AddTextField("app基于silk开发,更多信息点击关于查看",rect2); win.MainLoop();
} main()
{
dict_window();
}

ok,接下来看看效果:





解释:

这里用了一个bing词典以及韩小韩的文字转语音的api,这个api有个bug,不能空格,我还在研究这个语言的转译,等我回了再补充,如果你会可以发个评论告诉我!

其实你只要看个官方文档就大差不差了:https://silklang.org/chinese/tutorial_db.html


相关信息

源码有需要的可以下载:https://wudingyi1020.lanzouv.com/i9ImM09pz5af,对了windows和linux不保证你的库是自带的,没有请按照文档进行配置,否则可能无法使用!


下载相关问题公告蓝奏云如果遇到链接无法访问,可以把链接中的https://pan.lanzou替换成https://pan.lanzoux,给您造成的不便深感歉意。

silk-GUI图形界面开发一个词典的更多相关文章

  1. Java GUI图形界面开发工具

    Applet 应用程序     一种可以在 Web 浏览器中执行的小程序,扩展了浏览器中的网页功能. 缺: 1.需要下载 Applet 及其相关文件 2.Applet 的功能是受限制的 优: 3.无需 ...

  2. Quartz(GUI)图形界面程序----Quartz Web

    下载.设置和运行Quartz(GUI)图形界面程序----Quartz Web 一.获取Quartz Web程序(Quartz GUI).早期的 Quartz 框架开发者意识到一个 GUI 对于某类用 ...

  3. Java 图形界面开发--图文并茂建立学生管理系统

    (尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/50932501 冷血之心的博客) 图形用户界面(Graphics U ...

  4. JAVA 图形界面开发基础详解

    与C的win32一样,JAVA也有自己的图形界面开发,将在此篇博客中对基础部分进行讲解. 1.Java提供的图形界面类有哪些? Java提供了两套图形界面 (1)AWT组建(基础) AWT组件是jdk ...

  5. Java Swing图形界面开发

    本文转自xietansheng的CSDN博客内容,这是自己见过的最通俗易懂.最适合快速上手做Java GUI开发的教程了,这里整合一下作为自己以后复习的笔记: 原文地址:https://blog.cs ...

  6. JAVA与图形界面开发(Applet应用程序、AWT库、Swing)

    Applet 1)简单说,Applet就是嵌入到网页中的小程序,Java代码. 2)编写Applet程序,要继承JApplet类,并根据自己需要覆写相关方法(init.start.stop.destr ...

  7. centOS7下安装GUI图形界面

    1.如何在centOS7下安装GUI图形界面 当你安装centOS7服务器版本的时候,系统默认是不会安装GUI的图形界面程序,这个需要手动安装CentOS7 Gnome GUI包. 2.在系统下使用命 ...

  8. CentOS7安装GUI图形界面

    本文转自centOS7下安装GUI图形界面,侵权删. 1. 在命令行下 输入下面的命令来安装Gnome包. # yum groupinstall "GNOME Desktop" & ...

  9. CentOS7 下安装GUI图形界面GNOME

    在安装Gnome包之前,需要检查一下网络是否有网络(使用ping www.baidu.com) 一.先装X windows,-y表示参数同意所有软件安装操,当出现 Complete!说明这里安装成功了 ...

随机推荐

  1. 编程式导航路由跳转到当前路由(参数不变), 多次执行会抛出NavigationDuplicated的警告错误?

    注意:编程式导航(push|replace)才会有这种情况的异常,声明式导航是没有这种问题,因为声明式导航内部已经解决这种问题. 这种异常,对于程序没有任何影响的. 为什么会出现这种现象: 由于vue ...

  2. ESXI系列问题整理以及记录——使用Windows PowerShell中的SSH功能连接ESXI控制台

    首先进入ESXI管理页面,开启ESXI的SSH功能 接下来到位于同一局域网的Win主机上开启Powershell,如果ESXI主机的IP地址为192.168.1.77,则在Powershell中输入: ...

  3. 2.shell脚本99乘法表

    shell脚本99乘法表 [root@localhost data]# vim cf.sh

  4. 2022年最强八股文《码出八股文-斩出offer线》

    宝剑锋从磨砺出,梅花香自苦寒来,大家好,我是小码哥 整理好的八股文终于完成了,希望看完对大家面试有所收获! 目录: 基础篇 javaOOP面试题 java集合/泛型面试题 java异常面试题 java ...

  5. Apache ShardingSphere 5.1.2 发布|全新驱动 API + 云原生部署,打造高性能数据网关

    在 Apache ShardingSphere 5.1.1 发布后,ShardingSphere 合并了来自全球的团队或个人的累计 1028 个 PR,为大家带来 5.1.2 新版本.该版本在功能.性 ...

  6. 从Vue源码中我学到了几点精妙方法

    话不多说,赶快试试这几个精妙方法吧!在工作中肯定会用得到. 立即执行函数 页面加载完成后只执行一次的设置函数. (function (a, b) { console.log(a, b); // 1,2 ...

  7. zabbix-5.0自动发现

    1. 安装zabbix5.0 rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.no ...

  8. java基础知识点梳理

    前言 在别人追问我以下几个问题,自己在问题回答上不够全面和准确,对此自己把专门针对这几个问题进行总结! java相关问题 1.Java中构造方法跟普通方法的区别? 构造方法与普通方法的调用时机不同. ...

  9. Java中将对象或者集合对象转换成json字符串

    1.对象和字符串相互转换 2.集合对象和字符串相互转换

  10. appium简单使用

    App 测试通常会用到的工具 adb :Android 的控制工具,用于获取Android的各种数据和控制 Appium Desktop:内嵌了Appium Server和Inspector的综合工具 ...