Python開發(fā)桌面小程序功能
當使用桌面應用程序的時候,
有沒有那么一瞬間,
想學習一下桌面應用程序開發(fā)?
建議此次課程大家稍作了解不要浪費太多時間,
因為沒有哪家公司會招聘以為Python程序員開發(fā)桌面程序吧?
開發(fā)環(huán)境:
- Python 3.6
- Pycharm
代碼
界面設置
導入模塊
import tkinter as tk
實例化一個窗體對象
root = tk.Tk()
標題
root.title('計算器')大小以及出現(xiàn)的位置
root.geometry("295x280+150+150")透明度
root.attributes("-alpha", 0.9)背景
root["background"] = "#ffffff"
標簽
lable1 = tk.Label(root, textvariable=result_num, width=20, height=2, font=('宋體', 20), justify='left', background='#ffffff', anchor='se')布局
lable1.grid(padx=4, pady=4, row=0, column=0, columnspan=4)
按鈕
button_clear = tk.Button(root, text='C', width=5, font=('宋體', 16), relief='flat', background='#C0C0C0', command=lambda: clear())
button_back = tk.Button(root, text='←', width=5, font=('宋體', 16), relief='flat', background='#C0C0C0', command=lambda: back())
button_division = tk.Button(root, text='/', width=5, font=('宋體', 16), relief='flat', background='#C0C0C0', command=lambda: operator('/'))
button_multiplication = tk.Button(root, text='x', width=5, font=('宋體', 16), relief='flat', background='#C0C0C0', command=lambda: operator('*'))
button_clear .grid(padx=4, row=1, column=0)
button_back .grid(padx=4, row=1, column=1)
button_division .grid(padx=4, row=1, column=2)
button_multiplication .grid(padx=4, row=1, column=3)
button_seven = tk.Button(root, text='7', width=5, font=('宋體', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('7'))
button_eight = tk.Button(root, text='8', width=5, font=('宋體', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('8'))
button_nine = tk.Button(root, text='9', width=5, font=('宋體', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('9'))
button_subtraction = tk.Button(root, text='—', width=5, font=('宋體', 16), relief='flat', background='#C0C0C0', command=lambda: operator('-'))
button_seven .grid(padx=4, row=2, column=0)
button_eight .grid(padx=4, row=2, column=1)
button_nine .grid(padx=4, row=2, column=2)
button_subtraction .grid(padx=4, row=2, column=3)
button_four = tk.Button(root, text='4', width=5, font=('宋體', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('4'))
button_four.grid(padx=4, pady=4, row=3, column=0)
button_five = tk.Button(root, text='5', width=5, font=('宋體', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('5'))
button_five.grid(padx=4, row=3, column=1)
button_six = tk.Button(root, text='6', width=5, font=('宋體', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('6'))
button_six.grid(padx=4, row=3, column=2)
button_addition = tk.Button(root, text='+', width=5, font=('宋體', 16), relief='flat', background='#C0C0C0', command=lambda: operator('+'))
button_addition.grid(padx=4, row=3, column=3)
button_one = tk.Button(root, text='1', width=5, font=('宋體', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('1'))
button_one.grid(padx=4, row=4, column=0)
button_two = tk.Button(root, text='2', width=5, font=('宋體', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('2'))
button_two.grid(padx=4, row=4, column=1)
button_three = tk.Button(root, text='3', width=5, font=('宋體', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('3'))
button_three.grid(padx=4, row=4, column=2)
button_equal = tk.Button(root, text='=', width=5, height=3, font=('宋體', 16), relief='flat', background='#C0C0C0', command=lambda: equal())
button_equal.grid(padx=4, row=4, rowspan=5, column=3)
button_zero = tk.Button(root, text='0', width=12, font=('宋體', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('0'))
button_zero.grid(padx=4, pady=4, row=5, column=0, columnspan=2)
button_decimal = tk.Button(root, text='.', width=5, font=('宋體', 16), relief='flat', background='#FFDEAD', command=lambda: append_num('.'))
button_decimal.grid(padx=4, row=5, column=2)
現(xiàn)在得出界面效果

功能
添加數(shù)字
def append_num(i):
lists.append(i)
result_num.set(''.join(lists))
選擇運算符號
def operator(i):
if len(lists) > 0:
if lists[-1] in ['+', '-', '*', '/']:
lists[-1] = i
else:
lists.append(i)
result_num.set(''.join(lists))
清零
def clear():
lists.clear()
result_num.set(0)退格
def back():
del lists[-1]
result_num.set(lists)
等號
def equal():
a = ''.join(lists)
end_num = eval(a)
result_num.set(end_num)
lists.clear()
lists.append(str(end_num))
定義一個列表收集輸入的內(nèi)容
lists = [] result_num = tk.StringVar() result_num.set(0)
最后運行代碼,效果如下圖
先試試

運算得出結(jié)果

到此這篇關(guān)于Python開發(fā)一個桌面小程序的文章就介紹到這了,更多相關(guān)Python開發(fā)一個桌面小程序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python使用Apriori算法進行關(guān)聯(lián)性解析
這篇文章主要為大家分享了python使用Apriori算法進行關(guān)聯(lián)性的解析,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
Python實現(xiàn)監(jiān)聽目錄并取消文件只讀屬性
這篇文章主要為大家詳細介紹了如何利用Python實現(xiàn)監(jiān)聽目錄并取消文件只讀屬性,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2023-07-07
Python實現(xiàn)大數(shù)據(jù)收集至excel的思路詳解
這篇文章主要介紹了Python實現(xiàn)大數(shù)據(jù)收集至excel的思路,本文通過完整代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01
python多線程調(diào)用exit無法退出的解決方法
今天小編就為大家分享一篇python多線程調(diào)用exit無法退出的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02

