Openai/6971ad15-bc24-8001-895c-a3da27712d3a


This conversation was exported from ChatGPT[1]

gpt-5-mini edit

User: 用 python 寫桌面應用,顯示月曆 edit

用 python 寫桌面應用,顯示月曆

Assistant: 明白了,你想用 Python 做一個桌面應用來顯示月曆。我可以給你一個完整示例,使用 Tkinter(Python 內建的 GUI 庫)結合 calendar 模組來生成月曆。這個應用可以選擇年份和月份,並在窗口中顯示對應的月曆。 edit

明白了,你想用 Python 做一個桌面應用來顯示月曆。我可以給你一個完整示例,使用 Tkinter(Python 內建的 GUI 庫)結合 calendar 模組來生成月曆。這個應用可以選擇年份和月份,並在窗口中顯示對應的月曆。

下面是一個完整範例:

import tkinter as tk
from tkinter import ttk
import calendar

def show_calendar():
    year = int(year_entry.get())
    month = int(month_combo.get())
    
    # 清空之前的月曆
    cal_text.delete('1.0', tk.END)
    
    # 生成月曆
    cal = calendar.month(year, month)
    cal_text.insert(tk.END, cal)

=== 建立主窗口 ===
root = tk.Tk()
root.title("月曆顯示器")
root.geometry("400x400")

=== 年份輸入 ===
tk.Label(root, text="年份:").pack(pady=5)
year_entry = tk.Entry(root)
year_entry.pack()
year_entry.insert(0, "2026")  # 預設年份

=== 月份選擇 ===
tk.Label(root, text="月份:").pack(pady=5)
month_combo = ttk.Combobox(root, values=list(range(1, 13)))
month_combo.pack()
month_combo.current(0)  # 預設 1 月

=== 顯示月曆按鈕 ===
tk.Button(root, text="顯示月曆", command=show_calendar).pack(pady=10)

=== 月曆文字框 ===
cal_text = tk.Text(root, width=30, height=10, font=("Courier", 12))
cal_text.pack(pady=10)

root.mainloop()
功能說明: edit
  1. 使用 Entry 輸入年份。
  2. 使用 Combobox 選擇月份。
  3. 按下按鈕就會生成對應月份的文字月曆,顯示在 Text 小工具中。
  4. 字體使用等寬字體 "Courier",這樣月曆排列會整齊。

如果你願意,我可以幫你升級這個版本,做成 漂亮的圖形化月曆(像 Windows 日曆那樣,每天一格,點擊還能做標記),而不是純文字版。

你希望我做這個升級嗎?

References edit

  1. "Python 月曆桌面應用". ChatGPT. Retrieved 2026-01-23.