網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
當(dāng)使用桌面應(yīng)用程序的時(shí)候,
有沒(méi)有那么一瞬間,
想學(xué)習(xí)一下桌面應(yīng)用程序開(kāi)發(fā)?
建議此次課程大家稍作了解不要浪費(fèi)太多時(shí)間,
因?yàn)闆](méi)有哪家公司會(huì)招聘以為Python程序員開(kāi)發(fā)桌面程序吧?
開(kāi)發(fā)環(huán)境:
- Python 3.6
- Pycharm
代碼
界面設(shè)置
導(dǎo)入模塊
import tkinter as tk
實(shí)例化一個(gè)窗體對(duì)象
root = tk.Tk()
標(biāo)題
root.title('計(jì)算器')
大小以及出現(xiàn)的位置
root.geometry("295x280+150+150")
透明度
root.attributes("-alpha", 0.9)
背景
root["background"] = "#ffffff"
標(biāo)簽
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))
選擇運(yùn)算符號(hào)
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)
等號(hào)
def equal(): a = ''.join(lists) end_num = eval(a) result_num.set(end_num) lists.clear() lists.append(str(end_num))
定義一個(gè)列表收集輸入的內(nèi)容
lists = [] result_num = tk.StringVar() result_num.set(0)
最后運(yùn)行代碼,效果如下圖
先試試
運(yùn)算得出結(jié)果
原文鏈接:https://blog.csdn.net/m0_67575344/article/details/124026574
相關(guān)推薦
- 2022-12-22 C/C++?活動(dòng)預(yù)處理器詳解_C 語(yǔ)言
- 2022-05-11 深入理解AQS之獨(dú)占鎖ReentrantLock源碼分析
- 2022-05-08 C++?vector的簡(jiǎn)單實(shí)現(xiàn)_C 語(yǔ)言
- 2023-07-10 解決flask (flask-restful)中文亂碼問(wèn)題
- 2023-08-30 liunx shell腳本并發(fā)控制詳解
- 2022-10-24 解析Golang中引用類(lèi)型是否進(jìn)行引用傳遞_Golang
- 2023-05-22 shell腳本自動(dòng)輸入用戶(hù)名和密碼的實(shí)現(xiàn)_linux shell
- 2022-04-09 SpringMVC 使用RestFul風(fēng)格實(shí)現(xiàn)簡(jiǎn)單的文件下載
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門(mén)
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支