<?xml version="1.0" encoding="UTF-8"?>
<rss  xmlns:atom="http://www.w3.org/2005/Atom" 
      xmlns:media="http://search.yahoo.com/mrss/" 
      xmlns:content="http://purl.org/rss/1.0/modules/content/" 
      xmlns:dc="http://purl.org/dc/elements/1.1/" 
      version="2.0">
<channel>
<title>紙魚ㄉ部落格</title>
<link>https://paperfishblog.netlify.app/other.html</link>
<atom:link href="https://paperfishblog.netlify.app/other.xml" rel="self" type="application/rss+xml"/>
<description>未分類</description>
<generator>quarto-1.7.32</generator>
<lastBuildDate>Thu, 26 Feb 2026 16:00:00 GMT</lastBuildDate>
<item>
  <title>懶人包－快速在 Vscode 建立 Python 跟 R 環境</title>
  <dc:creator>紙魚 </dc:creator>
  <link>https://paperfishblog.netlify.app/posts/other/setting-py-r-in-vscode/</link>
  <description><![CDATA[ 






<p>最近會在學校用電腦的次數可能會相對變多，所以寫一下這個筆記~~。</p>
<section id="設定-python-在-vscode" class="level1">
<h1>設定 Python 在 Vscode</h1>
<section id="在-vsocde-中跑-python" class="level2">
<h2 class="anchored" data-anchor-id="在-vsocde-中跑-python">在 Vsocde 中跑 Python</h2>
<section id="執行步驟" class="level3">
<h3 class="anchored" data-anchor-id="執行步驟">執行步驟</h3>
<ol type="1">
<li><p>下載 Python &amp; Vscode</p></li>
<li><p>開啟 Vscode，選左側 Extensions，搜尋官方 Python (有藍勾勾)並安裝</p></li>
<li><p>在 Vscode 建立 <code>test.py</code>測試 code</p></li>
</ol>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"hello world"</span>)</span></code></pre></div>
<p>按右上角的播放鍵，下方會彈出 Terminal 顯示 output。</p>
</section>
</section>
<section id="在-vscode-跑-jupyter-notebook" class="level2">
<h2 class="anchored" data-anchor-id="在-vscode-跑-jupyter-notebook">在 Vscode 跑 Jupyter Notebook</h2>
<p>如果前面順利執行，代表原生 Python 的全域環境已經可以在 Vscode 執行。Jupyter Notebook 只要再多裝一個 Jupyter (有藍勾勾) 的 Extension 就好。</p>
<p>要測試的話也是一樣， 在 Vscode 建立 <code>test.ipynb</code>測試 code， <code>Crtl</code>+ <code>Enter</code> 執行 Code cell 。 首次執行時，會跳出需要安裝 ipykernel 的提示，選安裝。<strong>安裝完成後，建議先關閉 Vscode 後再重新開啟，不然可能會出現跑了 Cell 卻沒反應的情況</strong>。</p>
</section>
<section id="在-vscode-安裝-指定-venv-虛擬環境" class="level2">
<h2 class="anchored" data-anchor-id="在-vscode-安裝-指定-venv-虛擬環境">在 Vscode 安裝 &amp; 指定 venv 虛擬環境</h2>
<p>其實還有其他類型的虛擬環境可以裝(例如 conda)，但我習慣用 venv，所以寫這個：）。最快速懶人的做法如下</p>
<section id="在專案資料夾建立-venv-虛擬環境" class="level3">
<h3 class="anchored" data-anchor-id="在專案資料夾建立-venv-虛擬環境">在專案資料夾建立 venv 虛擬環境</h3>
<ol type="1">
<li><p>建立專案資料夾，並用 Vscode 開啟它。（或是開　Vscode 後選 Open Floder）</p></li>
<li><p>在 Vscode 的搜尋視窗內，輸入</p></li>
</ol>
<pre><code>&gt; Python: Select Interpreter</code></pre>
<ol start="3" type="1">
<li><p>然後選 “Create Virtual Environment…” / “<strong>Venv</strong> Creates a .venv virtual environment in the current workspace”</p></li>
<li><p>等待許久後，專案資料夾會出現 <code>.venv</code> 的虛擬環境資料夾。</p></li>
</ol>
<p>在 Jupyter Notebook 指定虛擬環境非常簡單，如果<code>.venv</code>跟剛剛設好的 <code>test.ipynb</code>在同一個專案資料夾下，只要在 <code>test.ipynb</code> 點選 Select Kernel，就會出現剛剛建立的 <code>.venv</code>。</p>
</section>
</section>
</section>
<section id="設定-r-在-vscode" class="level1">
<h1>設定 R 在 Vscode</h1>
<section id="在-vsocde-中跑-r" class="level2">
<h2 class="anchored" data-anchor-id="在-vsocde-中跑-r">在 Vsocde 中跑 R</h2>
<section id="執行步驟-1" class="level3">
<h3 class="anchored" data-anchor-id="執行步驟-1">執行步驟</h3>
<ol type="1">
<li><p>下載 R &amp; Vscode</p></li>
<li><p>開啟 R 原生 IDE，輸入以下code 安裝 Package:</p></li>
</ol>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"languageserver"</span>)</span></code></pre></div>
<p><img src="https://paperfishblog.netlify.app/posts/other/setting-py-r-in-vscode/r-lang.png" class="img-fluid"></p>
<ol start="3" type="1">
<li><p>開啟 Vscode，選左側 Extensions，搜尋官方 R (有藍勾勾)並安裝</p></li>
<li><p>在 Vscode search 視窗輸入</p></li>
</ol>
<pre><code>&gt; Preferences: Open Settings</code></pre>
<p>開啟 Preferences: Open Settings (UI) 視窗，在左側 Commend 面板找到 Extension 下 R 的部分，並找到 Rpaths 部份</p>
<p><a href="setting-r-in-vscode.md"></a></p>
<p>依據自己的作業系統輸入包含 R.exe 所在的 Path，以 Windows 為例，預設安裝 Path 為 <code>C:\Program Files\R\R-XXX(version)\bin\R.exe</code>，建議自己實際找一次以免出錯。</p>
</section>
<section id="測試" class="level3">
<h3 class="anchored" data-anchor-id="測試">測試</h3>
<p>在 Vscode 新增 <code>test.R</code> 輸入</p>
<div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">print</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Hello World!"</span>)</span></code></pre></div>
<p>按右上角的播放鍵，按第一下會喚醒 Terminal 裡的 R ，第二下後就會有結果囉！</p>
<p>這就是最基本的設定。</p>
</section>
</section>
<section id="在-vsocde-中跑-rmarkdown" class="level2">
<h2 class="anchored" data-anchor-id="在-vsocde-中跑-rmarkdown">在 Vsocde 中跑 Rmarkdown</h2>
<p>Rmarkdown 可以幫助我們在同一個中同時撰寫研究報告跟跑 R 程式的結果，非常的 useful！</p>
<section id="執行步驟-2" class="level3">
<h3 class="anchored" data-anchor-id="執行步驟-2">執行步驟</h3>
<ol type="1">
<li>安裝 pandoc: 因為 Rmarkdown 是基於 pandoc 設計的 package，所以需要先裝它。</li>
</ol>
<ul>
<li><p>前往 pandoc 官網下載最新版，依據作業系統下載對應執行檔</p></li>
<li><p>在 Terminal (Windows: cmd / Powershell 均可) 輸入</p></li>
</ul>
<div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb6-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">pandoc</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--version</span></span></code></pre></div>
<ul>
<li>有跑出對應版本代表有成功，沒有可能是需要將 Vscode 以及所有指令視窗關掉重開，或是環境變數問題。以 Windows 為例：
<ul>
<li>前者首先可以先嘗試關掉所有會用到指令操作的視窗，再重新開啟 cmd 或 powershell 重新測試，再去開啟 Vscode 的 Terminal 測試，都有跑出對應版本即可。</li>
<li>後者需要確定 <code>pandoc.exe</code> 的所在目錄，最好直接在檔案總管裡的C槽搜尋，因為在 Terminal 輸入的指令可能會找不到拒絕 User 存取的資料夾，再來去 Win 圖示&gt;右鍵&gt;設定&gt;搜尋環境變數&gt;點選編輯系統環境變數&gt;環境變數，在跳出視窗內點選”XXX(使用者名稱)的使用者變數”裡的 PATH，按”編輯”，會跳出目前已經設定的環境變數，檢查<code>pandoc.exe</code> 的所在目錄是在其中且路徑正確，如果沒有則修正，有的話把它上移看看，最後關掉所有視窗重新測試。</li>
</ul></li>
</ul>
<ol start="2" type="1">
<li>在 R 裡安裝 package rmarkdown</li>
</ol>
<div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"rmarkdown"</span>)</span></code></pre></div>
</section>
<section id="測試-1" class="level3">
<h3 class="anchored" data-anchor-id="測試-1">測試</h3>
<p>在 Vscode 新增 <code>test.rmd</code> 輸入 test code <a href="./test.txt" download="">點此下載 txt 版</a>。</p>
<!-- <a href="./test.rmd" download>點此下載 rmd 版(可能壞掉)</a> -->
<p>按右上角的播放鍵 ( Knit Rmd )，會跳出 output 顯示產製過程的 log，成功會像這樣：</p>
<pre><code>rmarkdown::render(...)


