silk-GUI图形界面开发一个词典
了解使用的库
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图形界面开发一个词典的更多相关文章
- Java GUI图形界面开发工具
Applet 应用程序 一种可以在 Web 浏览器中执行的小程序,扩展了浏览器中的网页功能. 缺: 1.需要下载 Applet 及其相关文件 2.Applet 的功能是受限制的 优: 3.无需 ...
- Quartz(GUI)图形界面程序----Quartz Web
下载.设置和运行Quartz(GUI)图形界面程序----Quartz Web 一.获取Quartz Web程序(Quartz GUI).早期的 Quartz 框架开发者意识到一个 GUI 对于某类用 ...
- Java 图形界面开发--图文并茂建立学生管理系统
(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/50932501 冷血之心的博客) 图形用户界面(Graphics U ...
- JAVA 图形界面开发基础详解
与C的win32一样,JAVA也有自己的图形界面开发,将在此篇博客中对基础部分进行讲解. 1.Java提供的图形界面类有哪些? Java提供了两套图形界面 (1)AWT组建(基础) AWT组件是jdk ...
- Java Swing图形界面开发
本文转自xietansheng的CSDN博客内容,这是自己见过的最通俗易懂.最适合快速上手做Java GUI开发的教程了,这里整合一下作为自己以后复习的笔记: 原文地址:https://blog.cs ...
- JAVA与图形界面开发(Applet应用程序、AWT库、Swing)
Applet 1)简单说,Applet就是嵌入到网页中的小程序,Java代码. 2)编写Applet程序,要继承JApplet类,并根据自己需要覆写相关方法(init.start.stop.destr ...
- centOS7下安装GUI图形界面
1.如何在centOS7下安装GUI图形界面 当你安装centOS7服务器版本的时候,系统默认是不会安装GUI的图形界面程序,这个需要手动安装CentOS7 Gnome GUI包. 2.在系统下使用命 ...
- CentOS7安装GUI图形界面
本文转自centOS7下安装GUI图形界面,侵权删. 1. 在命令行下 输入下面的命令来安装Gnome包. # yum groupinstall "GNOME Desktop" & ...
- CentOS7 下安装GUI图形界面GNOME
在安装Gnome包之前,需要检查一下网络是否有网络(使用ping www.baidu.com) 一.先装X windows,-y表示参数同意所有软件安装操,当出现 Complete!说明这里安装成功了 ...
随机推荐
- NetCore框架WTM的分表分库实现
介绍 本期主角: ShardingCore 一款ef-core下高性能.轻量级针对分表分库读写分离的解决方案,具有零依赖.零学习成本.零业务代码入侵 WTM WalkingTec.Mvvm框架(简称W ...
- c++ 辗转相除(动图)
#include<iostream> #include<cstdio> #include<iomanip> #include<cstring> usin ...
- CSP 2021 总结
CSP 2021 总结 PJ 开题顺序:1342 应该先做 T2 ,导致我 T2 直接看错 T1.T3 T1 :直接推规律即可,考场的想法应该正确 T3 :好家伙直接 map 走起 T2 最崩溃的来了 ...
- css设置元素背景透明度的2种方式
更新记录 本文迁移自Panda666原博客,原发布时间:2021年7月9日. 设置元素的背景的透明度可以使用2种方式:方式1:opacity属性.方式2:使用rgba值.两种方式有一点差异,opaci ...
- iOS全埋点解决方案-APP和H5打通
前言 所谓的 APP 和 H5 打通,是指 H5 集成 JavaScript 数据采集 SDK 后,H5 触发的事件不直接同步给服务器,而是先发给 APP 端的数据采集 SDK,经过 APP 端数 ...
- 配置nginx多域名虚拟主机
1.先做域名映射,由于我们使用的是阿里云域名. 登录阿里云控制台-->域名与网站(万网)-->域名-->选择一个域名-->域名解析-->添加记录 配置静态资源下载转发: ...
- MOEAD实现、基于分解的多目标进化、 切比雪夫方法-(python完整代码)
确定某点附近的点 答:每个解对应的是一组权重,即子问题,红点附近的四个点,也就是它的邻居怎么确定呢?由权重来确定,算法初始化阶段就确定了每个权重对应的邻居,也就是每个子问题的邻居子问题.权重的邻居通过 ...
- C++库的随机数生成
C++库为我们提供了很多生成随机数的方法. 使用C的随机数生成法 先学过C语言,或者仅仅用C++做算法的人.对rand()是非常熟悉了.这个函数没有参数,生成0到RAND_MAX的随机数(RAND_M ...
- 虚拟机使用docker 外部机器无法访问端口问题
1,排查防火墙firewall-cmd --state 如果输出的是"not running"则FirewallD没有在运行,且所有的防护策略都没有启动,那么可以排除防火墙阻断连接 ...
- React key究竟有什么作用?深入源码不背概念,五个问题刷新你对于key的认知
壹 ❀ 引 我在[react]什么是fiber?fiber解决了什么问题?从源码角度深入了解fiber运行机制与diff执行一文中介绍了react对于fiber处理的协调与提交两个阶段,而在介绍协调时 ...