3个Python实战项目

本文介绍了三个使用Python实现的GUI应用项目:1)日历程序,利用Tkinter库创建交互式日历查询;2)数学题目生成器,用户可自定义题目数量和运算类型;3)简易计算器,支持基本运算及三角函数。这些项目适合初学者练习PythonGUI编程。
摘要由CSDN通过智能技术生成

今天给大家带来3个Python的实战项目。

1.日历

我运行出来的效果是这样的

代码如下:

import threading
import calendar
import tkinter as tk
import datetime
import time
from tkinter import messagebox

#hello
window = tk.Tk()
window.geometry("300x400")
window.title("日历")

true_current_list = []
enquire_year_var = tk.StringVar()
enquire_month_var = tk.StringVar()
enquire_day_var = tk.StringVar()
now =datetime.datetime.now()
now2 = time.strftime("%H:%M:%S", time.localtime())
current_month = now.month
current_year = datetime.datetime.now().year
current_day = datetime.datetime.now().day
true_current_list.append(current_year)
true_current_list.append(current_month)
true_current_list.append(current_day)

def enquire_calendar():
    try:
        current_day_label.config(text=f"当前天:  {enquire_day_var.get()} 日")
        current_month_label.config(text=f"当前月份:  {enquire_month_var.get()} 月")
        current_year_label.config(text=f"当前年份:  {enquire_year_var.get()} 年")
        calendar_text.delete("1.0", "end")
        calendar_text.insert("end", chars=f"""
{calendar.month(int(enquire_year_var.get()), int(enquire_month_var.get()))}
    """)
    except ValueError:
        messagebox.showwarning(message="请正确输入")
    except IndexError:
        messagebox.showwarning(message="请正确输入")
def back():
    print(true_current_list)
    calendar_text.delete("1.0", "end")
    calendar_text.insert("end",chars=f"""
{calendar.month(true_current_list[0], true_current_list[1])}
""")
    current_day_label.config(text=f"当前天: {true_current_list[2]} 日")
    current_month_label.config(text=f"当前月份:  {true_current_list[1]} 月")
    current_year_label.config(text=f"当前年份:  {true_current_list[0]} 年")
#创建用户界面
calendar_text = tk.Text(window, width=21, height=10)
calendar_text.place(x=1, y=1)
current_year_label = tk.Label(window, text=f"当前年份:  {current_year} 年")
current_year_label.place(x=160, y=1)
current_month_label = tk.Label(window, text=f"当前月份:  {current_month} 月")
current_month_label.place(x=160, y=30)
current_day_label = tk.Label(window, text=f"当前天:  {current_day} 日")
current_day_label.place(x=160, y=60)
enquire_label = tk.Label(window, text="日期查询")
enquire_label.place(x=0, y=145)
enquire_year_label = tk.Label(window, text="年")
enquire_year_label.place(x=110, y=145)
enquire_year_entry = tk.Entry(window, textvariable=enquire_year_var, width=5)
enquire_year_entry.place(x=70, y=145)
enquire_month_label = tk.Label(window, text="月")
enquire_month_label.place(x=170, y=145)
enquire_month_entry = tk.Entry(window, textvariable=enquire_month_var, width=5)
enquire_month_entry.place(x=130, y=145)
enquire_day_entry = tk.Entry(window, textvariable=enquire_day_var, width=5)
enquire_day_entry.place(x=190, y=145)
enquire_day_label = tk.Label(window, text="日")
enquire_day_label.place(x=230, y=145)
enquire_buttton = tk.Button(window, text="查询", width=40, command=enquire_calendar)
enquire_buttton.place(x=0, y=180)
current_time_label = tk.Label(window, text="当前时间")
current_time_label.place(x=160, y=90)
back_button = tk.Button(window, text="返回当前月份", width=40, command=back)
back_button.place(x=0, y=210)
calendar_text.insert("end",chars=f"""
{calendar.month(current_year, current_month)}
""")
print("ok1")
def new_thread():
    while True:
        current_time_label.config(text=f"当前时间:  {time.strftime('%H:%M:%S')}")
        time.sleep(1)
def main():
    thread = threading.Thread(target=new_thread)
    #将新线程设置为守护线程
    thread.daemon = True
    thread.start()
    print("ok2")
if __name__ == "__main__":
    main()
    print("ok4")
window.mainloop()

2.数学题目生成器(以后不怕数学题少了):

 代码:

import random
print('welcome to math questipn maker')
while True:
    number_list =[]
    operations_list = []
    try:
        question_numbers = int(input('enter the number of questions: '))
        #一个算式中数字的数量,如输入2,就会有两个数字进行运算
        numbers_in_questions = int(input('enter the number of numbers in the questions: '))
        number_low = int(input('enter the lowest limit to the number of numbers in the questions:'))
        number_high = int(input('enter the highest limit to the number of numbers in the questions:'))
        operations = input('enter the operations in the form of  +,-,*,/ : ')
        operations_list = operations.split(',')
        for i in range(question_numbers):
            for i in range(numbers_in_questions):
                num = random.randint(number_low, number_high)
                number_list.append(num)
            for i in range(numbers_in_questions-3):
                operations_list.append(random.choice(operations.split(',')))
            for i in range(numbers_in_questions - 1):
                print(random.choice(number_list), random.choice(operations_list), end=" ")
            print(random.choice(number_list))
    except ValueError:
        print("please enter correctly")
        

3.简陋的计算器

 代码:

import tkinter as tk

