大家好,小编来为大家解答以下问题,python如何编写小程序,python小程序开发教程,现在让我们一起来看看吧!
大家好,给大家分享一下python如何编写小程序,很多人还不知道这一点。下面详细解释一下。现在让我们来看看!
嗨嗨。大家好,我是小圆~
想问大家,在自己使用桌面小程序的时候,有没有想过自己也可以开发出来一个小程序呢?
嘿嘿,现在就给大家分享一个用python开发出来的桌面小程序!
但是建议大家稍微了解一下就好了,不用花大量时间来研究这个哦
开发环境:
Python 3.6 Pycharm
代码
界面设置
导入模块
import tkinter as tk
实例化一个窗体对象
root = ()
标题
root.title('计算器')
大小以及出现的位置
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')
布局
(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'))
(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'))
(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'))
(padx=4, row=3, column=2)
button_addition = tk.Button(root, text='+', width=5, font=('宋体', 16), relief='flat', background='#C0C0C0', command=lambda: operator('+'))
(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'))
(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'))
(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'))
(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())
(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'))
(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('.'))
(padx=4, row=5, column=2)
现在得出界面效果
功能
添加数字
def append_num(i):
lists.append(i)
(''.join(lists))
选择运算符号
def operator(i):
if len(lists) > 0:
if lists[-1] in ['+', '-', '*', '/']:
lists[-1] = i
else:
lists.append(i)
(''.join(lists))
清零
def clear():
lists.clear()
(0)
退格
def back():
del lists[-1]
(lists)
等号
def equal():
a = ''.join(lists)
end_num = eval(a)
(end_num)
lists.clear()
lists.append(str(end_num))
定义一个列表收集输入的内容
lists = []
result_num = tk.StringVar()
(0)
最后运行代码,效果如下图:
先试试
运算得出结果
好啦,今天的分享到这里就结束了 ~ 对文章有问题的,或者有其他关于python的问题,可以在评论区留言或者私信我哦 觉得我分享的文章不错的话,可以关注一下我,或者给文章点赞(/≧▽≦)/
原文地址:https://blog.csdn.net/w666666Wwwwwww/article/details/134995313