processing file: test.rmd

1/2                  

2/2 [unnamed-chunk-1]

output file: test.knit.md


... +RTS -K512m -RTS test.knit.md --to html4 --from markdown+autolink_bare_uris+tex_math_single_backslash --output test.html --lua-filter 
...
...
...
--embed-resources --standalone --variable bs3=TRUE --section-divs --template ...
...
...--include-in-header 

...


Output created: test.html

&lt;&lt;&lt;vsc&gt;&gt;&gt;...(outputfilename &amp; Path)&lt;&lt;&lt;vsc&gt;&gt;&gt;
[VSC-R] test.rmd process exited with exit code null</code></pre>
<p>並且可以在跟<code>test.rmd</code>同資料夾下找到<code>test.html</code></p>
<p><img src="https://paperfishblog.netlify.app/posts/other/setting-py-r-in-vscode/rmd-ex.png" class="img-fluid"></p>
<p>沒記錯的話，設定中文並且直接轉 PDF 需要另行設定，有點麻煩，改天有機會繼續寫。目前的設定可以產出 html 後用瀏覽器的列印功能(<code>Ctrl</code>+<code>P</code>)轉 PDF，非常簡單。</p>
</section>
</section>
</section>
<section id="reference" class="level1">
<h1>Reference</h1>
<section id="python" class="level2">
<h2 class="anchored" data-anchor-id="python">Python</h2>
<p>軟軟的官方文件基本上夠用了~~</p>
<ul>
<li><a href="https://code.visualstudio.com/docs/datascience/jupyter-notebooks">https://code.visualstudio.com/docs/datascience/jupyter-notebooks</a></li>
</ul>
</section>
<section id="r" class="level2">
<h2 class="anchored" data-anchor-id="r">R</h2>
<ul>
<li><p><a href="https://kemushi54.github.io/2019/09/07/%E7%AD%86%E8%A8%98-R-with-Visual-Studio-Code-VS-Code/">https://kemushi54.github.io/2019/09/07/%E7%AD%86%E8%A8%98-R-with-Visual-Studio-Code-VS-Code/</a></p></li>
<li><p><a href="https://www.datanovia.com/learn/tools/r-in-vscode/r-markdown-and-report-generation-in-vscode.html">https://www.datanovia.com/learn/tools/r-in-vscode/r-markdown-and-report-generation-in-vscode.html</a></p></li>
</ul>



</section>
</section>

<div class="quarto-listing quarto-listing-container-default" id="listing-listing">
<div class="list quarto-listing-default">

</div>
<div class="listing-no-matching d-none">無符合的項目</div>
</div> ]]></description>
  <category>note</category>
  <category>Vscode</category>
  <category>python</category>
  <category>R</category>
  <category>rmarkdown</category>
  <guid>https://paperfishblog.netlify.app/posts/other/setting-py-r-in-vscode/</guid>
  <pubDate>Thu, 26 Feb 2026 16:00:00 GMT</pubDate>
  <media:content url="https://paperfishblog.netlify.app/posts/other/setting-py-r-in-vscode/image.png" medium="image" type="image/png" height="81" width="144"/>
</item>
<item>
  <title>解決無法 update R package 問題: xfun</title>
  <dc:creator>紙魚 </dc:creator>
  <link>https://paperfishblog.netlify.app/posts/other/rmd-problem/rmd-problem.html</link>
  <description><![CDATA[ 






<p>寫作業寫到一半突然遇到問題，真得嚇死，趕快紀錄一下，<del>順便遲交每月文章kpi</del>。</p>
<section id="問題敘述" class="level1">
<h1>問題敘述</h1>
<p>在寫 rmarkdown 文件時突然遇到 YMAL 部分(—處)跑出紅線:</p>
<pre><code>Failed to run diagnostics: ! in callr subprocess.
Caused by error in `loadNamespace(j &lt;- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]])`:
! namespace 'xfun' 0.47 is being loaded, but &gt;= 0.52 is requiredlintr</code></pre>
<p>查了一下解法很簡單，只要用 R 安裝套件</p>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"xfun"</span>)</span>
<span id="cb2-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">packageVersion</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"xfun"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#確認版本</span></span></code></pre></div>
<p>即可，但我偏偏還碰到了結尾是 <code>Permission denied</code> 的錯誤，必須要關掉 Vscode 重開。此外還試了另一個(AI推薦的方法):</p>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.getenv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R_LIBS_USER"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 查看你的使用者套件路徑</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"xfun"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lib =</span> <span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">Sys.getenv</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"R_LIBS_USER"</span>)) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 塞使用者套件路徑</span></span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"xfun"</span>, <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">lib =</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"...../R/win-library/4.4"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 或是 .libPaths() 裡的第一個路徑直接貼上</span></span></code></pre></div>
<p>但無效，現在想想可能是因為沒有徹底重開？總之，第三次重開後，再輸入</p>
<div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"xfun"</span>)</span></code></pre></div>
<p>這時跳出來的 error 居然不同了，大概像這樣：</p>
<pre><code>Error in unpackPkgZip(foundpkgs[okp, 2L], foundpkgs[okp, 1L], lib, libs_only,  :
  ERROR: failed to lock directory 'R\win-library\4.4' for modifying
Try removing '\R\win-library\4.4/00LOCK'</code></pre>
<p>真的是太神奇，如果不是 AI 推薦方法成功一半就是重開大法有成吧？總之這個訊息的用意是要找出對應路徑下的資料夾(<code>00LOCK</code>)並刪除它，之後再來</p>
<div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">install.packages</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"xfun"</span>)</span>
<span id="cb6-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">packageVersion</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"xfun"</span>) <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">#確認版本</span></span></code></pre></div>
<p>即可成功消除紅線， rmarkdown 也能順利編譯成 PDF了，可喜可賀可口可樂。</p>
</section>
<section id="其他苦主與討論" class="level1">
<h1>其他苦主與討論</h1>
<ul>
<li><a href="https://forum.posit.co/t/cannot-update-package-xfun-to-0-19/86877">Cannot update Package “xfun” to 0.19</a></li>
</ul>