window = tk.Tk()
window.title("Calculator")
window.geometry("350x400")
var = tk.StringVar()
entry = tk.Entry(window, textvariable=var, width=40)
entry.pack()
option = ""


class Calculator:
    def button_1():
        entry.insert("insert","1")

    def button_2():
        entry.insert("insert","2")

    def button_3():
        entry.insert("insert","3")

    def button_4():
        entry.insert("insert","4")

    def button_5():
        entry.insert("insert","5")

    def button_6():
        entry.insert("insert","6")

    def button_7():
        entry.insert("insert","7")

    def button_8():
        entry.insert("insert","8")

    def button_9():
        entry.insert("insert","9")

    def button_0():
        entry.insert("insert","0")

    def button_add():
        entry.insert("insert","+")

    def button_sub():
        entry.insert("insert","-")

    def button_mul():
        entry.insert("insert","*")

    def button_div():
        entry.insert("insert","/")

    def button_pow():
        entry.insert("insert","**")

    def button_front():
        entry.insert("insert","(")

    def button_back():
        entry.insert("insert",")")

    def button_sin():
        entry.insert("insert","math.sin(math.radians(number))")

    def button_cos():
        entry.insert("insert","math.cos(math.radians(number))")

    def button_tan():
        entry.insert("insert","math.tan(math.radians(number))")

    def button_baifenhao():
        entry.insert("insert","* 0.01")

    def button_del():
        current_text = entry.get()
        new = current_text[:-1]
        entry.delete(0,tk.END)
        entry.insert(0,new)

    def button_equ():
        try:
            num = entry.get()
            result = eval(str(num))
            entry.insert("end",f" = {result}")
        except IndexError:
            var.set("Are you kidding?")
        except ValueError:
            var.set("Are you kidding?")
        except SyntaxError:
            var.set("Are you kidding?")
        except NameError:
            var.set("Are you kidding?")
        except ZeroDivisionError:
            var.set("0 cant divide other numbers!")

    def button_C():
        var.set("")
    b1 = tk.Button(window, text="1", width=8, height=2, command=button_1)
    b1.pack()
    b1.place(x=10,y=50)
    b2 = tk.Button(window, text="2", width=8, height=2, command=button_2)
    b2.pack()
    b2.place(x=90,y=50)
    b3 = tk.Button(window, text="3", width=8, height=2, command=button_3)
    b3.pack()
    b3.place(x=170,y=50)
    b4 = tk.Button(window, text="4", width=8, height=2, command=button_4)
    b4.pack()
    b4.place(x=250, y=50)
    b5 = tk.Button(window, text="5", width=8, height=2, command=button_5)
    b5.pack()
    b5.place(x=10, y=100)
    b6 = tk.Button(window, text="6", width=8, height=2, command=button_6)
    b6.pack()
    b6.place(x=90, y=100)
    b7 = tk.Button(window, text="7", width=8, height=2, command=button_7)
    b7.pack()
    b7.place(x=170, y=100)
    b8 = tk.Button(window,text="8", width=8, height=2, command=button_8)
    b8.pack()
    b8.place(x=250,y=100)
    b9 = tk.Button(window, text="9", width=8, height=2, command=button_9)
    b9.pack()
    b9.place(x=10, y=150)
    b0 = tk.Button(window, text="0", width=8, height=2, command=button_0)
    b0.pack()
    b0.place(x=90, y=150)
    badd = tk.Button(window, text="+", width=8, height=2, command=button_add)
    badd.pack()
    badd.place(x=170, y=150)
    bsub = tk.Button(window,text="-", width=8, height=2, command=button_sub)
    bsub.pack()
    bsub.place(x=250, y=150)
    bmul = tk.Button(window,text="*", width=8, height=2, command=button_mul)
    bmul.pack()
    bmul.place(x=10,y=200)
    bdiv = tk.Button(window, text="/", width=8, height=2, command=button_div)
    bdiv.pack()
    bdiv.place(x=90, y=200)
    bequ = tk.Button(window, text="=", width=8, height=2, command=button_equ)
    bequ.pack()
    bequ.place(x=90, y=250)
    bc = tk.Button(window, text="C", width=8, height=2, command=button_C)
    bc.pack()
    bc.place(x=170, y=250)
    b_del = tk.Button(window, text="del", width=8, height=2, command=button_del)
    b_del.pack()
    b_del.place(x=250, y=250)
    bpow = tk.Button(window, text="^", width=8, height=2, command=button_pow)
    bpow.pack()
    bpow.place(x=10, y=250)
    b_front = tk.Button(window, text="(", width=8 ,height=2, command=button_front)
    b_front.pack()
    b_front.place(x=170, y=200)
    b_back = tk.Button(window, text=")", width=8, height=2, command=button_back)
    b_back.pack()
    b_back.place(x=250, y=200)
    b_sin = tk.Button(window, text="sin", width=8, height=2, command=button_sin)
    b_sin.pack()
    b_sin.place(x=10, y=300)
    b_cos = tk.Button(window, text="cos", width=8, height=2, command=button_cos)
    b_cos.pack()
    b_cos.place(x=90, y=300)
    b_tan = tk.Button(window, text="tan", width=8, height=2, command=button_tan)
    b_tan.pack()
    b_tan.place(x=170, y=300)
    b_baifenhao = tk.Button(window, text="%", width=8, height=2,command=button_baifenhao)
    b_baifenhao.pack()
    b_baifenhao.place(x=250, y=300)
window.mainloop()

以上就是3个简单的Python实战项目,希望对大家有所帮助

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值