{"id":2044,"date":"2023-07-23T20:40:40","date_gmt":"2023-07-23T12:40:40","guid":{"rendered":"http:\/\/xinyixx.com\/?p=2044"},"modified":"2023-07-23T20:41:30","modified_gmt":"2023-07-23T12:41:30","slug":"python25","status":"publish","type":"post","link":"https:\/\/www.xinyixx.com\/index.php\/2023\/07\/23\/python25\/","title":{"rendered":"python\u7a0b\u5e8f25\uff1a\u5199\u4e00\u4e2a\u5e94\u7528\u5b9e\u4f8b&#8211;\u4e07\u5e74\u5386"},"content":{"rendered":"<p>\u4eca\u5929\u5199\u4e00\u4e2apython\u7684\u5e94\u7528\u5b9e\u4f8b\uff0c\u7528\u5230\u4e86python\u7684\u4e09\u4e2a\u6a21\u5757&#8211;tkinter\u3001datetime\u3001calendar\uff0ctkinter\u8fd9\u4e2a\u6a21\u5757\u4e4b\u524d\u7684\u6559\u7a0b\u4ecb\u7ecd\u8fc7\u8fd9\u91cc\u5c31\u4e0d\u8fc7\u591a\u8d58\u8ff0\uff0c\u7b80\u5355\u8bf4\u4e00\u4e0b\u53e6\u5916\u4e24\u4e2a\u9ed8\u8ba4\u6a21\u5757\u3002<\/p>\n\n\n\n<p>datetime\u5b9a\u4e49\u4e86\u51e0\u4e2a\u7c7b\u6765\u8868\u793a\u65e5\u671f\u3001\u65f6\u95f4\u548c\u65f6\u95f4\u95f4\u9694\u3002\u5176\u4e2d\u6700\u91cd\u8981\u7684\u7c7b\u6709\uff1a<\/p>\n\n\n\n<p>datetime.date\uff1a\u8868\u793a\u65e5\u671f\u7684\u7c7b\uff0c\u5305\u62ec\u5e74\u3001\u6708\u3001\u65e5\u3002<br>datetime.time\uff1a\u8868\u793a\u65f6\u95f4\u7684\u7c7b\uff0c\u5305\u62ec\u65f6\u3001\u5206\u3001\u79d2\u3001\u6beb\u79d2\u3002<br>datetime.datetime\uff1a\u8868\u793a\u65e5\u671f\u548c\u65f6\u95f4\u7684\u7c7b\uff0c\u5305\u62ec\u5e74\u3001\u6708\u3001\u65e5\u3001\u65f6\u3001\u5206\u3001\u79d2\u3001\u6beb\u79d2\u3002<br>datetime.timedelta\uff1a\u8868\u793a\u65f6\u95f4\u95f4\u9694\u7684\u7c7b\uff0c\u53ef\u4ee5\u7528\u6765\u8fdb\u884c\u65f6\u95f4\u7684\u52a0\u51cf\u8fd0\u7b97\u3002<br>\u8fd9\u4e9b\u7c7b\u90fd\u6709\u5404\u81ea\u7684\u6784\u9020\u51fd\u6570\u548c\u5c5e\u6027\uff0c\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8fdb\u884c\u4f7f\u7528\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528 datetime.datetime.now() \u83b7\u53d6\u5f53\u524d\u65e5\u671f\u548c\u65f6\u95f4\uff0c\u6216\u8005\u4f7f\u7528 datetime.timedelta(days=1) \u521b\u5efa\u4e00\u4e2a\u4ee3\u8868\u4e00\u5929\u65f6\u95f4\u95f4\u9694\u7684\u5b9e\u4f8b\u3002<\/p>\n\n\n\n<p><code>calendar<\/code> \u6a21\u5757\u662f Python \u4e2d\u5904\u7406\u65e5\u5386\u7684\u6807\u51c6\u5e93\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u4e9b\u51fd\u6570\u548c\u7c7b\u6765\u5904\u7406\u65e5\u5386\u548c\u65e5\u671f\u3002\u5176\u4e2d\u6700\u5e38\u7528\u7684\u51fd\u6570\u6709\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>calendar.month(year, month, w=0, l=0)<\/code>\uff1a\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u8868\u793a\u7ed9\u5b9a\u5e74\u4efd\u548c\u6708\u4efd\u7684\u65e5\u5386\u3002<\/li>\n\n\n\n<li><code>calendar.calendar(year, w=2, l=1, c=6)<\/code>\uff1a\u8fd4\u56de\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u8868\u793a\u7ed9\u5b9a\u5e74\u4efd\u7684\u65e5\u5386\u3002<\/li>\n\n\n\n<li><code>calendar.weekday(year, month, day)<\/code>\uff1a\u8fd4\u56de\u7ed9\u5b9a\u65e5\u671f\u662f\u661f\u671f\u51e0\uff080 \u8868\u793a\u661f\u671f\u4e00\uff0c6 \u8868\u793a\u661f\u671f\u65e5\uff09\u3002<\/li>\n\n\n\n<li><code>calendar.isleap(year)<\/code>\uff1a\u8fd4\u56de\u7ed9\u5b9a\u5e74\u4efd\u662f\u5426\u4e3a\u95f0\u5e74\u3002<\/li>\n\n\n\n<li><code>calendar.leapdays(y1, y2)<\/code>\uff1a\u8fd4\u56de\u5728\u7ed9\u5b9a\u5e74\u4efd\u8303\u56f4\u5185\u7684\u95f0\u5e74\u6570\u91cf\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u6b64\u5916\uff0c<code>calendar<\/code> \u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u7c7b\uff0c\u5982 <code>calendar.TextCalendar<\/code> \u548c <code>calendar.HTMLCalendar<\/code>\uff0c\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u6587\u672c\u6216 HTML \u683c\u5f0f\u7684\u65e5\u5386\u3002\u8fd9\u4e9b\u7c7b\u53ef\u4ee5\u81ea\u5b9a\u4e49\u8f93\u51fa\u683c\u5f0f\uff0c\u5e76\u652f\u6301\u591a\u79cd\u8bed\u8a00\u3002<\/p>\n\n\n\n<p>\u8fd9\u91cc\u540c\u6837\u9700\u8981\u5148\u5b89\u88c5\u6a21\u5757\uff1a<code>pip install tkinter<\/code><\/p>\n\n\n\n<p>\u4ee5\u4e0b\u4e3a\u4e07\u5e74\u5386\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import tkinter as tk<br>import datetime<br>import calendar<br><br>class CalendarApp:<br>    def __init__(self):<br>        # \u521b\u5efa\u4e3b\u7a97\u53e3<br>        self.root = tk.Tk()<br>        self.root.title('\u4e07\u5e74\u5386')<br><br>        # \u521b\u5efa\u65e5\u671f\u6807\u7b7e<br>        self.date_label = tk.Label(self.root, text='', font=('Arial', 20))<br>        self.date_label.pack(pady=20)<br><br>        # \u521b\u5efa\u65e5\u5386\u8868\u683c<br>        self.table = tk.Frame(self.root)<br>        self.table.pack(pady=20)<br><br>        # \u521b\u5efa\u65e5\u671f\u9009\u62e9\u6846<br>        self.year_var = tk.StringVar()<br>        self.year_var.set(str(datetime.datetime.now().year))<br>        self.month_var = tk.StringVar()<br>        self.month_var.set(str(datetime.datetime.now().month))<br>        self.year_entry = tk.Entry(self.root, textvariable=self.year_var, font=('Arial', 16), width=6)<br>        self.year_entry.pack(side=tk.LEFT, padx=10)<br>        self.month_entry = tk.Entry(self.root, textvariable=self.month_var, font=('Arial', 16), width=4)<br>        self.month_entry.pack(side=tk.LEFT)<br>        self.go_button = tk.Button(self.root, text='Go', font=('Arial', 16), command=self.go)<br>        self.go_button.pack(side=tk.LEFT, padx=10)<br><br>        # \u663e\u793a\u65e5\u5386<br>        self.show_calendar()<br><br>    def show_calendar(self):<br>        # \u6e05\u7a7a\u65e5\u5386\u8868\u683c<br>        for widget in self.table.winfo_children():<br>            widget.destroy()<br><br>        # \u83b7\u53d6\u5f53\u524d\u6708\u4efd\u7684\u65e5\u5386<br>        cal = calendar.monthcalendar(int(self.year_var.get()), int(self.month_var.get()))<br><br>        # \u6dfb\u52a0\u8868\u5934<br>        headers = ['Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa', 'Su']<br>        for i in range(7):<br>            label = tk.Label(self.table, text=headers[i], font=('Arial', 12))<br>            label.grid(row=0, column=i, padx=5, pady=5)<br><br>        # \u6dfb\u52a0\u65e5\u671f<br>        for i in range(len(cal)):<br>            for j in range(7):<br>                day = cal[i][j]<br>                if day != 0:<br>                    button = tk.Button(self.table, text=str(day), font=('Arial', 12), width=3, height=2)<br>                    button.grid(row=i+1, column=j, padx=5, pady=5)<br>                    button.bind('&lt;Button-1&gt;', lambda event, date=datetime.date(int(self.year_var.get()), int(self.month_var.get()), day): self.show_memo(event, date))<br><br>        # \u66f4\u65b0\u65e5\u671f\u6807\u7b7e<br>        self.date_label.config(text=f'{self.year_var.get()}\u5e74{self.month_var.get()}\u6708')<br><br>    def show_memo(self, event, date):<br>        # \u521b\u5efa\u5907\u5fd8\u5f55\u7a97\u53e3<br>        memo_window = tk.Toplevel(self.root)<br>        memo_window.title(f'{date}\u5907\u5fd8\u5f55')<br><br>        # \u521b\u5efa\u5907\u5fd8\u5f55\u6587\u672c\u6846<br>        memo_text = tk.Text(memo_window, font=('Arial', 16))<br>        memo_text.pack(padx=20, pady=20)<br><br>        # \u52a0\u8f7d\u5907\u5fd8\u5f55\u5185\u5bb9<br>        try:<br>            with open(f'{date}.txt', 'r') as f:<br>                memo_text.insert(tk.END, f.read())<br>        except FileNotFoundError:<br>            pass<br><br>        # \u521b\u5efa\u4fdd\u5b58\u6309\u94ae<br>        save_button = tk.Button(memo_window, text='\u4fdd\u5b58', font=('Arial', 16), command=lambda: self.save_memo(date, memo_text.get('1.0', tk.END)))<br>        save_button.pack(pady=20)<br><br>    def save_memo(self, date, content):<br>        # \u4fdd\u5b58\u5907\u5fd8\u5f55\u5185\u5bb9<br>        with open(f'{date}.txt', 'w') as f:<br>            f.write(content)<br><br>    def go(self):<br>        # \u663e\u793a\u6307\u5b9a\u6708\u4efd\u7684\u65e5\u5386<br>        self.show_calendar()<br><br>    def run(self):<br>        # \u8fd0\u884c\u7a0b\u5e8f<br>        self.root.mainloop()<br><br>if __name__ == '__main__':<br>    app = CalendarApp()<br>    app.run()<\/pre>\n\n\n\n<p>\u6309Ctrl+Shift+F10\u8fd0\u884c\u4e00\u4e0b\uff0c<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"423\" height=\"637\" src=\"http:\/\/xinyixx.com\/wp-content\/uploads\/2023\/07\/image-174.png\" alt class=\"wp-image-2048\" srcset=\"https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/07\/image-174.png 423w, https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/07\/image-174-199x300.png 199w\" sizes=\"(max-width: 423px) 100vw, 423px\"><\/noscript><img decoding=\"async\" width=\"423\" height=\"637\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20423%20637%22%3E%3C%2Fsvg%3E\" alt class=\"wp-image-2048 lazyload\" srcset=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20423%20637%22%3E%3C%2Fsvg%3E 423w\" sizes=\"(max-width: 423px) 100vw, 423px\" data-srcset=\"https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/07\/image-174.png 423w, https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/07\/image-174-199x300.png 199w\" data-src=\"http:\/\/xinyixx.com\/wp-content\/uploads\/2023\/07\/image-174.png\"><\/figure>\n\n\n\n<p>\u5728\u6587\u672c\u6846\u5185\u8f93\u5165\u65e5\u671f\u548c\u6708\u4efd\u540e\u70b9\u51fbGo\u5373\u53ef\u8df3\u8f6c\u5230\u8be5\u6708\u4efd\uff0c\u8fd9\u91cc\u6700\u591a\u80fd\u591f\u4ece\u516c\u5143\u5143\u5e74\u52309999\u5e74\u4efb\u610f\u4e00\u4e2a\u6708\u4efd\uff0c\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u771f\u6b63\u5b9e\u73b0\u4e86\u4e07\u5e74\u5386\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"377\" height=\"596\" src=\"http:\/\/xinyixx.com\/wp-content\/uploads\/2023\/07\/image-176.png\" alt class=\"wp-image-2050\" srcset=\"https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/07\/image-176.png 377w, https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/07\/image-176-190x300.png 190w\" sizes=\"(max-width: 377px) 100vw, 377px\"><\/noscript><img decoding=\"async\" width=\"377\" height=\"596\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20377%20596%22%3E%3C%2Fsvg%3E\" alt class=\"wp-image-2050 lazyload\" srcset=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20377%20596%22%3E%3C%2Fsvg%3E 377w\" sizes=\"(max-width: 377px) 100vw, 377px\" data-srcset=\"https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/07\/image-176.png 377w, https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/07\/image-176-190x300.png 190w\" data-src=\"http:\/\/xinyixx.com\/wp-content\/uploads\/2023\/07\/image-176.png\"><\/figure>\n\n\n\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"402\" height=\"575\" src=\"http:\/\/xinyixx.com\/wp-content\/uploads\/2023\/07\/image-175.png\" alt class=\"wp-image-2049\" srcset=\"https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/07\/image-175.png 402w, https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/07\/image-175-210x300.png 210w\" sizes=\"(max-width: 402px) 100vw, 402px\"><\/noscript><img decoding=\"async\" width=\"402\" height=\"575\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20402%20575%22%3E%3C%2Fsvg%3E\" alt class=\"wp-image-2049 lazyload\" srcset=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20402%20575%22%3E%3C%2Fsvg%3E 402w\" sizes=\"(max-width: 402px) 100vw, 402px\" data-srcset=\"https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/07\/image-175.png 402w, https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/07\/image-175-210x300.png 210w\" data-src=\"http:\/\/xinyixx.com\/wp-content\/uploads\/2023\/07\/image-175.png\"><\/figure>\n\n\n\n<p>\u70b9\u51fb\u67d0\u4e00\u4e2a\u65e5\u671f\uff0c\u80fd\u591f\u6dfb\u52a0\u5907\u5fd8\u5f55\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"565\" height=\"682\" src=\"http:\/\/xinyixx.com\/wp-content\/uploads\/2023\/07\/image-177.png\" alt class=\"wp-image-2051\" srcset=\"https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/07\/image-177.png 565w, https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/07\/image-177-249x300.png 249w\" sizes=\"(max-width: 565px) 100vw, 565px\"><\/noscript><img decoding=\"async\" width=\"565\" height=\"682\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20565%20682%22%3E%3C%2Fsvg%3E\" alt class=\"wp-image-2051 lazyload\" srcset=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%20565%20682%22%3E%3C%2Fsvg%3E 565w\" sizes=\"(max-width: 565px) 100vw, 565px\" data-srcset=\"https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/07\/image-177.png 565w, https:\/\/www.xinyixx.com\/wp-content\/uploads\/2023\/07\/image-177-249x300.png 249w\" data-src=\"http:\/\/xinyixx.com\/wp-content\/uploads\/2023\/07\/image-177.png\"><\/figure>\n\n\n\n<p>\u611f\u5174\u8da3\u7684\u540c\u5b66\u5728IDE\u91cc\u5c1d\u8bd5\u4e00\u4e0b\u5427\u3002<monica-translate-wrapper><monica-translate class=\"monica-translate-loading-spinner\" style=\"display: block; margin: 8px 0px;\"><\/monica-translate><\/monica-translate-wrapper><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4eca\u5929\u5199\u4e00\u4e2apython\u7684\u5e94\u7528\u5b9e\u4f8b\uff0c\u7528\u5230\u4e86python\u7684\u4e09\u4e2a\u6a21\u5757&#8211;tkinter\u3001datetime\u3001 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_import_markdown_pro_load_document_selector":0,"_import_markdown_pro_submit_text_textarea":"","footnotes":""},"categories":[14,10,7],"tags":[71,69,77],"class_list":["post-2044","post","type-post","status-publish","format-standard","hentry","category-teacher","category-coding","category-software","tag-python","tag-learning","tag-coding","entry"],"_links":{"self":[{"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/posts\/2044","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/comments?post=2044"}],"version-history":[{"count":0,"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/posts\/2044\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/media?parent=2044"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/categories?post=2044"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xinyixx.com\/index.php\/wp-json\/wp\/v2\/tags?post=2044"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}