github.com/lxn/walk 写打开文件
来源:互联网 发布:cydia 数据库错误 编辑:程序博客网 时间:2024/05/21 07:56
使用 github.com/lxn/walk 写一个类似记事本打开文件的操作
===========================================================
editor.go
===========================================================
package mainimport ( "log" "editor/Abouter" //关于按钮相关 "editor/Opener" //打开按钮相关 "github.com/lxn/walk" . "github.com/lxn/walk/declarative")func main() { if _,err:= (MainWindow{ AssignTo: &Opener.Mw.MainWindow, Title: "NoteBook", MenuItems: []MenuItem{ Menu{ Text: "&File", Items: []MenuItem{ Action{ Text: "&NewFile", }, Action{ AssignTo: &openAction, Text: "&Open", //Image: "/img/open.png", OnTriggered: Opener.Mw.OpenAction_Triggered, }, Separator{}, Action{ Text: "Exit", OnTriggered: func() { Opener.Mw.Close() }, }, }, }, Menu{ Text: "&Help", Items: []MenuItem{ Action{ Text: "About", OnTriggered: Abouter.AboutAction_Triggered, }, }, }, }, MinSize: Size{320, 240}, Size: Size{800, 600}, Layout: VBox{MarginsZero: true}, Children: []Widget{ HSplitter{ Children: []Widget{ TextEdit{ AssignTo: &Opener.Te, HScroll: true, VScroll: true, }, }, }, }, }.Run()); err != nil { log.Fatal(err) }}
===========================================================
Opener.go
===========================================================
package Openerimport ( "encoding/csv" "log" "os" "github.com/axgle/mahonia" "github.com/golang/glog" "github.com/lxn/walk")var Te*walk.TextEditvar Mw=new(MyMainWindow)type MyMainWindow struct { *walk.MainWindow prevFilePath string}func (Mw *MyMainWindow)OpenAction_Triggered() { err := Te.SetText("")// 以便于清空文本框原有的内容 if err !=nil { glog.Error("SetText() clear content :", err) } if err:= Mw.OpenFile(); err!=nil { log.Print(err) } //max := te.MaxLength() //fmt.Println("max = ", max)}func GetCSV(csvPathstring) [][]string { file, err:= os.Open(csvPath) if err !=nil { glog.Error("Error when open file in GetCSV():", err) return nil } defer file.Close() decoder := mahonia.NewDecoder("gbk")// 把原来ANSI格式的文本文件里的字符,用gbk进行解码。 //r := csv.NewReader(file) r := csv.NewReader(decoder.NewReader(file))// 这样,最终返回的字符串就是utf-8了。(go只认utf8) res, err:= r.ReadAll() if err !=nil { glog.Error("Error when read csv in GetCSV():", err) return nil } return res}func (Mw *MyMainWindow)OpenFile()error { dlg :=new(walk.FileDialog) //var te *walk.TextEdit dlg.FilePath = Mw.prevFilePath dlg.Filter = "txt Files (*.txt)|*.txt" dlg.Title = "Select an file" if ok,err:= dlg.ShowOpen(Mw); err!=nil { return err } else if!ok { return nil } Mw.prevFilePath = dlg.FilePath content :=GetCSV(Mw.prevFilePath) for i,_:= range content { //te.AppendText(content[i][0]) AppendChangeText(content[i][0]) } return nil}func AppendChangeText(valuestring) { start, end:= Te.TextSelection() len := Te.TextLength() Te.SetTextSelection(len, len) Te.ReplaceSelectedText(value+"\n",true) Te.SetTextSelection(start, end)}
=============================================================
Abouter.go
=============================================================
package Abouterimport ( "editor/Opener" "github.com/lxn/walk")func AboutAction_Triggered() { walk.MsgBox(Opener.Mw, "About", "Walk Image Viewer Example", walk.MsgBoxIconInformation)}
执行效果图:
阅读全文
2 0
- github.com/lxn/walk 写打开文件
- 在goclipse中编写lxn/walk的win32程序ITT_ADD_TOOLS错误问题
- Go实战--使用golang开发Windows Gui桌面程序(lxn/walk)
- C#写文件时打开
- python打开os.walk()返回文件出现找不到文件的问题
- Vbs调用COM之打开/保存文件
- MFC调用com打开文件预览
- 不提示 打开文件,写文件。
- QTextStream写文件,文件打开时乱码
- 打开文件和写文件的操作
- C++写一个打开文件的百宝箱
- 以写模式打开一个文件
- python写文件打开后是乱码
- github.com项目文件浏览工具(octotree)
- 文件遍历os.walk方法
- Python os.walk文件遍历
- com 打开一个excel文件[open函数] 解读
- 写一篇博客记录个人这段时间的学习心得 我的github:https://github.com/w1232101/
- 双极性的信号,能不能直接输入到单电源供电的ADC?
- Android
- wpf中界面获取鼠标或键盘操作
- photoswipe.js插件
- Maven 详解
- github.com/lxn/walk 写打开文件
- Mock.js
- C++ Primer Plus(第六版)—— 第六章 分支语句和逻辑运算符 笔记和答案
- Android解决被软键盘遮挡的EditText问题
- Numpy 字符串处理
- 七日年化收益率怎么计算
- ajax使用get方法及jsonp传递参数实例
- 线程同步工具-CyclicBarrier
- Python3.x和Python2.x的区别