</section>

<div class="quarto-listing quarto-listing-container-default" id="listing-listing">
<div class="list quarto-listing-default">

</div>
<div class="listing-no-matching d-none">無符合的項目</div>
</div> ]]></description>
  <category>problem solver</category>
  <category>rmarkdown</category>
  <guid>https://paperfishblog.netlify.app/posts/other/rmd-problem/rmd-problem.html</guid>
  <pubDate>Fri, 26 Dec 2025 16:00:00 GMT</pubDate>
</item>
<item>
  <title>近況&amp;未來規劃(1)</title>
  <dc:creator>紙魚 </dc:creator>
  <link>https://paperfishblog.netlify.app/posts/other/dairy1/</link>
  <description><![CDATA[ 






<p>因為這個月作業、報告、論文進度各種夾殺，雖然我有一些筆記存稿，但因為都是理論類，內容感覺還是理解太淺，所以這個月應該來不及生出一篇QQ。另一方面，最近也想把我的網站跟 side project 做改版，但是可能到寒假才能動工，所以來寫一篇當作許願(?。最後，最近也有看到一些 AI 用在部落格文章上的一些討論，讓我開始思考有沒有更好的呈現內容的方式。</p>
<p>所以簡單來說，這是一篇想到甚麼東西就很隨意地記錄下來的文章，<del>用來水部落格文章數ㄉ</del></p>
<hr>
<section id="許願清單的部分" class="level1">
<h1>許願清單的部分~</h1>
<section id="我的部落格" class="level2">
<h2 class="anchored" data-anchor-id="我的部落格">我的部落格</h2>
<section id="新增歷史文章的板塊" class="level3">
<h3 class="anchored" data-anchor-id="新增歷史文章的板塊">新增歷史文章的板塊</h3>
<p>我有一個小小的興趣，就是看跟經濟史有關的書籍，大學時歷史是我一半的主修，時間允許也會去參加我感興趣的歷史講座。我有自己的手寫講座筆記，但還沒有很系統的整理過。現在因為歷史不再是我的主修，有些東西會隨時間忘卻，實在很可惜，所以很想把這些東西整理一下丟上來。</p>
<p>不過，考慮到 Quarto 的模板限制，直接丟文章列表旁邊的標籤( 原本是 Categories，我硬把它的名字改掉ㄌㄎㄎ)會混在一起。所以我開始思考要怎麼規劃，讓統計/資料科學的文章跟歷史分開，目前想到的解法就是在右上角做一個連結，點進去會顯示歷史類的文章類表跟文章標籤(就跟目前的主頁顯示一樣!)。不過這樣好像也需要新的 home page ，正在苦惱中~</p>
</section>
<section id="新增文章留言" class="level3">
<h3 class="anchored" data-anchor-id="新增文章留言">新增文章留言</h3>
<p>其實這個超級簡單，至少比前面簡單多了，只是 Quarto 跟一般 CMS 系統相比有個缺點是沒有在做 SEO 的話文章根本不會有曝光機會，因為搜尋系統根本爬不到XD 之前研究了下，如果有交 site map 給瀏覽器的話似乎有機會讓讀者用關鍵字找到，不過我目前只有交 Chrome 的，之前用 Edge 測試就無法。所以覺得這功能可有可無，感覺要先處理 SEO 的部份再新增才有意義。</p>
</section>
<section id="製作新史學的-csl-格式" class="level3">
<h3 class="anchored" data-anchor-id="製作新史學的-csl-格式">製作新史學的 csl 格式</h3>
<p><del>這個需求的起源是我覺得 apa 太醜</del>，唯一有在進行中的需求，不過因為我對製作 csl 檔案實在一竅不通，叫AI寫也是各種問題，我也搞不清楚到底是 quarto 渲染的問題還是 pandoc 不行，又或是我的語法要調整，就，很討厭的卡住了。這個需求也是我認為最需要先解決的，解完新增歷史文章時的 bib 列出來會好看很多。</p>
<p><img src="https://paperfishblog.netlify.app/posts/other/dairy1/where-is-my-author.png" class="img-fluid"> <em>目前測試結果，啊我的作者呢QAQ</em></p>
</section>
<section id="部落格的封面圖" class="level3">
<h3 class="anchored" data-anchor-id="部落格的封面圖">部落格的封面圖</h3>
<p>本著極簡風格(?)的理念，部落格的文章封面一直都是用小畫家+鼠繪使用，因為這樣風格最簡單，用AI生成的圖片有我不需要的 style。缺點就是圖片size無法統一，這是我困擾的地方，最近想到可以用 Canva 將以後生成的圖片統一，還可以用手畫(更換裝置的話)，整體來說只有優點沒有缺點。</p>
<p><del>不過我這篇還是小畫家就是了，因為簡單(逃</del></p>
</section>
</section>
<section id="side-project" class="level2">
<h2 class="anchored" data-anchor-id="side-project">side project</h2>
<section id="要大幅改版" class="level3">
<h3 class="anchored" data-anchor-id="要大幅改版">要!大!幅!改!版!</h3>
<p>因為真的設計得很不行，聽了業界人士講座才發現我設計得太差，沒辦法每隔一段時間就自動更新資料，也沒有類似 fine-tuning 的機制，導致模型的預測會隨時間拉長正確度降低。</p>
<p>要做到這些，首先需要自動爬資料、清洗資料的腳本，再來是可能會需要一個資料庫存資料，不能再用 csv 混過去，然後是 CICD 的環境，還有可能會需要 api 串接之類的。都是之前沒碰過的東西，之後要好好研究了。</p>
</section>
</section>
</section>
<section id="關於-ai-使用" class="level1">
<h1>關於 AI 使用</h1>
<p>最近看到 AI 使用方面的討論，主要是看到其他部落格文章作者跟 AI 互動方式，還有用 AI 建立工作流的的演講，讓我重新思考要不要重新註明我使用 AI 的地方。</p>
<p>目前這篇部落格的文章絕大多數都是我撰寫的，不過像數學式、流程圖，跟 code 產生與解說會經過 AI 處理，再經過我微調後再丟上來。<sup>1</sup>依據撰寫主題的不同，有些部落客先跟 AI 討論要寫的內容再下筆，或是將草稿交給 AI 審閱，但每一個字都是本人撰寫；有些部落客則是像我一樣有限度的將一部分的內容交給 AI 處理；也有些部落客則是完全把自己的部落格當成 AI 生成說明文件的放置區，一點進去就會知道這篇文章 100% 由 AI 生成。老實說，點到最後一種的時候我的心情都會不太好，因為通常我會想搜尋網路而不是跟 AI 討論的時候，都是我已經不相信 AI 能直接給我正確的 solution 的時候，結果滿心期待的點進去居然還是 AI 生成的文章，真的是喔氣氣氣氣氣氣。</p>
<p>看了一些關於 AI 使用方向的討論後，我認為把文章標註清楚不是壞事，只是目前關於部分使用 AI 生成文章還沒看到一個比較好的標註機制，反倒是完全不使用任何 AI 的作者因為通常本身就反對使用AI，反而會清楚標示他的文章以及授權範圍，這塊需要向他們好好學習。只是要如何把他們的做法應用在我的案例上，就需要好好想想了。</p>
</section>
<section id="結語或者該說許願" class="level1">
<h1>結語(或者該說許願?)</h1>
<p>希望我能平安度過期末、期末歐趴、12月可以生文章，以及以上願望盡早成真，以上~~</p>



</section>


<div class="quarto-listing quarto-listing-container-default" id="listing-listing">
<div class="list quarto-listing-default">

</div>
<div class="listing-no-matching d-none">無符合的項目</div>
</div><div id="quarto-appendix" class="default"><section id="footnotes" class="footnotes footnotes-end-of-document"><h2 class="anchored quarto-appendix-heading">腳註</h2>

<ol>
<li id="fn1"><p>如果看到文句不通順，那就是我沒改到:P↩︎</p></li>
</ol>
</section></div> ]]></description>
  <category>dairy</category>
  <guid>https://paperfishblog.netlify.app/posts/other/dairy1/</guid>
  <pubDate>Tue, 18 Nov 2025 16:00:00 GMT</pubDate>
  <media:content url="https://paperfishblog.netlify.app/posts/other/dairy1/image.png" medium="image" type="image/png" height="81" width="144"/>
</item>
<item>
  <title>使用python做離線倒數計時器</title>
  <dc:creator>紙魚 </dc:creator>
  <link>https://paperfishblog.netlify.app/posts/other/make-app/</link>
  <description><![CDATA[ 






<!-- 使用 AI -->
<div data-ai-tag="2">

</div>
<script src="../../../../asserts/box.js" defer=""></script>
<p>這篇文章有點流水帳，就是一篇製作玩具的過程隨筆。用AI寫程式可以做到自己以前做不到的，但遇到問題還是要想辦法解決，解決問題還是需要思考跟經驗，所以我想還是紀錄一下，供日後參考囉。</p>
<hr>
<section id="緣起" class="level1">
<h1>緣起</h1>
<p>因為最近覺得用眼過度，想要一個小玩具來提醒自己要定時休息，由於倒數計時的功能不會很複雜，自己用還可以客製化自己想要的功能，所以就來動手做做看！</p>
<p>我先叫AI生了靜態倒數計時器的網頁，UI看起來很不錯：</p>
<p><img src="https://paperfishblog.netlify.app/posts/other/make-app/image1.png" class="img-fluid"></p>
<p>不過很就發現，它沒有辦法準時計時，因為瀏覽器會暫停閒置的網站，哭哭。</p>
<p>然後我就把歪腦筋動到了用 python 製作 exe 上，之前只是耳聞過 python 可以做到，但我其實沒有嘗試過製作一個 exe 檔，正好是個機會 :) 就來試試看吧！</p>
<section id="使用環境與套件" class="level2">
<h2 class="anchored" data-anchor-id="使用環境與套件">使用環境與套件</h2>
<p><strong>使用環境</strong></p>
<ul>
<li><p>Windows 11</p></li>
<li><p>python version: 3.12.5</p></li>
</ul>
<p><strong>使用套件:</strong></p>
<ul>
<li>tkinter</li>
</ul>
<blockquote class="blockquote">
<p>製作 GUI 的基本套件，優點是簡單，缺點是UI有些陽春</p>
</blockquote>
<ul>
<li>customtkinte</li>
</ul>
<blockquote class="blockquote">
<p>UI比較美觀的套件，用來優化舊有UI。</p>
</blockquote>
<ul>
<li>playsound</li>
</ul>
<blockquote class="blockquote">
<p>播放聲音用的套件</p>
</blockquote>
<ul>
<li>pyinstaller</li>
</ul>
<blockquote class="blockquote">
<p>製作exe的套件</p>
</blockquote>
</section>
</section>
<section id="製作過程" class="level1">
<h1>製作過程</h1>
<section id="第一步開需求" class="level2">
<h2 class="anchored" data-anchor-id="第一步開需求">第一步：開需求</h2>
<p>我的需求如下：</p>
<ul>
<li>每30分鐘跳視窗提醒休息5分鐘，使用者按按鈕再繼續計時。</li>
<li>螢幕上要顯示剩下幾分鐘。</li>
<li>以秒為單位更新畫面。</li>
</ul>
</section>
<section id="第二步丟ai生架構" class="level2">
<h2 class="anchored" data-anchor-id="第二步丟ai生架構">第二步：丟AI生架構</h2>
<p>把需求丟給AI後，生了一個架構出來</p>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb1-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> tkinter <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> tk</span>
<span id="cb1-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> tkinter <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> messagebox</span>
<span id="cb1-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> time</span>
<span id="cb1-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> threading</span>
<span id="cb1-5"></span>
<span id="cb1-6"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 時間設定（秒）</span></span>
<span id="cb1-7">WORK_DURATION <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 30分鐘</span></span>
<span id="cb1-8">BREAK_DURATION <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span>   <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 5分鐘</span></span>
<span id="cb1-9"></span>
<span id="cb1-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># GUI 主視窗</span></span>
<span id="cb1-11">root <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tk.Tk()</span>
<span id="cb1-12">root.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"番茄鐘計時器"</span>)</span>
<span id="cb1-13">root.geometry(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"320x180"</span>)</span>
<span id="cb1-14">root.resizable(<span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>, <span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">False</span>)</span>
<span id="cb1-15"></span>
<span id="cb1-16">status_label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tk.Label(root, text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"準備中..."</span>, font<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Arial"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">16</span>))</span>
<span id="cb1-17">status_label.pack(pady<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb1-18"></span>
<span id="cb1-19">time_label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tk.Label(root, text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"剩餘時間：00:00"</span>, font<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Arial"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span>))</span>
<span id="cb1-20">time_label.pack(pady<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb1-21"></span>
<span id="cb1-22"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 控制計時的旗標</span></span>
<span id="cb1-23">stop_flag <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> threading.Event()</span>
<span id="cb1-24"></span>
<span id="cb1-25"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> format_time(seconds):</span>
<span id="cb1-26">    minutes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> seconds <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span></span>
<span id="cb1-27">    secs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> seconds <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span></span>
<span id="cb1-28">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>minutes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:02}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">:</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>secs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:02}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb1-29"></span>
<span id="cb1-30"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> run_timer(duration, status_text, done_message):</span>
<span id="cb1-31">    remaining <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> duration</span>
<span id="cb1-32"></span>
<span id="cb1-33">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 更新狀態文字</span></span>
<span id="cb1-34">    root.after(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span>: status_label.config(text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>status_text))</span>
<span id="cb1-35"></span>
<span id="cb1-36">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> remaining <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">and</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> stop_flag.is_set():</span>
<span id="cb1-37">        display <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"剩餘時間：</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>format_time(remaining)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb1-38">        root.after(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span> d<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>display: time_label.config(text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>d))</span>
<span id="cb1-39">        time.sleep(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb1-40">        remaining <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb1-41"></span>
<span id="cb1-42">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> stop_flag.is_set():</span>
<span id="cb1-43">        root.after(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span>, <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span>: messagebox.showinfo(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"提醒"</span>, done_message))</span>
<span id="cb1-44"></span>
<span id="cb1-45"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> timer_loop():</span>
<span id="cb1-46">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> stop_flag.is_set():</span>
<span id="cb1-47">        run_timer(WORK_DURATION, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"⏱ 工作中"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"⏰ 工作時間結束！請休息 5 分鐘"</span>)</span>
<span id="cb1-48">        run_timer(BREAK_DURATION, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"☕ 休息中"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"✅ 休息結束，準備進入下一輪"</span>)</span>
<span id="cb1-49"></span>
<span id="cb1-50"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> start_timer():</span>
<span id="cb1-51">    stop_flag.clear()</span>
<span id="cb1-52">    threading.Thread(target<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>timer_loop, daemon<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>).start()</span>
<span id="cb1-53"></span>
<span id="cb1-54"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> stop_timer():</span>
<span id="cb1-55">    stop_flag.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">set</span>()</span>
<span id="cb1-56">    status_label.config(text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"計時已停止"</span>)</span>
<span id="cb1-57">    time_label.config(text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"剩餘時間：00:00"</span>)</span>
<span id="cb1-58"></span>
<span id="cb1-59"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 按鈕 UI</span></span>
<span id="cb1-60">btn_frame <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tk.Frame(root)</span>
<span id="cb1-61">btn_frame.pack(pady<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span>)</span>
<span id="cb1-62"></span>
<span id="cb1-63">start_btn <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tk.Button(btn_frame, text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"開始計時"</span>, command<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>start_timer, font<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Arial"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>))</span>
<span id="cb1-64">start_btn.pack(side<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>tk.LEFT, padx<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb1-65"></span>
<span id="cb1-66">stop_btn <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> tk.Button(btn_frame, text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"結束計時"</span>, command<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>stop_timer, font<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Arial"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">12</span>))</span>
<span id="cb1-67">stop_btn.pack(side<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>tk.RIGHT, padx<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb1-68"></span>
<span id="cb1-69"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 啟動 GUI 主迴圈</span></span>
<span id="cb1-70">root.mainloop()</span></code></pre></div>
<p>做出來的畫面像這樣：</p>
<p><img src="https://paperfishblog.netlify.app/posts/other/make-app/image2.png" class="img-fluid"></p>
<p>但用 <code>tkinter</code> 做出來的介面有點醜，於是改用<code>customtkinte</code>替代，再追加希望有聲音提醒的需求，於是有了第二版：</p>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb2-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> customtkinter <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> ctk</span>
<span id="cb2-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> time</span>
<span id="cb2-3"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> threading</span>
<span id="cb2-4"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> tkinter <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> messagebox</span>
<span id="cb2-5"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">from</span> playsound <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> playsound</span>
<span id="cb2-6"></span>
<span id="cb2-7">WORK_DURATION <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">30</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span></span>
<span id="cb2-8">BREAK_DURATION <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">5</span> <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">*</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span></span>
<span id="cb2-9">SOUND_FILE <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"ding.mp3"</span></span>
<span id="cb2-10"></span>
<span id="cb2-11">ctk.set_appearance_mode(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"dark"</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 或 "light"</span></span>
<span id="cb2-12">ctk.set_default_color_theme(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"blue"</span>)</span>
<span id="cb2-13"></span>
<span id="cb2-14">app <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ctk.CTk()</span>
<span id="cb2-15">app.title(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"番茄鐘計時器"</span>)</span>
<span id="cb2-16">app.geometry(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"360x220"</span>)</span>
<span id="cb2-17"></span>
<span id="cb2-18">status_label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ctk.CTkLabel(app, text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"準備中..."</span>, font<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Arial"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">18</span>))</span>
<span id="cb2-19">status_label.pack(pady<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb2-20"></span>
<span id="cb2-21">time_label <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ctk.CTkLabel(app, text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"剩餘時間：00:00"</span>, font<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"Arial"</span>, <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">32</span>))</span>
<span id="cb2-22">time_label.pack(pady<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb2-23"></span>
<span id="cb2-24">stop_flag <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> threading.Event()</span>
<span id="cb2-25"></span>
<span id="cb2-26"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> format_time(seconds):</span>
<span id="cb2-27">    minutes <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> seconds <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">//</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span></span>
<span id="cb2-28">    secs <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> seconds <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">%</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">60</span></span>
<span id="cb2-29">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> <span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>minutes<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:02}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">:</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>secs<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">:02}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span></span>
<span id="cb2-30"></span>
<span id="cb2-31"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> run_timer(duration, status_text, done_message):</span>
<span id="cb2-32">    remaining <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> duration</span>
<span id="cb2-33">    status_label.configure(text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>status_text)</span>
<span id="cb2-34">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> remaining <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">and</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> stop_flag.is_set():</span>
<span id="cb2-35">        time_label.configure(text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"剩餘時間：</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>format_time(remaining)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb2-36">        time.sleep(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb2-37">        remaining <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb2-38">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> stop_flag.is_set():</span>
<span id="cb2-39">        threading.Thread(target<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">lambda</span>: playsound(SOUND_FILE), daemon<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>).start()</span>
<span id="cb2-40">        messagebox.showinfo(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"提醒"</span>, done_message)</span>
<span id="cb2-41"></span>
<span id="cb2-42"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> timer_loop():</span>
<span id="cb2-43">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> stop_flag.is_set():</span>
<span id="cb2-44">        run_timer(WORK_DURATION, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"⏱ 工作中"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"⏰ 工作時間結束！請休息 5 分鐘"</span>)</span>
<span id="cb2-45">        run_timer(BREAK_DURATION, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"☕ 休息中"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"✅ 休息結束，準備進入下一輪"</span>)</span>
<span id="cb2-46"></span>
<span id="cb2-47"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> start_timer():</span>
<span id="cb2-48">    stop_flag.clear()</span>
<span id="cb2-49">    threading.Thread(target<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>timer_loop, daemon<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>).start()</span>
<span id="cb2-50"></span>
<span id="cb2-51"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> stop_timer():</span>
<span id="cb2-52">    stop_flag.<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">set</span>()</span>
<span id="cb2-53">    status_label.configure(text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"計時已停止"</span>)</span>
<span id="cb2-54">    time_label.configure(text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"剩餘時間：00:00"</span>)</span>
<span id="cb2-55"></span>
<span id="cb2-56">btn_frame <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ctk.CTkFrame(app)</span>
<span id="cb2-57">btn_frame.pack(pady<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb2-58"></span>
<span id="cb2-59">start_btn <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ctk.CTkButton(btn_frame, text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"開始計時"</span>, command<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>start_timer)</span>
<span id="cb2-60">start_btn.pack(side<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"left"</span>, padx<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb2-61"></span>
<span id="cb2-62">stop_btn <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ctk.CTkButton(btn_frame, text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"結束計時"</span>, command<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>stop_timer)</span>
<span id="cb2-63">stop_btn.pack(side<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"right"</span>, padx<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">10</span>)</span>
<span id="cb2-64"></span>
<span id="cb2-65">app.mainloop()</span></code></pre></div>
<p>介面長得像這樣：　</p>
<p><img src="https://paperfishblog.netlify.app/posts/other/make-app/image3.png" class="img-fluid"></p>
<p>好看多了！</p>
<p>其實不用聲音檔，光有這樣的架構，再寫個 bat 呼叫腳本其實就能用了，但到這裡我開始越來越貪心，想要弄得更精細，於是又加了2個需求</p>
<ol type="1">
<li>聲音檔分成2個，工作時和休息時的音效不同</li>
<li>聲音只播一次，且要跟訊息一起出現</li>
</ol>
</section>
<section id="第三步微調程式與參數" class="level2">
<h2 class="anchored" data-anchor-id="第三步微調程式與參數">第三步：微調程式與參數</h2>
<section id="聲音檔分成2個" class="level3">
<h3 class="anchored" data-anchor-id="聲音檔分成2個">聲音檔分成2個</h3>
<p>實際上需要改動2個地方，一個是在函數<code>run_timer</code>增加區分工作跟休息的音效，另一個是在函數<code>timer_loop</code>中塞入2個聲音檔。音檔可以自行去網路上找喜歡的，並且建議轉成<code>.wav</code>檔格式，執行程式時比較不會出問題(像<code>.mp3</code>就常會有有時可以順利播放有時無法)</p>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb3-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 主循環：工作 → 休息 → 循環</span></span>
<span id="cb3-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> timer_loop():</span>
<span id="cb3-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> stop_flag.is_set():</span>
<span id="cb3-4">        run_timer(WORK_DURATION, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"⏱ 工作中"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"⏰ 工作結束！請休息 5 分鐘"</span>, SOUND_FILE)</span>
<span id="cb3-5">        run_timer(BREAK_DURATION, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"☕ 休息中"</span>, <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"✅ 休息結束，準備進入下一輪"</span>, SOUND_FILE2)</span></code></pre></div>
</section>
<section id="聲音只播一次且跟訊息一起出現" class="level3">
<h3 class="anchored" data-anchor-id="聲音只播一次且跟訊息一起出現">聲音只播一次，且跟訊息一起出現</h3>
<p>這需要調整兩個函數：</p>
<div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb4-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 播放音效（非阻塞）</span></span>
<span id="cb4-2"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> play_sound(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span>):</span>
<span id="cb4-3">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb4-4">        pygame.mixer.music.load(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span>)</span>
<span id="cb4-5">        pygame.mixer.music.play()</span>
<span id="cb4-6">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Exception</span> <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> e:</span>
<span id="cb4-7">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"⚠ 播放音效失敗：</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb4-8"></span>
<span id="cb4-9"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 執行計時器</span></span>
<span id="cb4-10"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 這裡使用非阻塞方式播放音效，避免阻塞主線</span></span>
<span id="cb4-11"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> run_timer(duration, status_text, done_message, sound_file):</span>
<span id="cb4-12">    remaining <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> duration</span>
<span id="cb4-13">    status_label.configure(text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span>status_text)</span>
<span id="cb4-14"></span>
<span id="cb4-15">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">while</span> remaining <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">&gt;=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">0</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">and</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> stop_flag.is_set():</span>
<span id="cb4-16">        time_label.configure(text<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"剩餘時間：</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>format_time(remaining)<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span>
<span id="cb4-17">        time.sleep(<span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span>)</span>
<span id="cb4-18">        remaining <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">-=</span> <span class="dv" style="color: #AD0000;
background-color: null;
font-style: inherit;">1</span></span>
<span id="cb4-19"></span>
<span id="cb4-20">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> stop_flag.is_set():</span>
<span id="cb4-21">        play_sound(sound_file)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ✅ 非阻塞播放音效</span></span>
<span id="cb4-22">        messagebox.showinfo(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"提醒"</span>, done_message)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># ✅ 等待使用者按下「確定」</span></span></code></pre></div>
</section>
<section id="自訂音效開啟關閉" class="level3">
<h3 class="anchored" data-anchor-id="自訂音效開啟關閉">自訂音效開啟/關閉</h3>
<p>這裡指的是自訂音效，window預設的視窗跳出音效不在此範圍。需要增加/修改的指令有：</p>
<div class="sourceCode" id="cb5" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb5-1">sound_enabled <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> ctk.BooleanVar(value<span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span><span class="va" style="color: #111111;
background-color: null;
font-style: inherit;">True</span>)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 預設開啟音效</span></span></code></pre></div>
<p>這條程式需要放在主視窗 UI 剛建立的時候，也就是<code>app = ctk.CTk()</code>之後，不然會報錯。</p>
<p>其次是修改 <code>play_sound()</code>：</p>
<div class="sourceCode" id="cb6" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb6-1"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> play_sound(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span>):</span>
<span id="cb6-2">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">if</span> <span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">not</span> sound_enabled.get():</span>
<span id="cb6-3">        <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span>  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 如果沒勾選，就不播放音效</span></span>
<span id="cb6-4">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb6-5">        pygame.mixer.music.load(<span class="bu" style="color: null;
background-color: null;
font-style: inherit;">file</span>)</span>
<span id="cb6-6">        pygame.mixer.music.play()</span>
<span id="cb6-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Exception</span> <span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">as</span> e:</span>
<span id="cb6-8">        <span class="bu" style="color: null;
background-color: null;
font-style: inherit;">print</span>(<span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">f"⚠ 播放音效失敗：</span><span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">{</span>e<span class="sc" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">}</span><span class="ss" style="color: #20794D;
background-color: null;
font-style: inherit;">"</span>)</span></code></pre></div>
<p>就可以得到一個勾選是否要啟用音效的欄位了。</p>
</section>
<section id="配合打包成exe的調整" class="level3">
<h3 class="anchored" data-anchor-id="配合打包成exe的調整">配合打包成exe的調整</h3>
<p>因為py檔在執行程式時會認附加檔案的路徑，即使打包成exe後也是如此，因此需要就這一段做微調。</p>
<div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb7-1"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> os</span>
<span id="cb7-2"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> sys</span>
<span id="cb7-3"></span>
<span id="cb7-4"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 取得正確路徑（支援 pyinstaller 打包後的執行檔）</span></span>
<span id="cb7-5"><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">def</span> resource_path(relative_path):</span>
<span id="cb7-6">    <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">"""取得資源真實路徑（支援開發階段與打包後）"""</span></span>
<span id="cb7-7">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">try</span>:</span>
<span id="cb7-8">        base_path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> sys._MEIPASS  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># PyInstaller 的暫存資料夾</span></span>
<span id="cb7-9">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">except</span> <span class="pp" style="color: #AD0000;
background-color: null;
font-style: inherit;">Exception</span>:</span>
<span id="cb7-10">        base_path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> os.path.abspath(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"."</span>)</span>
<span id="cb7-11">    <span class="cf" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">return</span> os.path.join(base_path, relative_path)</span>
<span id="cb7-12"></span>
<span id="cb7-13"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 使用方式：</span></span>
<span id="cb7-14">sound_file_path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> resource_path(<span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sound.wav"</span>)</span>
<span id="cb7-15"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 程式使用 resource_path()，就可以正確找到聲音檔了。</span></span>
<span id="cb7-16"></span>
<span id="cb7-17"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 例如播放聲音</span></span>
<span id="cb7-18"><span class="im" style="color: #00769E;
background-color: null;
font-style: inherit;">import</span> playsound</span>
<span id="cb7-19">playsound.playsound(sound_file_path)</span></code></pre></div>
<p>執行時，PyInstaller 會自動把聲音檔解壓到臨時目錄中，這樣就可以找到聲音檔了。</p>
<blockquote class="blockquote">
<p>如果不使用<code>resource_path()</code>會怎麼樣？</p>
<p>由於 PyInstaller 會把打包成一個 <code>.exe</code>。當執行這個 <code>.exe</code> 時，它會先解壓所有檔案到一個臨時目錄（通常是 %TEMP%_MEIxxxxx），然後才開始執行程式。如果沒有<code>resource_path()</code>就會導致找不到檔案，因為它找到臨時目錄去了。</p>
</blockquote>
</section>
<section id="ico" class="level3">
<h3 class="anchored" data-anchor-id="ico">加入自定義圖示</h3>
<p>正常不做調整的情況下，視窗圖示為<img src="https://paperfishblog.netlify.app/posts/other/make-app/image4.png" class="img-fluid">，但我想換掉變成自己的。先找一個 <code>.png</code> 轉成 <code>.ico</code> 圖示，放進同一個專案資料夾。接下來要做的事跟前面的音檔類似 :</p>
<div class="sourceCode" id="cb8" style="background: #f1f3f5;"><pre class="sourceCode python code-with-copy"><code class="sourceCode python"><span id="cb8-1"><span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 設定視窗圖示</span></span>
<span id="cb8-2">icon_path <span class="op" style="color: #5E5E5E;
background-color: null;
font-style: inherit;">=</span> resource_path(ICON_FILE)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 使用之前定義的資源路徑函數</span></span>
<span id="cb8-3">app.iconbitmap(icon_path)  <span class="co" style="color: #5E5E5E;
background-color: null;
font-style: inherit;"># 必須是 .ico 格式</span></span></code></pre></div>
</section>
</section>
<section id="第四步打包成exe" class="level2">
<h2 class="anchored" data-anchor-id="第四步打包成exe">第四步：打包成exe</h2>
<p>用<code>pyinstaller</code>進行打包，首先要先安裝套件:</p>
<div class="sourceCode" id="cb9" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb9-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">pip</span> install pyinstaller</span></code></pre></div>
<section id="基本指令" class="level3">
<h3 class="anchored" data-anchor-id="基本指令">基本指令</h3>
<p>打包的基本指令為</p>
<div class="sourceCode" id="cb10" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb10-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">pyinstaller</span> your_script.py</span></code></pre></div>
<p>執行完成後系統會提示放在 <code>.\dist</code>的資料夾下，具體結構為：</p>
<pre><code>your_script/
├── dist/
│   └── your_script.exe  ← 可執行檔在這裡
├── build/
├── your_script.spec </code></pre>
<p><code>build/</code> 和 <code>.spec</code> 是中間產物，可以保留也可以刪除。</p>
<p>除此之外還有常用附加指令：</p>
<table class="caption-top table">
<colgroup>
<col style="width: 40%">
<col style="width: 59%">
</colgroup>
<thead>
<tr class="header">
<th>參數</th>
<th>說明</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><code>--onefile</code></td>
<td>打包成單一 <code>.exe</code> 檔案（預設會拆成很多檔）</td>
</tr>
<tr class="even">
<td><code>--noconsole</code></td>
<td>不顯示命令列（適合正式 GUI 程式），不輸入時點擊<code>exe</code>會跳出cmd畫面。</td>
</tr>
<tr class="odd">
<td><code>--icon=icon.ico</code></td>
<td>指定執行檔圖示</td>
</tr>
</tbody>
</table>
<blockquote class="blockquote">
<p>實作注意事項：</p>
<ol type="1">
<li><p><code>--onefile</code>建議必加，不然會跑出很多資料夾or檔案 <del>有點像遊戲拆包</del></p></li>
<li><p><code>--noconsole</code> 在測試階段可以不用加入，方便檢查程式被打包成<code>exe</code>後會不會有前面沒有出現過的錯誤訊息。</p></li>
</ol>
</blockquote>
</section>
<section id="加入附加檔" class="level3">
<h3 class="anchored" data-anchor-id="加入附加檔">加入附加檔</h3>
<p>假設加入的聲音為<code>sounds.wav</code>，那麼指令為：</p>
<div class="sourceCode" id="cb12" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb12-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">--add-data</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sounds.wav;."</span></span></code></pre></div>
<p>這裡的<code>;</code>後面是資料夾的意思，<code>.</code>表示跟主程式一樣放在同一個資料夾。如果有兩個就在後面繼續使用此指令。</p>
<p>如果聲音檔跟主程式放在不同資料夾，像這樣：</p>
<pre><code>project/
├── main.py
└── sounds/
    ├── click.wav
    └── error.wav</code></pre>
<p>那指令就是：</p>
<div class="sourceCode" id="cb14" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb14-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">pyinstaller</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--onefile</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">--add-data</span> <span class="st" style="color: #20794D;
background-color: null;
font-style: inherit;">"sounds;sounds"</span> main.py</span></code></pre></div>
<p>代表「把 <code>sounds/</code> 整個資料夾加到 <code>.exe</code> 所在目錄中」</p>
<blockquote class="blockquote">
<p>注意：</p>
<p>如果有設定前面的圖示，一樣需要用 <code>--add-data "XXX.ico;."</code>，不然它會找不到檔案。</p>
</blockquote>
</section>
<section id="加入icon" class="level3">
<h3 class="anchored" data-anchor-id="加入icon">加入icon</h3>
<p>這裡指的是打包成 exe 時的圖示，指令為：</p>
<div class="sourceCode" id="cb15" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb15-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">--icon=icon.ico</span></span></code></pre></div>
<p>是顯示exe程式的圖示，可以自己找或加工一張png，再去轉成ico來用。生成exe後圖示不一定馬上會出現，通常過一段時間才會出現。</p>
</section>
</section>
<section id="打包過程遇到的困難" class="level2">
<h2 class="anchored" data-anchor-id="打包過程遇到的困難">打包過程遇到的困難</h2>
<p>在測試py腳本時一切順利，不過在打包exe遇到套件沒有安裝的神奇狀況，而且問AI，是它給的做法都完全沒用，後來仔細觀察<code>pyinstaller</code>產出的log才發現原來是我平常都把套件都安裝在 <code>venv</code> 的虛擬環境下，但是 <code>pyinstaller</code> 在執行時沒有調整過就會自動找全域環境，於是就用指令啟動虛擬環境，確認虛擬環境下有安裝<code>pyinstaller</code>後再執行打包就沒問題了。</p>
<p>中間還遇到一個有點蠢的問題XD，我常用的虛擬環境安裝在 “桌面” 下的資料夾，好死不死路徑名稱是中文的，導致專案在Vscode自動指定虛擬環境後還遇到因為無法辨識中文路徑而舞法啟用python的狀況XD。其實只要再建一個虛擬環境在C槽就好，但我就是不死心<del>對原本的虛擬環境有感情ㄌ</del>，一直找其他方法，後來用指令啟動虛擬環境後就好了，白繞了一點路XD。</p>
<p><del>不過最後我還是乖乖建新的虛擬環境了，不然其他專案早晚會出問題</del></p>
</section>
</section>
<section id="此-exe-檔背後執行原理" class="level1">
<h1>此 exe 檔背後執行原理</h1>
<p>涉及作業系統相關的知識，基礎用語請見<a href="https://medium.com/erens-tech-book/%E7%90%86%E8%A7%A3-process-thread-94a40721b492">此來源</a>。 每當執行一個 <code>.exe</code>，作業系統就會開一個新的 process，由這個 process 執行你的程式，並從主執行緒 ( main tread )開始跑起。在程式中用 <code>threading.Thread()</code> 則會再額外開新的tread，也就是說每按下一個按鈕事件，就會啟動一個新的 tread。</p>
<p>這也讓目前的小程式有個問題，當過於頻繁地切換按鈕事件時程式容易當機，且容易有兩個按鈕 tread 同時在跑的狀況，是未來可以再精進的部分。</p>
</section>
<section id="心得" class="level1">
<h1>心得</h1>
<p>雖然是個簡單的小玩具，但中間為了客製化、解決遇到的問題還是花了一天多的時間還做出稍微滿意的樣子。不過也學到很多~ <del>作為逃避寫論文的娛樂也很好</del>，下次有機會再來玩玩別的😄。</p>



</section>

<div class="quarto-listing quarto-listing-container-default" id="listing-listing">
<div class="list quarto-listing-default">

</div>
<div class="listing-no-matching d-none">無符合的項目</div>
</div> ]]></description>
  <category>python</category>
  <category>toys</category>
  <guid>https://paperfishblog.netlify.app/posts/other/make-app/</guid>
  <pubDate>Fri, 08 Aug 2025 16:00:00 GMT</pubDate>
  <media:content url="https://paperfishblog.netlify.app/posts/other/make-app/image.png" medium="image" type="image/png" height="81" width="144"/>
</item>
<item>
  <title>如何使用 Python jupyter-book 建立一本書</title>
  <dc:creator>紙魚 </dc:creator>
  <link>https://paperfishblog.netlify.app/posts/other/jpyterbook/</link>
  <description><![CDATA[ 






<!-- 使用 AI -->
<div data-ai-tag="1,2">

</div>
<script src="../../../../asserts/box.js" defer=""></script>
<section id="前言" class="level1">
<h1>前言</h1>
<p>說到 jupyter-book，可能很多人會想到 Jupyter Notebook，但這裡指的是一個 python 套件，用來建立線上書籍的工具，特別適合用於副檔名為 <code>.ipynb</code> 的 Jupyter Notebook 。最後產出的網站類似 gitbook 或是 bookdown 的靜態網站，適合用來寫教學、筆記或是書籍。也是在我使用 Quarto 之前考慮過的選項之一，雖然最後因為產出有點陽春、能改動的東西較少所以放棄了，但它的製作過程簡單，所以留下這個筆記，方便日後需要時可以快速上手。</p>
<section id="使用環境" class="level2">
<h2 class="anchored" data-anchor-id="使用環境">使用環境</h2>
<p>這是我的使用環境，不一定適用每個人，但截至目前為止都運作順利：</p>
<ul>
<li><p>IDE ： VsCode</p></li>
<li><p>Python version : 3.12.5</p></li>
</ul>
</section>
</section>
<section id="首次興建" class="level1">
<h1>首次興建</h1>
<p>依照以下步驟操作：</p>
<hr>
<section id="安裝-jupyter-book" class="level2">
<h2 class="anchored" data-anchor-id="安裝-jupyter-book">安裝 jupyter-book</h2>
<div class="sourceCode" id="cb1" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb1-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">pip</span> install <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-U</span> jupyter-book</span></code></pre></div>
<hr>
</section>
<section id="建立一本書的骨架" class="level2">
<h2 class="anchored" data-anchor-id="建立一本書的骨架">建立一本書的骨架</h2>
<p>使用命令行建立一個新的書籍專案：</p>
<div class="sourceCode" id="cb2" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb2-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">jupyter-book</span> create mybook/</span></code></pre></div>
<p>這會建立一個名為 <code>mybook/</code> 的資料夾，裡面包含書籍的基本結構，包括 Markdown 與 Jupyter Notebook 範例，<code>mybook/</code> 亦可以自行換成其他資料夾名稱。</p>
<hr>
</section>
<section id="編輯內容" class="level2">
<h2 class="anchored" data-anchor-id="編輯內容">編輯內容</h2>
<blockquote class="blockquote">
<p>從這裡開始就是在編輯時會重複執行的步驟！</p>
</blockquote>
<p>可以在 <code>mybook/</code> 目錄中看到這些重要檔案和資料夾：</p>
<ul>
<li><code>mybook/_config.yml</code>：書籍的設定（標題、主題、logo 等）</li>
<li><code>mybook/_toc.yml</code>：書籍目錄（控制章節順序）</li>
<li><code>mybook/intro.md</code>、<code>mybook/chapters/*.ipynb</code>：實際內容，可新增 Markdown 或 Notebook 檔案</li>
</ul>
<section id="新增章節例子" class="level3">
<h3 class="anchored" data-anchor-id="新增章節例子">新增章節例子：</h3>
<ol type="1">
<li>在 <code>mybook/</code> 下新增一個檔案，例如 <code>chapter1.md</code></li>
<li>在 <code>_toc.yml</code> 中加上該檔案的設定：</li>
</ol>
<div class="sourceCode" id="cb3" style="background: #f1f3f5;"><pre class="sourceCode yaml code-with-copy"><code class="sourceCode yaml"><span id="cb3-1"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">format</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> jb-book</span></span>
<span id="cb3-2"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">root</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> intro</span></span>
<span id="cb3-3"><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">chapters</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span></span>
<span id="cb3-4"><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">  </span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">-</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> </span><span class="fu" style="color: #4758AB;
background-color: null;
font-style: inherit;">file</span><span class="kw" style="color: #003B4F;
background-color: null;
font-weight: bold;
font-style: inherit;">:</span><span class="at" style="color: #657422;
background-color: null;
font-style: inherit;"> chapter1</span></span></code></pre></div>
<hr>
</section>
</section>
<section id="編譯網站" class="level2">
<h2 class="anchored" data-anchor-id="編譯網站">編譯網站</h2>
<p>在書籍資料夾中執行：</p>
<div class="sourceCode" id="cb4" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb4-1"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">jupyter-book</span> build mybook/</span></code></pre></div>
<p>這會自動產生靜態網站，輸出目錄為：</p>
<pre><code>mybook/_build/html/</code></pre>
<hr>
</section>
<section id="預覽網站" class="level2">
<h2 class="anchored" data-anchor-id="預覽網站">預覽網站</h2>
<p>使用瀏覽器打開以下檔案即可：</p>
<pre><code>mybook/_build/html/index.html</code></pre>
<p>或用 Python 的 HTTP server 預覽：</p>
<div class="sourceCode" id="cb7" style="background: #f1f3f5;"><pre class="sourceCode bash code-with-copy"><code class="sourceCode bash"><span id="cb7-1"><span class="bu" style="color: null;
background-color: null;
font-style: inherit;">cd</span> mybook/_build/html</span>
<span id="cb7-2"><span class="ex" style="color: null;
background-color: null;
font-style: inherit;">python</span> <span class="at" style="color: #657422;
background-color: null;
font-style: inherit;">-m</span> http.server</span></code></pre></div>
<p>然後打開瀏覽器到 <a href="http://localhost:8000">http://localhost:8000</a></p>
<blockquote class="blockquote">
<ol type="1">
<li>注意：如果你有修改 <code>_toc.yml</code> 或其他設定，必須重新編譯網站才能看到變更，不然會顯示舊的內容。</li>
<li>如果編譯網站後有做<strong>刪除頁面</strong>的動作，請到 <code>_build/html/</code> 資料夾刪除對應的 html，不然他不會蓋到<del>很笨我知道</del>。</li>
<li>執行後 terminal 的 powershell 非必要請不要關閉，因為它在運行 HTTP server。需要關閉可以在 terminal 視窗 輸入 <code>Ctrl + C</code> 停止服務。或是另開新的 terminal 視窗來執行其他命令。</li>
</ol>
</blockquote>
<hr>
</section>
<section id="發布網站選擇性" class="level2">
<h2 class="anchored" data-anchor-id="發布網站選擇性">發布網站（選擇性）</h2>
<p>可以把 <code>_build/html</code> 上傳到 GitHub Pages、Netlify、Vercel 等平台，或用 GitHub Actions 自動部署。</p>



</section>
</section>

<div class="quarto-listing quarto-listing-container-default" id="listing-listing">
<div class="list quarto-listing-default">

</div>
<div class="listing-no-matching d-none">無符合的項目</div>
</div> ]]></description>
  <category>other</category>
  <category>python</category>
  <guid>https://paperfishblog.netlify.app/posts/other/jpyterbook/</guid>
  <pubDate>Thu, 24 Jul 2025 16:00:00 GMT</pubDate>
  <media:content url="https://paperfishblog.netlify.app/posts/other/jpyterbook/image.png" medium="image" type="image/png" height="81" width="144"/>
</item>
</channel>
</rss>
