{"id":3816,"date":"2023-11-06T05:56:56","date_gmt":"2023-11-05T21:56:56","guid":{"rendered":"http:\/\/cnliutz.ipyingshe.net\/?p=3816"},"modified":"2023-11-06T07:25:42","modified_gmt":"2023-11-05T23:25:42","slug":"3816","status":"publish","type":"post","link":"http:\/\/cnliutz.wicp.vip\/?p=3816","title":{"rendered":"Python \u7edf\u8ba1\u8bcd\u9891gui"},"content":{"rendered":"\n<p>from tkinter import Tk, Label, Entry, Button, Canvas, Scrollbar<br>from collections import defaultdict<br>import matplotlib.pyplot as plt<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">#\u7edf\u8ba1\u8bcd\u9891<\/h1>\n\n\n\n<p>def count_words(text):<br>words = text.split()<br>word_freq = defaultdict(int)<br>for word in words:<br>word_freq[word] += 1<br>return word_freq<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">#\u7ed8\u5236\u8bcd\u9891\u56fe\u5f62<\/h1>\n\n\n\n<p>def plot_word_freq(word_freq):<br>plt.bar(word_freq.keys(), word_freq.values())<br>plt.xlabel(&#8216;Words&#8217;)<br>plt.ylabel(&#8216;Frequency&#8217;)<br>plt.title(&#8216;Word Frequency&#8217;)<br>plt.show()<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">#\u521b\u5efa\u4e3b\u7a97\u53e3<\/h1>\n\n\n\n<p>root = Tk()<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">#\u521b\u5efa\u6807\u7b7e\u548c\u8f93\u5165\u6846<\/h1>\n\n\n\n<p>label_text = Label(root, text=&#8217;\u8bf7\u8f93\u5165\u6587\u672c\uff1a&#8217;)<br>entry_text = Entry(root)<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">#\u521b\u5efa\u6309\u94ae<\/h1>\n\n\n\n<p>button_count = Button(root, text=&#8217;\u7edf\u8ba1\u8bcd\u9891&#8217;, command=lambda: count_words(entry_text.get()))<br>button_plot = Button(root, text=&#8217;\u7ed8\u5236\u56fe\u5f62&#8217;, command=lambda: plot_word_freq(count_words(entry_text.get())))<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">#\u521b\u5efa\u753b\u5e03\u548c\u6eda\u52a8\u6761<\/h1>\n\n\n\n<p>canvas = Canvas(root, width=800, height=600)<br>scrollbar = Scrollbar(root, orient=&#8217;vertical&#8217;, command=canvas.yview)<br><br>#\u5728\u753b\u5e03\u4e2d\u6dfb\u52a0\u6587\u672c\u6807\u7b7e\u3001\u8f93\u5165\u6846\u3001\u4e24\u4e2a\u6309\u94ae<br>label_text.pack()<br>entry_text.pack()<br>button_count.pack()<br>button_plot.pack()<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">#\u5c06\u753b\u5e03\u548c\u6eda\u52a8\u6761\u7ec4\u5408\u5728\u4e00\u8d77<\/h1>\n\n\n\n<p>canvas.configure(yscrollcommand=scrollbar.set)<br>scrollbar.pack(side=&#8217;right&#8217;, fill=&#8217;y&#8217;)<br>canvas.pack()<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">#\u4e3b\u5faa\u73af<\/h1>\n\n\n\n<p>root.mainloop()<\/p>\n","protected":false},"excerpt":{"rendered":"<p>from tkinter import Tk, Label, Entry, Bu <span class=\"readmore\"><a href=\"http:\/\/cnliutz.wicp.vip\/?p=3816\">Continue Reading<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-3816","post","type-post","status-publish","format-standard","hentry","category-2"],"_links":{"self":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/3816","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3816"}],"version-history":[{"count":3,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/3816\/revisions"}],"predecessor-version":[{"id":3820,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/3816\/revisions\/3820"}],"wp:attachment":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3816"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3816"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3816"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}