{"id":1669,"date":"2021-03-25T07:25:41","date_gmt":"2021-03-24T23:25:41","guid":{"rendered":"http:\/\/cnliutz.uicp.io\/?p=1669"},"modified":"2021-03-25T07:25:41","modified_gmt":"2021-03-24T23:25:41","slug":"%e8%ae%a1%e7%ae%97%e5%99%a8tkinter%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"http:\/\/xnw60rlg.ipyingshe.net:5347\/?p=1669","title":{"rendered":"\u8ba1\u7b97\u5668tkinter\u4ee3\u7801"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#filename:Caculater\r\n import tkinter,time,decimal,math,string\r\n \r\n root=tkinter.Tk()\r\n root.title('\u8ba1\u7b97\u5668')\r\n root.resizable(0,0)\r\n global cuncu, vartext, result, fuhao\r\n result = fuhao = None\r\n vartext = tkinter.StringVar()\r\n cuncu = &#91;]\r\n \r\n class anjianzhi:\r\n     global cuncu, vartext, result, fuhao\r\n     def __init__(self,anjian):\r\n         self.anjian = anjian\r\n     def jia(self):\r\n         cuncu.append(self.anjian)\r\n         vartext.set( ''.join(cuncu))\r\n     def tui(self):\r\n         cuncu.pop()\r\n         vartext.set(''.join(cuncu))\r\n     def clear(self):\r\n         cuncu.clear()\r\n         vartext.set('')\r\n         result = None\r\n         fuhao = None\r\n     def zhengfu(self):\r\n         if cuncu&#91;0]:\r\n             if cuncu&#91;0] == '-':\r\n                 cuncu&#91;0] = '+'\r\n             elif cuncu&#91;0] == '+':\r\n                 cuncu&#91;0] = '-'\r\n             else:\r\n                 cuncu.insert(0, '-')\r\n         vartext.set(''.join(cuncu))\r\n     def xiaoshudian(self):\r\n         if cuncu.count('.') >= 1:\r\n             pass\r\n         else:\r\n             if cuncu == &#91;] :\r\n                 cuncu.append('0')\r\n             cuncu.append('.')\r\n             vartext.set(''.join(cuncu))\r\n     def yunshuan(self):\r\n         global cuncu, vartext, result, fuhao\r\n         if vartext.get() == '':\r\n             pass\r\n         else:\r\n             get1 = decimal.Decimal(vartext.get())\r\n             if self.anjian in ('1\/x','sqrt'):\r\n                 if self.anjian == '1\/x':\r\n                     result = 1\/get1\r\n                 elif self.anjian == 'sqrt':\r\n                     result = math.sqrt(get1)\r\n             elif  self.anjian in ('+','-','*','\/','='):\r\n                 if fuhao is not None:\r\n                     get1 = decimal.Decimal(result)\r\n                     get2 = decimal.Decimal(vartext.get())\r\n                     if fuhao == '+':\r\n                         result = get1 + get2\r\n                     elif fuhao == '-':\r\n                         result = get1 - get2\r\n                     elif fuhao == '*':\r\n                         result = get1 * get2\r\n                     elif fuhao == '\/':\r\n                         result = get1 \/ get2\r\n                 else:\r\n                     result = get1\r\n                 if self.anjian == '=':\r\n                     fuhao = None\r\n                 else:\r\n                     fuhao = self.anjian\r\n             print(fuhao)\r\n             print(result)\r\n             vartext.set(str(result))\r\n             cuncu.clear()\r\n \r\n def copy1():\r\n     # tkinter.Misc().clipboard_clear()\r\n     tkinter.Misc().clipboard_append(string(vartext.get()))\r\n \r\n def buju(root):\r\n     global cuncu, vartext, result, fuhao\r\n    entry1 = tkinter.Label(root, width=30, height=2, bg='white', anchor='se', textvariable=vartext)\r\n     entry1.grid(row=0, columnspan=5)\r\n     buttonMC=tkinter.Button(root,text='MC',width=5)\r\n     buttonMR=tkinter.Button(root,text='MR',width=5)\r\n     buttonMS=tkinter.Button(root,text='MS',width=5)\r\n     buttonM1=tkinter.Button(root,text='M+',width=5)\r\n     buttonM2=tkinter.Button(root,text='M-',width=5)\r\n     buttonMC.grid(row=1,column=0)\r\n     buttonMR.grid(row=1,column=1)\r\n     buttonMS.grid(row=1,column=2)\r\n     buttonM1.grid(row=1,column=3)\r\n     buttonM2.grid(row=1,column=4)\r\n \r\n \r\n     buttonJ=tkinter.Button(root,text='\u2190',width=5,command=anjianzhi('c').tui)\r\n     buttonCE=tkinter.Button(root,text='CE',width=5)\r\n     buttonC=tkinter.Button(root,text=' C ',width=5,command=anjianzhi('c').clear)\r\n     button12=tkinter.Button(root,text='\u00b1',width=5,command=anjianzhi('c').zhengfu)\r\n     buttonD=tkinter.Button(root,text='\u221a',width=5,command=anjianzhi('sqrt').yunshuan)\r\n     buttonJ.grid(row=2,column=0)\r\n     buttonCE.grid(row=2,column=1)\r\n     buttonC.grid(row=2,column=2)\r\n     button12.grid(row=2,column=3)\r\n     buttonD.grid(row=2,column=4)\r\n \r\n     button7=tkinter.Button(root,text=' 7 ',width=5,command=anjianzhi('7').jia)\r\n     button8=tkinter.Button(root,text=' 8 ',width=5,command=anjianzhi('8').jia)\r\n     button9=tkinter.Button(root,text=' 9 ',width=5,command=anjianzhi('9').jia)\r\n     buttonc=tkinter.Button(root, text=' \/ ',width=5,command=anjianzhi('\/').yunshuan)\r\n     buttonf= tkinter.Button(root, text=' % ',width=5)\r\n     button7.grid(row=3,column=0)\r\n     button8.grid(row=3,column=1)\r\n     button9.grid(row=3,column=2)\r\n     buttonc.grid(row=3,column=3)\r\n     buttonf.grid(row=3,column=4)\r\n \r\n     button4=tkinter.Button(root,text=' 4 ',width=5,command=anjianzhi('4').jia)\r\n     button5=tkinter.Button(root,text=' 5 ',width=5,command=anjianzhi('5').jia)\r\n     button6=tkinter.Button(root,text=' 6 ',width=5,command=anjianzhi('6').jia)\r\n     buttonx=tkinter.Button(root,text=' * ',width=5,command=anjianzhi('*').yunshuan)\r\n     buttonfs=tkinter.Button(root,text='1\/x',width=5,command=anjianzhi('1\/x').yunshuan)\r\n     button4.grid(row=4,column=0)\r\n     button5.grid(row=4,column=1)\r\n     button6.grid(row=4,column=2)\r\n     buttonx.grid(row=4,column=3)\r\n     buttonfs.grid(row=4,column=4)\r\n \r\n     button1 = tkinter.Button(root, text=' 1 ',width=5,command=anjianzhi('1').jia)\r\n     button2 = tkinter.Button(root, text=' 2 ',width=5,command=anjianzhi('2').jia)\r\n     button3 = tkinter.Button(root, text=' 3 ',width=5,command=anjianzhi('3').jia)\r\n     button_= tkinter.Button(root, text=' - ',width=5,command=anjianzhi('-').yunshuan)\r\n     buttondy= tkinter.Button(root, text=' \\n = \\n ',width=5,command=anjianzhi('=').yunshuan)\r\n     button1.grid(row=5, column=0)\r\n     button2.grid(row=5, column=1)\r\n     button3.grid(row=5, column=2)\r\n     button_.grid(row=5, column=3)\r\n     buttondy.grid(row=5, column=4,rowspan=2)\r\n \r\n     button0=tkinter.Button(root,text='   0   ',width=11,command=anjianzhi('0').jia)\r\n     buttonjh = tkinter.Button(root,text=' . ',width=5,command=anjianzhi('c').xiaoshudian)\r\n     buttonjia=tkinter.Button(root,text=' + ',width=5,command=anjianzhi('+').yunshuan)\r\n     button0.grid(row=6,column=0,columnspan=2)\r\n     buttonjh.grid(row=6,column=2)\r\n     buttonjia.grid(row=6,column=3)\r\n def caidan(root):\r\n \r\n     menu=tkinter.Menu(root)\r\n     submenu1=tkinter.Menu(menu,tearoff=0)\r\n     menu.add_cascade(label='\u67e5\u770b',menu=submenu1)\r\n     submenu2 = tkinter.Menu(menu, tearoff=0)\r\n     submenu2.add_command(label='\u590d\u5236')\r\n     submenu2.add_command(label='\u7c98\u8d34')\r\n     menu.add_cascade(label='\u7f16\u8f91',menu=submenu2)\r\n     submenu = tkinter.Menu(menu, tearoff=0)\r\n     submenu.add_command(label='\u67e5\u770b\u5e2e\u52a9')\r\n     submenu.add_separator()\r\n     submenu.add_command(label='\u5173\u4e8e\u8ba1\u7b97\u673a')\r\n     menu.add_cascade(label='\u5e2e\u52a9',menu=submenu)\r\n     root.config(menu=menu)\r\n \r\n \r\n buju(root)\r\n caidan(root)\r\n root.mainloop()\r\n\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-1669","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/1669","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1669"}],"version-history":[{"count":1,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/1669\/revisions"}],"predecessor-version":[{"id":1670,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=\/wp\/v2\/posts\/1669\/revisions\/1670"}],"wp:attachment":[{"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1669"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/xnw60rlg.ipyingshe.net:5347\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}