學(xué)了一周的Python,這篇文章算是為這段時(shí)間自學(xué)做的小總結(jié)。
創(chuàng)新互聯(lián)長(zhǎng)期為1000+客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開(kāi)放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為懷仁企業(yè)提供專業(yè)的成都做網(wǎng)站、成都網(wǎng)站制作,懷仁網(wǎng)站改版等技術(shù)服務(wù)。擁有十年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開(kāi)發(fā)。一、Python簡(jiǎn)介
Python是一門十分優(yōu)美的腳本語(yǔ)言,如果學(xué)過(guò)java、c++那入門Python是非常簡(jiǎn)單的。Python具有豐富和強(qiáng)大的類庫(kù)。它常被昵稱為膠水語(yǔ)言,它能夠很輕松的把用其他語(yǔ)言制作的各種模塊(尤其是C/C++)輕松地聯(lián)結(jié)在一起。常見(jiàn)的一種應(yīng)用情形是,使用Python快速生成程序的原型(有時(shí)甚至是程序的最終界面),然后對(duì)其中有特別要求的部分,用更合適的語(yǔ)言改寫(xiě)。
二、Python版計(jì)算器的實(shí)現(xiàn)
工具準(zhǔn)備:
1. Python2.7
2. wxPython
3. wxGlade界面編輯工具
4. pyCharm
具體配置什么的我就不說(shuō)了,網(wǎng)上有很多。Python的GUI開(kāi)發(fā)庫(kù)有很多,這里我們使用wxPython。用wxGlade編輯界面,wxGlade是一款很好用的GUI編輯工具,它大的優(yōu)點(diǎn)是能自動(dòng)生成編輯好的圖形界面的代碼。
這里主要介紹幾點(diǎn):
wx.TextCtrl(文本輸入控件),這個(gè)作為計(jì)算器的顯示屏,點(diǎn)擊數(shù)字時(shí)用AppendText方法將數(shù)字添加至文本末尾,要取出數(shù)字時(shí)用GetValue方法,點(diǎn)擊運(yùn)算符時(shí)要將文本框中的數(shù)字清空(先存儲(chǔ))用Clear方法然后還有將操作符保存,點(diǎn)擊等號(hào)后要將第二個(gè)操作數(shù)存儲(chǔ),然后用int方法將字符串轉(zhuǎn)換為數(shù)字,計(jì)算后用SetValue方法將結(jié)果送入文本框,具體代碼如下:
import wx # begin wxGlade: extracode # end wxGlade class MyFrame(wx.Frame): def __init__(self, *args, **kwds): # begin wxGlade: MyFrame.__init__ kwds["style"] = wx.DEFAULT_FRAME_STYLE wx.Frame.__init__(self, *args, **kwds) self.text_ctrl_1 = wx.TextCtrl(self, -1, "",style=wx.TE_READONLY) self.button_37 = wx.Button(self, -1, "1") self.button_38 = wx.Button(self, -1, "2") self.button_39 = wx.Button(self, -1, "3") self.button_40 = wx.Button(self, -1, "+") self.button_41 = wx.Button(self, -1, "4") self.button_42 = wx.Button(self, -1, "5") self.button_43 = wx.Button(self, -1, "6") self.button_44 = wx.Button(self, -1, "-") self.button_46 = wx.Button(self, -1, "7") self.button_45 = wx.Button(self, -1, "8") self.button_47 = wx.Button(self, -1, "9") self.button_48 = wx.Button(self, -1, "x") self.button_49 = wx.Button(self, -1, "C") self.button_50 = wx.Button(self, -1, "0") self.button_51 = wx.Button(self, -1, "=") self.button_52 = wx.Button(self, -1, "/") self.__set_properties() self.__do_layout() self.Bind(wx.EVT_BUTTON, self.bu1, self.button_37) self.Bind(wx.EVT_BUTTON, self.bu2, self.button_38) self.Bind(wx.EVT_BUTTON, self.bu3, self.button_39) self.Bind(wx.EVT_BUTTON, self.bu_plus, self.button_40) self.Bind(wx.EVT_BUTTON, self.bu4, self.button_41) self.Bind(wx.EVT_BUTTON, self.bu5, self.button_42) self.Bind(wx.EVT_BUTTON, self.bu6, self.button_43) self.Bind(wx.EVT_BUTTON, self.bu_min, self.button_44) self.Bind(wx.EVT_BUTTON, self.bu7, self.button_46) self.Bind(wx.EVT_BUTTON, self.bu8, self.button_45) self.Bind(wx.EVT_BUTTON, self.bu9, self.button_47) self.Bind(wx.EVT_BUTTON, self.bu_mul, self.button_48) self.Bind(wx.EVT_BUTTON, self.bu_clear, self.button_49) self.Bind(wx.EVT_BUTTON, self.bu0, self.button_50) self.Bind(wx.EVT_BUTTON, self.bu_result, self.button_51) self.Bind(wx.EVT_BUTTON, self.bu_chu, self.button_52) # end wxGlade self.Show(True) def __set_properties(self): # begin wxGlade: MyFrame.__set_properties self.SetTitle("Python Calculater by CYG") self.text_ctrl_1.SetMinSize((400, 50)) self.button_37.SetMinSize((100, 50)) self.button_38.SetMinSize((100, 50)) self.button_39.SetMinSize((100, 50)) self.button_40.SetMinSize((100, 50)) self.button_41.SetMinSize((100, 50)) self.button_42.SetMinSize((100, 50)) self.button_43.SetMinSize((100, 50)) self.button_44.SetMinSize((100, 50)) self.button_46.SetMinSize((100, 50)) self.button_45.SetMinSize((100, 50)) self.button_47.SetMinSize((100, 50)) self.button_48.SetMinSize((100, 50)) self.button_49.SetMinSize((100, 50)) self.button_50.SetMinSize((100, 50)) self.button_51.SetMinSize((100, 50)) self.button_52.SetMinSize((100, 50)) # end wxGlade def __do_layout(self): # begin wxGlade: MyFrame.__do_layout sizer_2 = wx.BoxSizer(wx.VERTICAL) sizer_3 = wx.BoxSizer(wx.VERTICAL) grid_sizer_1 = wx.GridSizer(4, 4, 0, 0) sizer_3.Add(self.text_ctrl_1, 0, 0, 0) grid_sizer_1.Add(self.button_37, 0, 0, 0) grid_sizer_1.Add(self.button_38, 0, 0, 0) grid_sizer_1.Add(self.button_39, 0, 0, 0) grid_sizer_1.Add(self.button_40, 0, 0, 0) grid_sizer_1.Add(self.button_41, 0, 0, 0) grid_sizer_1.Add(self.button_42, 0, 0, 0) grid_sizer_1.Add(self.button_43, 0, 0, 0) grid_sizer_1.Add(self.button_44, 0, 0, 0) grid_sizer_1.Add(self.button_46, 0, 0, 0) grid_sizer_1.Add(self.button_45, 0, 0, 0) grid_sizer_1.Add(self.button_47, 0, 0, 0) grid_sizer_1.Add(self.button_48, 0, 0, 0) grid_sizer_1.Add(self.button_49, 0, 0, 0) grid_sizer_1.Add(self.button_50, 0, 0, 0) grid_sizer_1.Add(self.button_51, 0, 0, 0) grid_sizer_1.Add(self.button_52, 0, 0, 0) sizer_3.Add(grid_sizer_1, 1, wx.EXPAND, 0) sizer_2.Add(sizer_3, 1, wx.EXPAND, 0) self.SetSizer(sizer_2) sizer_2.Fit(self) self.Layout() # end wxGlade def bu1(self, event): # wxGlade: MyFrame.<event_handler> self.text_ctrl_1.AppendText("1") event.Skip() def bu2(self, event): # wxGlade: MyFrame.<event_handler> self.text_ctrl_1.AppendText("2") event.Skip() def bu3(self, event): # wxGlade: MyFrame.<event_handler> self.text_ctrl_1.AppendText("3") event.Skip() def bu_plus(self, event): # wxGlade: MyFrame.<event_handler> self.num1=self.text_ctrl_1.GetValue() self.op="+" self.text_ctrl_1.Clear() event.Skip() def bu4(self, event): # wxGlade: MyFrame.<event_handler> self.text_ctrl_1.AppendText("4") event.Skip() def bu5(self, event): # wxGlade: MyFrame.<event_handler> self.text_ctrl_1.AppendText("5") event.Skip() def bu6(self, event): # wxGlade: MyFrame.<event_handler> self.text_ctrl_1.AppendText("6") event.Skip() def bu_min(self, event): # wxGlade: MyFrame.<event_handler> self.num1=self.text_ctrl_1.GetValue() self.op="-" self.text_ctrl_1.Clear() event.Skip() def bu7(self, event): # wxGlade: MyFrame.<event_handler> self.text_ctrl_1.AppendText("7") event.Skip() def bu8(self, event): # wxGlade: MyFrame.<event_handler> self.text_ctrl_1.AppendText("8") event.Skip() def bu9(self, event): # wxGlade: MyFrame.<event_handler> self.text_ctrl_1.AppendText("9") event.Skip() def bu_mul(self, event): # wxGlade: MyFrame.<event_handler> self.num1=self.text_ctrl_1.GetValue() self.op="x" self.text_ctrl_1.Clear() event.Skip() def bu_clear(self, event): # wxGlade: MyFrame.<event_handler> self.text_ctrl_1.Clear() event.Skip() def bu0(self, event): # wxGlade: MyFrame.<event_handler> self.text_ctrl_1.AppendText("0") event.Skip() def bu_chu(self, event): # wxGlade: MyFrame.<event_handler> self.num1=self.text_ctrl_1.GetValue() self.op="/" self.text_ctrl_1.Clear() event.Skip() def bu_result(self, event): # wxGlade: MyFrame.<event_handler> self.num2=self.text_ctrl_1.GetValue() num1=int(self.num1) num2=int(self.num2) if self.op=="+": self.text_ctrl_1.SetValue(str(num1+num2)) elif self.op=="-": self.text_ctrl_1.SetValue(str(num1-num2)) elif self.op=="x": self.text_ctrl_1.SetValue(str(num1*num2)) elif self.op=="/": self.text_ctrl_1.SetValue(str(num1/num2)) event.Skip() # end of class MyFrame if __name__ == "__main__": app=wx.App(False) myframe=MyFrame(None) app.MainLoop()
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前文章:python實(shí)現(xiàn)簡(jiǎn)易版計(jì)算器-創(chuàng)新互聯(lián)
當(dāng)前路徑:http://www.rwnh.cn/article8/cecsip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、品牌網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航、搜索引擎優(yōu)化、外貿(mào)建站、網(wǎng)站排名
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容