1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        手把手教你使用PySimpleGUI庫打造一款輕量級(jí)計(jì)算器

        共 4385字,需瀏覽 9分鐘

         ·

        2022-01-26 13:18

        點(diǎn)擊上方“Python爬蟲與數(shù)據(jù)挖掘”,進(jìn)行關(guān)注

        回復(fù)“書籍”即可獲贈(zèng)Python從入門到進(jìn)階共10本電子書

        天街小雨潤如酥,草色遙看近卻無。

        大家好,我是Python進(jìn)階者。

        前言

        前幾天在Python交流群里邊,【??(這是月亮的背面)】大佬分享了一個(gè)有趣的代碼,用于PySimpleGUI庫打造了一款簡易計(jì)算器,覺得挺有意思,非常適合入門PySimpleGUI的小伙伴們學(xué)習(xí),這里拿出來給大家分享一波。

        實(shí)現(xiàn)過程

        這里直接上代碼,如下所示:

        import?PySimpleGUI?as?sg


        #?定義主窗口布局,確定行數(shù)
        def?window_main():
        ????layout?=?[
        ????????[sg.Text('計(jì)算結(jié)果:',?font=("微軟雅黑",?10)),?sg.Button('歷史記錄',?font=("微軟雅黑",?10),?pad=(10,?1))],
        ????????[sg.Text('0',?key='-express-',?justification='right',?size=(30,?1),?font=("微軟雅黑",?10),?background_color='#fff',?text_color='#000')],
        ????????[sg.Text('0',?key='-result-',?justification='right',?size=(30,?1),?font=("微軟雅黑",?10),?background_color='#fff',?text_color='#000')],
        ????????[sg.Button('清空',?size=(6,?2)),?sg.Button('刪除',?size=(6,?2)),?sg.Button('x2',?size=(6,?2)),?sg.Button('÷',?size=(6,?2))],
        ????????[sg.Button('7',?size=(6,?2)),?sg.Button('8',?size=(6,?2)),?sg.Button('9',?size=(6,?2)),?sg.Button('x',?size=(6,?2))],
        ????????[sg.Button('4',?size=(6,?2)),?sg.Button('5',?size=(6,?2)),?sg.Button('6',?size=(6,?2)),?sg.Button('-',?size=(6,?2))],
        ????????[sg.Button('1',?size=(6,?2)),?sg.Button('2',?size=(6,?2)),?sg.Button('3',?size=(6,?2)),?sg.Button('+',?size=(6,?2))],
        ????????[sg.Button('+/-',?size=(6,?2)),?sg.Button('0',?size=(6,?2)),?sg.Button('.',?size=(6,?2)),?sg.Button('=',?size=(6,?2))],
        ????]

        ????#?創(chuàng)建窗口
        ????return?sg.Window('簡易計(jì)算器@月亮',?layout,?finalize=True,?default_element_size=(50,?1))

        #?定義歷史記錄窗口布局
        def?createwindow_history(history_list=None):
        ????history_text?=?''
        ????if?history_list:
        ????????history_text?=?'\n'.join(['='.join(i)?for?i?in?history_list])
        ????layout?=?[
        ????????[sg.Text('歷史記錄:',?font=("微軟雅黑",?10))],
        ????????[sg.Multiline(history_text,?justification='right',?disabled=True,?autoscroll=True,?size=(30,?10),?font=("微軟雅黑",?10),?background_color='#fff',?text_color='#000')]
        ????]
        ????return?sg.Window('歷史記錄',?layout,?finalize=True)


        def?get_result(eval_str):
        ????global?result
        ????eval_str?=?eval_str.replace('^',?'**').replace('x',?'*').replace('÷',?'/')
        ????try:
        ????????result?=?eval(eval_str)
        ????except?Exception?as?e:
        ????????result?=?'0'
        ????window_main['-result-'].update(result)
        ????return?str(result)


        window_main?=?window_main()
        window_sub?=?None
        history_list?=?[]
        express?=?'0'
        result?=?'0'
        flag?=?0

        while?True:
        ????window,?event,?value?=?sg.read_all_windows()
        ????if?window?==?window_main?and?event?in?(None,?sg.WIN_CLOSED):
        ????????if?window_sub?is?not?None:
        ????????????window_sub.close()
        ????????break
        ????elif?event?==?'歷史記錄':
        ????????if?not?window_sub:
        ????????????window_sub?=?createwindow_history(history_list)
        ????????else:
        ????????????window_sub.close()
        ????????????window_sub?=?None
        ????elif?window?==?window_sub?and?event?is?None:
        ????????window_sub.close()
        ????????window_sub?=?None
        ????elif?event?==?'=':
        ????????express1?=?express
        ????????express?=?get_result(express)
        ????????history_list.append([express1,?express])
        ????????flag?=?1
        ????elif?event?==?'清空':
        ????????express?=?'0'
        ????????result?=?'0'
        ????????window_main['-express-'].update(express)
        ????????window_main['-result-'].update(result)
        ????elif?event?==?'刪除':
        ????????if?len(express.lstrip('-').strip('(').strip(')'))?==?1:
        ????????????express?=?'0'
        ????????elif?express[-1]?==?')':
        ????????????express?=?express.lstrip('-').strip('(').strip(')')
        ????????else:
        ????????????express?=?express[:-1]
        ????????window_main['-express-'].update(express)
        ????elif?event?==?'x2':
        ????????express?=?f'({express})?^?2'
        ????????window_main['-express-'].update(express)
        ????elif?event?==?'+/-':
        ????????express?=?f'-({express})'
        ????????get_result(express)
        ????else:
        ????????if?flag?==?1?and?event?in?'0123456789':
        ????????????express?=?'0'
        ????????????flag?=?0
        ????????if?express?==?'0':
        ????????????express?=?event
        ????????else:
        ????????????express?=?express?+?event
        ????????window_main['-express-'].update(express)

        window.close()

        代碼運(yùn)行之后,一款建議計(jì)算器就已經(jīng)浮現(xiàn)在眼前了。

        之后你可以自己做點(diǎn)簡單的加減乘除等計(jì)算,都是可以的,也支持清除,查看歷史記錄功能等等。

        總結(jié)

        大家好,我是Python進(jìn)階者。這篇文章主要基于PySimpleGUI庫,打造了一款輕量級(jí)計(jì)算器,實(shí)現(xiàn)計(jì)算器的相關(guān)功能。

        最后感謝【??(這是月亮的背面)】大佬的代碼分享,也歡迎大家積極嘗試,有好的內(nèi)容也可以分享給我噢!

        ????歡迎大家積極嘗試,有好的內(nèi)容也可以分享給我噢!

        小伙伴們,快快用實(shí)踐一下吧!如果在學(xué)習(xí)過程中,有遇到任何問題,歡迎加我好友,我拉你進(jìn)Python學(xué)習(xí)交流群共同探討學(xué)習(xí)。

        -------------------?End?-------------------

        往期精彩文章推薦:

        歡迎大家點(diǎn)贊,留言,轉(zhuǎn)發(fā),轉(zhuǎn)載,感謝大家的相伴與支持

        想加入Python學(xué)習(xí)群請(qǐng)?jiān)诤笈_(tái)回復(fù)【入群

        萬水千山總是情,點(diǎn)個(gè)【在看】行不行

        /今日留言主題/

        隨便說一兩句吧~~

        瀏覽 52
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            日韩二级 | 少妇一级淫片50分钟 | 夜夜骚av.一区二区三区 | 爱国操逼中国操逼操逼 | 国产精品视频十区 | 特级西西444www大胆高清无视频 | 伊人色色色 | 在线无码免费 | 91女学生在线观看 | 91亚洲电影 |