首页 > 其他分享 >silk-GUI图形界面开发一个词典

silk-GUI图形界面开发一个词典

时间:2022-08-21 20:13:09浏览次数:62  
标签:word description 图形界面 db param text1 func 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的支持!

标签:word,description,图形界面,db,param,text1,func,silk,GUI
From: https://www.cnblogs.com/wdysblog/p/16610713.html

相关文章