{"id":42854,"date":"2024-12-01T10:57:56","date_gmt":"2024-12-01T02:57:56","guid":{"rendered":"https:\/\/fwq.ai\/blog\/42854\/"},"modified":"2024-12-01T10:57:56","modified_gmt":"2024-12-01T02:57:56","slug":"fyne-%e5%88%97%e8%a1%a8%e5%b0%8f%e9%83%a8%e4%bb%b6%e6%97%a0%e6%b3%95%e6%ad%a3%e7%a1%ae%e6%98%be%e7%a4%ba%e9%a1%b9%e7%9b%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/42854\/","title":{"rendered":"Fyne \u5217\u8868\u5c0f\u90e8\u4ef6\u65e0\u6cd5\u6b63\u786e\u663e\u793a\u9879\u76ee"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>Fyne \u5217\u8868\u5c0f\u90e8\u4ef6\u65e0\u6cd5\u6b63\u786e\u663e\u793a\u9879\u76ee<\/span><\/p>\n<h1>Fyne \u5217\u8868\u5c0f\u90e8\u4ef6\u65e0\u6cd5\u6b63\u786e\u663e\u793a\u9879\u76ee<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-26 18:30:37<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aFyne \u5217\u8868\u5c0f\u90e8\u4ef6\u65e0\u6cd5\u6b63\u786e\u663e\u793a\u9879\u76ee\u300b<\/span>\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span><\/span>\uff0c\u5982\u679c\u4f60\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u5728\u5e26\u6709\u6570\u636e\u7684\u5217\u8868\u5c0f\u90e8\u4ef6\u4e2d\u663e\u793a\u5b57\u6bcd\u8868\u3002\u6211\u4f7f\u7528\u81ea\u5b9a\u4e49\u53ef\u70b9\u51fb\u6807\u7b7e\u4f5c\u4e3a\u8981\u5728\u5217\u8868\u4e2d\u663e\u793a\u7684\u5c0f\u90e8\u4ef6\u3002<\/p>\n<p> \u7531\u4e8e\u67d0\u79cd\u539f\u56e0\uff0c\u5f53\u5c0f\u90e8\u4ef6\u52a0\u8f7d\u65f6\uff0c\u4e00\u5207\u90fd\u663e\u793a\u6b63\u5e38\u3002\u4f46\u662f\u5f53\u6211\u5f00\u59cb\u6eda\u52a8\u65f6\uff0c\u5b57\u6bcd\u5f00\u59cb\u4ee5\u5b8c\u5168\u968f\u673a\u7684\u987a\u5e8f\u663e\u793a\uff0c\u6211\u4e0d\u660e\u767d\u4e3a\u4ec0\u4e48\u3002<\/p>\n<p> \u8fd9\u662f\u91cd\u73b0\u8be5\u9519\u8bef\u7684\u5b8c\u6574\u5de5\u4f5c\u4ee3\u7801\u3002<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n\n    \"fyne.io\/fyne\/v2\"\n    \"fyne.io\/fyne\/v2\/app\"\n    \"fyne.io\/fyne\/v2\/data\/binding\"\n    \"fyne.io\/fyne\/v2\/widget\"\n)\n\nfunc makeAlphabet() []string {\n    var alphabet []string\n    for ch := 'A'; ch &lt;= 'Z'; ch++ {\n        alphabet = append(alphabet, string(ch))\n    }\n    return alphabet\n}\n\ntype TapLabel struct {\n    *widget.Label \/\/composition\n\n    \/\/function pointers to set to get events\n    OnTapped func(string)\n}\n\nfunc (mc *TapLabel) Tapped(pe *fyne.PointEvent) {\n    if mc.OnTapped != nil {\n        mc.OnTapped(mc.Text)\n    }\n}\n\nfunc NewTapLabel(text string, tappedLeft func(string)) *TapLabel {\n    return &amp;TapLabel{widget.NewLabel(text), tappedLeft}\n}\n\nfunc alphabetToBrands(letter string) {\n    fmt.Println(letter)\n}\n\nfunc main() {\n    app := app.New()\n    window := app.NewWindow(\"tac_hub\")\n    window.Resize(fyne.NewSize(200,200))\n\n    rawData := makeAlphabet()\n    data := binding.BindStringList(&amp;rawData)\n    list := widget.NewListWithData(\n        data,\n        func() fyne.CanvasObject {\n            return NewTapLabel(\"template\", alphabetToBrands)\n        },\n        func(i binding.DataItem, o fyne.CanvasObject) {\n            o.(*TapLabel).Bind(i.(binding.String))\n        },\n    )\n\n    window.SetContent(list)\n    window.ShowAndRun()\n}<\/pre>\n<p>\u70b9\u51fb\u64cd\u4f5c\u6b63\u5e38\u5de5\u4f5c\uff0c\u5e76\u7ed9\u6211\u6b63\u786e\u7684\u5b57\u6bcd\uff08\u4e0d\u662f\u663e\u793a\u7684\u5b57\u6bcd\uff0c\u800c\u662f\u5e94\u8be5\u663e\u793a\u7684\u5b57\u6bcd\uff09\u3002<\/p>\n<p> \u6211\u731c\u6211\u4e00\u5b9a\u662f\u5728\u67d0\u4e2a\u5730\u65b9\u505a\u9519\u4e86\u4ec0\u4e48\uff0c\u4f46\u6211\u4e0d\u77e5\u9053\u662f\u4ec0\u4e48\u3002<\/p>\n<p>\u5982\u679c\u6709\u4eba\u53ef\u4ee5\u63d0\u4f9b\u5e2e\u52a9\uff0c\u6211\u4eec\u5c06\u4e0d\u80dc\u611f\u6fc0\uff01<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p><strong>\u4f7f\u7528\u81ea\u5b9a\u4e49\u5c0f\u90e8\u4ef6\u7684\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<p>\u8fd9\u662f\u57fa\u4e8e fyne \u3002\u7279\u522b\u662f\u60a8\u62e8\u6253 \uff1a<\/p>\n<p>extendbasewidget \u7531\u6269\u5c55\u5c0f\u90e8\u4ef6\u4f7f\u7528\u4ee5\u5229\u7528 basewidget \u529f\u80fd\u3002<\/p>\n<p>\u60a8\u4ecd\u7136\u5d4c\u5165 <code>widget.label<\/code>\uff0c\u4f46\u4e0d\u662f\u4f5c\u4e3a\u6307\u9488\uff08\u4f7f\u7528\u6307\u9488\u4e5f\u53ef\u4ee5\uff0c\u4f46\u6587\u6863\u4f7f\u7528\u975e\u6307\u9488\u793a\u4f8b\uff09\uff0c\u4ee5\u4fbf\u5c06\u5fc5\u8981\u7684\u65b9\u6cd5\u63d0\u5347\u5230\u60a8\u7684\u5c0f\u90e8\u4ef6\u4e2d\uff1a<\/p>\n<pre>func newtaplabel(text string, tappedleft func(string)) *taplabel {\n    label := &amp;taplabel{}\n    label.extendbasewidget(label)\n    label.settext(text)\n    label.ontapped = tappedleft\n    return label\n}<\/pre>\n<p>\u7136\u540e\u60a8\u66f4\u6539\u66f4\u65b0\u51fd\u6570\u4ee5\u5c06\u5b9e\u9645\u7ed1\u5b9a\u7684\u5b57\u7b26\u4e32\u8bbe\u7f6e\u4e3a\u6807\u7b7e\uff0c\u800c\u4e0d\u662f\u50cf\u539f\u59cb\u793a\u4f8b\u4e2d\u90a3\u6837\u518d\u6b21\u7ed1\u5b9a\u6570\u636e\uff1a<\/p>\n<pre>func(i binding.dataitem, o fyne.canvasobject) {\n            s, _ := i.(binding.string).get()\n            o.(*taplabel).settext(s)\n        },<\/pre>\n<p>\u81f3\u4e8e<strong>\u4e3a\u4ec0\u4e48<\/strong>\uff0c\u5982\u679c\u60a8\u7701\u7565\u8c03\u7528 <code>extendbasewidget<\/code>\uff0c\u5c31\u4f1a\u51fa\u73b0\u9519\u8bef\uff0c\u6211\u6000\u7591\u8fd9\u662f\u56e0\u4e3a\u5b83\u7f3a\u5c11\u6b64\u65b9\u6cd5\u8bbe\u7f6e\u7684\u57fa\u672c\u5c0f\u90e8\u4ef6\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u6ca1\u6709\u81ea\u5b9a\u4e49\u5c0f\u90e8\u4ef6\u7684\u89e3\u51b3\u65b9\u6848<\/strong>\uff1a<\/p>\n<pre>list := widget.NewListWithData(\n        data,\n        func() fyne.CanvasObject {\n            return &amp;widget.Label{Text: \"template\"}\n        },\n        func(i binding.DataItem, o fyne.CanvasObject) {\n            o.(*widget.Label).Bind(i.(binding.String))\n        },\n    )\n    list.OnSelected = func(id widget.ListItemID) {\n        fmt.Println(rawData[id])\n    }<\/pre>\n<p>\u653e\u5f03\u81ea\u5b9a\u4e49 <code>taplabel<\/code>\uff0c\u6539\u7528 <code>&amp;widget.label{text: \"template\"}<\/code>\uff0c\u7136\u540e\u4f7f\u7528 <code>list.onselected<\/code> \u6253\u5370\u70b9\u51fb\u7684\u9879\u76ee\u5e76\u5173\u95ed\u6570\u636e\u5207\u7247\u3002<\/p>\n<p>\u770b\u8d77\u6765\u50cf\u8fd9\u91cc\u7684\u53c2\u6570 <code>o.(*taplabel).bind(i.(binding.string))<\/code> \u968f\u7740\u6eda\u52a8\u64cd\u4f5c\u7684\u53d1\u751f\u800c\u6539\u53d8\u3002\u6dfb\u52a0\u65e5\u5fd7\u4ee5\u5728\u6eda\u52a8\u65f6\u89c2\u5bdf\u8be5\u51fd\u6570\u4e2d\u7684 <code>i.(binding.string)<\/code> \u503c<\/p>\n<p>\u770b\u770b\u8fd9\u4e2a\uff1a<\/p>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aFyne \u5217\u8868\u5c0f\u90e8\u4ef6\u65e0\u6cd5\u6b63\u786e\u663e\u793a\u9879\u76ee\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591aGolang\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; Fyne \u5217\u8868\u5c0f\u90e8\u4ef6\u65e0\u6cd5\u6b63\u786e\u663e\u793a\u9879\u76ee Fyne \u5217\u8868\u5c0f\u90e8\u4ef6\u65e0\u6cd5\u6b63\u786e\u663e\u793a\u9879\u76ee \u6765\u6e90\uff1astackoverflow 2024-04-26 18:30:37 0\u6d4f\u89c8 \u6536\u85cf \u5927\u5bb6\u597d\uff0c\u4eca\u5929\u672c\u4eba\u7ed9\u5927\u5bb6\u5e26\u6765\u6587\u7ae0\u300aFyne \u5217\u8868\u5c0f\u90e8\u4ef6\u65e0\u6cd5\u6b63\u786e\u663e\u793a\u9879\u76ee\u300b\uff0c\u6587\u4e2d\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230\uff0c\u5982\u679c\u4f60\u5bf9Golang\u65b9\u9762\u7684\u77e5\u8bc6\u70b9\u611f\u5174\u8da3\uff0c\u90a3\u5c31\u8bf7\u5404\u4f4d\u670b\u53cb\u7ee7\u7eed\u770b\u4e0b\u53bb\u5427~\u5e0c\u671b\u80fd\u771f\u6b63\u5e2e\u5230\u4f60\u4eec\uff0c\u8c22\u8c22\uff01 \u95ee\u9898\u5185\u5bb9 \u6211\u6b63\u5728\u5c1d\u8bd5\u5728\u5e26\u6709\u6570\u636e\u7684\u5217\u8868\u5c0f\u90e8\u4ef6\u4e2d\u663e\u793a\u5b57\u6bcd\u8868\u3002\u6211\u4f7f\u7528\u81ea\u5b9a\u4e49\u53ef\u70b9\u51fb\u6807\u7b7e\u4f5c\u4e3a\u8981\u5728\u5217\u8868\u4e2d\u663e\u793a\u7684\u5c0f\u90e8\u4ef6\u3002 \u7531\u4e8e\u67d0\u79cd\u539f\u56e0\uff0c\u5f53\u5c0f\u90e8\u4ef6\u52a0\u8f7d\u65f6\uff0c\u4e00\u5207\u90fd\u663e\u793a\u6b63\u5e38\u3002\u4f46\u662f\u5f53\u6211\u5f00\u59cb\u6eda\u52a8\u65f6\uff0c\u5b57\u6bcd\u5f00\u59cb\u4ee5\u5b8c\u5168\u968f\u673a\u7684\u987a\u5e8f\u663e\u793a\uff0c\u6211\u4e0d\u660e\u767d\u4e3a\u4ec0\u4e48\u3002 \u8fd9\u662f\u91cd\u73b0\u8be5\u9519\u8bef\u7684\u5b8c\u6574\u5de5\u4f5c\u4ee3\u7801\u3002 package main import ( &#8220;fmt&#8221; &#8220;fyne.io\/fyne\/v2&#8221; &#8220;fyne.io\/fyne\/v2\/app&#8221; &#8220;fyne.io\/fyne\/v2\/data\/binding&#8221; &#8220;fyne.io\/fyne\/v2\/widget&#8221; ) func makeAlphabet() []string { var alphabet []string for ch := &#8216;A&#8217;; ch &lt;= &#8216;Z&#8217;; ch++ { alphabet = append(alphabet, string(ch)) } return alphabet } type [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-42854","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42854","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=42854"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42854\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=42854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=42854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=42854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}