目录
tkinter
计算器
2.计算题练习
猜数字
烦人的程序
无法拒绝的请假条。。。
爬虫
你想看豆瓣评分前十的电影?
WXpython
记事本(可保存)编辑
数字逻辑
解方程
tkinter
计算器
import tkinter as tk
import tkinter.messagebox as mroot = tk.Tk()root.title("计算器")root.geometry("360x500")E1 = tk.Entry(root, width=16, font=('kai ti', 30), highlightcolor='grey')
E1.place(x=15, y=40)def rr_ride():E1.insert(999, '**')def check():if int(E1.get()) >= 1000000000:if len(E1.get()) >= 15:m.showwarning('warning', '数字太多了!')E1.delete(0, 'end')def number_1():E1.insert(999, '1')def number_2():E1.insert(999, '2')def number_3():E1.insert(999, '3')def number_4():E1.insert(999, '4')def number_5():E1.insert(999, '5')def number_6():E1.insert(999, '6')def number_7():E1.insert(999, '7')def number_8():E1.insert(999, '8')def number_9():E1.insert(999, '9')def number_0():E1.insert(999, '0')def clean():E1.delete(0, 'end')def reduce():E1.insert(999, '-')def add():E1.insert(999, '+')def delete():E1.delete(0, 'end')def hun():E1.insert(999, '%')def ride():E1.insert(999, '*')def r_ride():word = int(E1.get()) ** 0.5E1.delete(0, 'end')E1.insert(999, word)check()def division():E1.insert(999, '/')def d_division():word = '1', '/', E1.get()str(word)E1.delete(0, 'end')E1.insert(999, word)def calculate():word = eval(E1.get())E1.delete(0, 'end')E1.insert('999', word)check()# line 1B1 = tk.Button(root, text=' C ', activebackground='yellow', bd=5, font=('kai ti', 20), bg='orange', command=clean)
B1.place(x=15, y=120)B2 = tk.Button(root, text=' 1 ', activebackground='grey', bd=5, font=('kai ti', 20), command=number_1)
B2.place(x=15, y=200)B3 = tk.Button(root, text=' 4 ', activebackground='grey', bd=5, font=('kai ti', 20), command=number_4)
B3.place(x=15, y=280)B4 = tk.Button(root, text=' 7 ', activebackground='grey', bd=5, font=('kai ti', 20), command=number_7)
B4.place(x=15, y=360)B11 = tk.Button(root, text=' - ', activebackground='grey', bd=5, font=('kai ti', 20), command=reduce)
B11.place(x=15, y=430)# line 2B5 = tk.Button(root, text='开方', activebackground='grey', bd=5, font=('kai ti', 20), command=r_ride)
B5.place(x=105, y=120)B6 = tk.Button(root, text=' 2 ', activebackground='grey', bd=5, font=('kai ti', 20), command=number_2)
B6.place(x=105, y=200)B7 = tk.Button(root, text=' 5 ', activebackground='grey', bd=5, font=('kai ti', 20), command=number_5)
B7.place(x=105, y=280)B8 = tk.Button(root, text=' 8 ', activebackground='grey', bd=5, font=('kai ti', 20), command=number_8)
B8.place(x=105, y=360)B11 = tk.Button(root, text=' 0 ', activebackground='grey', bd=5, font=('kai ti', 20), command=number_0)
B11.place(x=105, y=430)# line 3B9 = tk.Button(root, text=' ^ ', activebackground='grey', bd=5, font=('kai ti', 20), command=rr_ride)
B9.place(x=200, y=120)B13 = tk.Button(root, text='1/x', activebackground='grey', bd=5, font=('kai ti', 20), bg='yellow', command=d_division)
B13.place(x=280, y=120)B10 = tk.Button(root, text=' 3 ', activebackground='grey', bd=5, font=('kai ti', 20), command=number_3)
B10.place(x=195, y=200)B11 = tk.Button(root, text=' 6 ', activebackground='grey', bd=5, font=('kai ti', 20), command=number_6)
B11.place(x=195, y=280)B12 = tk.Button(root, text=' 9 ', activebackground='grey', bd=5, font=('kai ti', 20), command=number_9)
B12.place(x=195, y=360)B12 = tk.Button(root, text=' x ', activebackground='grey', bd=5, font=('kai ti', 20), command=ride)
B12.place(x=195, y=430)# line 4B10 = tk.Button(root, text=' + ', activebackground='grey', bd=5, font=('kai ti', 20), command=add)
B10.place(x=280, y=200)B14 = tk.Button(root, text=' / ', activebackground='red', bd=5, font=('kai ti', 20), command=division)
B14.place(x=280, y=280)B13 = tk.Button(root, text=' = ', activebackground='blue', bd=5, font=('kai ti', 20), width=4, height=4, command=calculate)
B13.place(x=280, y=360)root.mainloop()
2.计算题练习
可以随机出10道计算题给你做
import random
import tkinter as tk
import timeroot = tk.Tk()
root.geometry('700x550')
A = 20ANSWER_List = []for i in range(10):num_1 = random.randint(0, 100)num_2 = random.randint(0, 100)symbol = ['+', '-', '*']symbol_random = random.randint(0, 2)t1 = f'{num_1}{symbol[symbol_random]}{num_2}'t = t1, "="tk.Label(root, text=t, font=('kai ti', 20)).place(x=20, y=A)eval_ = eval(t1)ANSWER_List.append(eval_)A += 40def OK():F = 0T = 0if int(E.get()) == int(ANSWER_List[0]):tk.Label(root, text='回答正确', bg='green').place(x=450, y=20)T += 1else:tk.Label(root, text=f'回答错误,正确答案是{ANSWER_List[0]}', bg='red').place(x=450, y=20)F += 1if int(E2.get()) == int(ANSWER_List[1]):tk.Label(root, text='回答正确', bg='green').place(x=450, y=60)T += 1else:tk.Label(root, text=f'回答错误,正确答案是{ANSWER_List[1]}', bg='red').place(x=450, y=60)F += 1if int(E3.get()) == int(ANSWER_List[2]):tk.Label(root, text='回答正确', bg='green').place(x=450, y=100)T += 1else:tk.Label(root, text=f'回答错误,正确答案是{ANSWER_List[2]}', bg='red').place(x=450, y=100)F += 1if int(E4.get()) == int(ANSWER_List[3]):tk.Label(root, text='回答正确', bg='green').place(x=450, y=140)T += 1else:tk.Label(root, text=f'回答错误,正确答案是{ANSWER_List[3]}', bg='red').place(x=450, y=140)F += 1if int(E5.get()) == int(ANSWER_List[4]):tk.Label(root, text='回答正确', bg='green').place(x=450, y=180)T += 1else:tk.Label(root, text=f'回答错误,正确答案是{ANSWER_List[4]}', bg='red').place(x=450, y=180)F += 1if int(E6.get()) == int(ANSWER_List[5]):tk.Label(root, text='回答正确', bg='green').place(x=450, y=220)T += 1else:tk.Label(root, text=f'回答错误,正确答案是{ANSWER_List[5]}', bg='red').place(x=450, y=220)F += 1if int(E7.get()) == int(ANSWER_List[6]):tk.Label(root, text='回答正确', bg='green').place(x=450, y=260)T += 1else:tk.Label(root, text=f'回答错误,正确答案是{ANSWER_List[6]}', bg='red').place(x=450, y=260)F += 1if int(E8.get()) == int(ANSWER_List[7]):tk.Label(root, text='回答正确', bg='green').place(x=450, y=300)T += 1else:tk.Label(root, text=f'回答错误,正确答案是{ANSWER_List[7]}', bg='red').place(x=450, y=300)F += 1if int(E9.get()) == int(ANSWER_List[8]):tk.Label(root, text='回答正确', bg='green').place(x=450, y=340)T += 1else:tk.Label(root, text=f'回答错误,正确答案是{ANSWER_List[8]}', bg='red').place(x=450, y=340)F += 1if int(E10.get()) == int(ANSWER_List[9]):tk.Label(root, text='回答正确', bg='green').place(x=450, y=380)T += 1else:tk.Label(root, text=f'回答错误,正确答案是{ANSWER_List[9]}', bg='red').place(x=450, y=380)F += 1tk.Label(root, text=f'分数{T * 10}', font=('kai ti', 20)).place(x=200, y=450)E = tk.Entry(font=('kai ti', 20))
E.place(x=140, y=20)E2 = tk.Entry(font=('kai ti', 20))
E2.place(x=140, y=60)E3 = tk.Entry(font=('kai ti', 20))
E3.place(x=140, y=100)E4 = tk.Entry(font=('kai ti', 20))
E4.place(x=140, y=140)E5 = tk.Entry(font=('kai ti', 20))
E5.place(x=140, y=180)E6 = tk.Entry(font=('kai ti', 20))
E6.place(x=140, y=220)E7 = tk.Entry(font=('kai ti', 20))
E7.place(x=140, y=260)E8 = tk.Entry(font=('kai ti', 20))
E8.place(x=140, y=300) E9 = tk.Entry(font=('kai ti', 20))
E9.place(x=140, y=340)E10 = tk.Entry(font=('kai ti', 20))
E10.place(x=140, y=380)Button = tk.Button(root, text='确 认', font=('kai ti', 25), command=OK)
Button.place(x=200, y=500)root.mainloop()
猜数字
import tkinter as tk
from tkinter import messagebox
import random messagebox.showinfo('规则', '猜一个0到100间的数')
class GuessNumberGame: def __init__(self, root): self.root = root self.root.title("猜数字游戏") # 初始化游戏变量 self.number_to_guess = random.randint(1, 100) self.guesses = 0 # 创建UI元素 self.label = tk.Label(root, text="我猜的数字是?", font=("Arial", 16)) self.label.pack(pady=20) self.entry = tk.Entry(root, font=("Arial", 14), width=10) self.entry.pack() self.guess_button = tk.Button(root, text="猜测", command=self.check_guess) self.guess_button.pack(pady=20) self.guess_label = tk.Label(root, text="", font=("Arial", 14)) self.guess_label.pack() def check_guess(self): user_guess = self.entry.get() if not user_guess.isdigit() or int(user_guess) < 1 or int(user_guess) > 100: messagebox.showerror("错误", "请输入一个1到100之间的整数!") return self.guesses += 1 user_guess = int(user_guess) if user_guess == self.number_to_guess: messagebox.showinfo("恭喜!", f"恭喜你,猜对了!数字是 {self.number_to_guess}。\n你总共猜了 {self.guesses} 次。") self.reset_game() elif user_guess < self.number_to_guess: self.guess_label.config(text="太低了!再试试看。") else: self.guess_label.config(text="太高了!再试试看。") def reset_game(self): self.number_to_guess = random.randint(1, 100) self.guesses = 0 self.entry.delete(0, tk.END) self.guess_label.config(text="") def main(): root = tk.Tk() app = GuessNumberGame(root) root.mainloop() if __name__ == "__main__": main()
烦人的程序
每隔十秒执行一次,因为执行完就跑,所以只能用任务管理器关闭
import tkinter as tk
from tkinter import messagebox
import time def annoying_popup(): # 显示一个消息框 messagebox.showinfo("你好", "我又来了,想我了吗") # 等待一段时间(例如5秒)后再次调用自己 root.after(5000, annoying_popup) # 创建Tkinter窗口
root = tk.Tk()
root.withdraw() # 隐藏主窗口 # 调用函数开始循环
annoying_popup() # 进入Tkinter主事件循环
root.mainloop()
无法拒绝的请假条。。。
import tkinter as tk
import tkinter.messagebox as msgbox
import os
import syswhile True:root = tk.Tk()root.title('请假条')root.geometry('500x300')def agree():msgbox.showinfo('哈哈哈', '好的再见')sys.exit()def reject():msgbox.showinfo('.......', '不给假是吧')os.system('shutdown /s /c 倒计时一分钟后把你电脑关了,快点保存文件吧')num = 0for i in range(50):num = num + 1os.system(f'md 骚扰文件,哈哈哈气死你{num}')L1 = tk.Label(root, text='我今天很舒服,想请假', font=('kai ti', 17)).place(x=100, y=1)tk.Button(root, text='同意', command=agree, font=('kai ti', 20)).place(x=100, y=100)tk.Button(root, text='不行', command=reject, font=('kai ti', 20)).place(x=300, y=100)root.mainloop()
有点恶心,不建议自己尝试,关不掉的话就用任务管理器
爬虫
你想看豆瓣评分前十的电影?
需要注意的是!你们的USEAGENT可能和我不同,不知道自己的USERAGENT怎么搞的话可以看我之前的爬虫教程!!!
import requests
import reurl = 'https://movie.douban.com/top250' #要爬取的urlheaders = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.6261.95 Safari/537.36'
}res = requests.get(url, headers=headers) #请求urlobj = re.compile(r'<span class="title">(?P<name>[\u4e00-\u9fa5].*?)</span>.*?<div class="bd">.*?<p class="">(?P<dy>.*?)</p>.*?</div>.*?<span class="rating_num" property="v:average">(?P<percent>.*?)</span>', re.S)ret = obj.finditer(res.text)for iter in ret:info = iter.group('name'), iter.group('dy'), iter.group('percent')print(info[0], info[1], f'电影评分:{info[2]}')print('--------------------------------------------------------------------------------------------------------')
WXpython
记事本(可保存)
import os
import wxclass MainWindow(wx.Frame):def __init__(self, parent, title):wx.Frame.__init__(self, parent, title=title, size=(500,300))self.control = wx.TextCtrl(self, style=wx.TE_MULTILINE)self.CreateStatusBar() # A StatusBar in the bottom of the window# Setting up the menu.filemenu= wx.Menu()# wx.ID_ABOUT and wx.ID_EXIT are standard ids provided by wxWidgets.menuExit = filemenu.Append(wx.ID_EXIT,"退出","退出此程序")menuSave = filemenu.Append(wx.ID_SAVE, '保存', '保存输入框中的内容')# Creating the menubar.menuBar = wx.MenuBar()menuBar.Append(filemenu,"菜单") # Adding the "filemenu" to the MenuBarself.SetMenuBar(menuBar) # Adding the MenuBar to the Frame content.# Set events.self.Bind(wx.EVT_MENU, self.OnExit, menuExit)self.Bind(wx.EVT_MENU, self.save_file_content, menuSave)self.Show(True)def OnExit(self,e):self.Close(True)def save_file_content(self, event):'''保存文件内容与菜单中的保存选项绑定'''self.dir_name = ''fd = wx.FileDialog(self, '把文件保存到何处', self.dir_name, '.txt', 'TEXT file(*.txt)|*.txt', wx.FD_SAVE)if fd.ShowModal() == wx.ID_OK:self.file_name = fd.GetFilename()self.dir_name = fd.GetDirectory()try:with open(os.path.join(self.dir_name, self.file_name), 'w', encoding='utf-8') as f:text = self.text_control.GetValue()f.write(text)save_msg = wx.MessageDialog(self, '文件已保存', '提示')except FileNotFoundError:save_msg = wx.MessageDialog(self, '保存失败,无效的保存路径', '提示') else:save_msg = wx.MessageDialog(self, '未选择保存路径', '错误')save_msg.ShowModal()save_msg.Destroy()app = wx.App(False)
frame = MainWindow(None, "记事本")
app.MainLoop()
数字逻辑
解方程
from sympy import Symbol
from sympy import solvefor i in range(2):an = input('你需要解哪种方程A:一元一次, B:二元一次(输入A或B)')if an == 'A':print('示例1:5*x + 3 = 8写成 5*x + 3 - 8'' 示例2:6*x - 5 = 0就写 6*x - 5 , 将右边变到左边使右边等于零')x = Symbol('x')expr = input('输入一元一次方程: ')answer1 = solve(expr, dict=True)answer2 = answer1[0]print('x=', answer2[x])breakif an == 'B':x = Symbol('x')y = Symbol('y')print('示例1:5*x + 3*y = 8写成 5*x + 3*y - 8'' 示例2:6*x - 5y = 0就写 6*x - 5*y , 将右边变到左边使右边等于零')expr1 = input('输入方程1(按回车键后输入第二条):')expr2 = input('输入方程2:')da = solve((expr1, expr2), dict=True)D = da[0]print('x=', D[x], 'y=', D[y])breakelse:print('输入A或B!')