• 浔川画板v5.0——浔川python科技社


    浔川画板v5.0


    本代码由浔川python社、浔川python科技社联合创作


    1. # -*- coding: utf-8 -*-
    2. import tkinter as tk
    3. import tkinter.messagebox
    4. import pickle
    5. import random
    6. # 窗口
    7. window = tk.Tk()
    8. window.title('欢迎进入python')
    9. window.geometry('450x200')
    10. # 画布放置图片
    11. # canvas=tk.Canvas(window,height=300,width=500)
    12. # imagefile=tk.PhotoImage(file='qm.png')
    13. # image=canvas.create_image(0,0,anchor='nw',image=imagefile)
    14. # canvas.pack(side='top')
    15. # 标签 用户名密码
    16. Verification_Code = random.randint(1000, 9999)#设置一个随机的四位数
    17. Verification_Code = str(Verification_Code)#把类型转换为str型
    18. print(type(Verification_Code))
    19. tk.Label(window, text='用户名:').place(x=100, y=30)
    20. tk.Label(window, text='密码:').place(x=100, y=70)
    21. tk.Label(window, text='验证码').place(x=100, y=110)
    22. tk.Label(window, text=Verification_Code).place(x=320, y=110)
    23. # 用户名输入框
    24. var_usr_name = tk.StringVar()
    25. entry_usr_name = tk.Entry(window, textvariable=var_usr_name)
    26. entry_usr_name.place(x=160, y=30)
    27. # 密码输入框
    28. var_usr_pwd = tk.StringVar()
    29. entry_usr_pwd = tk.Entry(window, textvariable=var_usr_pwd, show='*')
    30. entry_usr_pwd.place(x=160, y=70)
    31. #验证码输入框
    32. var_usr_yzm = tk.StringVar()
    33. entry_usr_yzm = tk.Entry(window, textvariable=var_usr_yzm)
    34. entry_usr_yzm.place(x=160, y=110)
    35. # 登录函数
    36. def usr_log_in():
    37. # 输入框获取用户名密码
    38. usr_name = var_usr_name.get()
    39. usr_pwd = var_usr_pwd.get()
    40. usr_yzm = var_usr_yzm.get()
    41. #测试类型
    42. print(type(usr_yzm),type(Verification_Code))
    43. # 从本地字典获取用户信息,如果没有则新建本地数据库
    44. try:
    45. with open('usr_info.pickle', 'rb') as usr_file:
    46. usrs_info = pickle.load(usr_file)
    47. except FileNotFoundError:
    48. with open('usr_info.pickle', 'wb') as usr_file:
    49. usrs_info = {'admin': 'admin'}
    50. pickle.dump(usrs_info, usr_file)
    51. # 判断验证码是否正确用户名和密码是否匹配
    52. if usr_yzm == Verification_Code:
    53. if usr_name in usrs_info:
    54. if usr_pwd == usrs_info[usr_name]:
    55. tk.messagebox.showinfo(title='welcome',
    56. message='欢迎您:' + usr_name)
    57. else:
    58. tk.messagebox.showerror(message='密码错误')
    59. # 用户名密码不能为空
    60. elif usr_name == '' or usr_pwd == '':
    61. tk.messagebox.showerror(message='用户名或密码为空')
    62. # 不在数据库中弹出是否注册的框
    63. else:
    64. is_signup = tk.messagebox.askyesno('欢迎', '您还没有注册,是否现在注册')
    65. if is_signup:
    66. usr_sign_up()
    67. elif usr_yzm == '':
    68. tk.messagebox.showerror(message='验证码不能为空')
    69. else:
    70. tk.messagebox.showerror(message='验证码有误!')
    71. # 注册函数
    72. def usr_sign_up():
    73. # 确认注册时的相应函数
    74. def signtowcg():
    75. # 获取输入框内的内容
    76. nn = new_name.get()
    77. np = new_pwd.get()
    78. npf = new_pwd_confirm.get()
    79. # 本地加载已有用户信息,如果没有则已有用户信息为空
    80. try:
    81. with open('usr_info.pickle', 'rb') as usr_file:
    82. exist_usr_info = pickle.load(usr_file)
    83. except FileNotFoundError:
    84. exist_usr_info = {}
    85. # 检查用户名存在、密码为空、密码前后不一致
    86. if nn in exist_usr_info:
    87. tk.messagebox.showerror('错误', '用户名已存在')
    88. elif np == '' or nn == '':
    89. tk.messagebox.showerror('错误', '用户名或密码为空')
    90. elif np != npf:
    91. tk.messagebox.showerror('错误', '密码前后不一致')
    92. # 注册信息没有问题则将用户名密码写入数据库
    93. else:
    94. exist_usr_info[nn] = np
    95. with open('usr_info.pickle', 'wb') as usr_file:
    96. pickle.dump(exist_usr_info, usr_file)
    97. tk.messagebox.showinfo('欢迎', '注册成功')
    98. # 注册成功关闭注册框
    99. window_sign_up.destroy()
    100. # 新建注册界面
    101. window_sign_up = tk.Toplevel(window)
    102. window_sign_up.geometry('350x200')
    103. window_sign_up.title('注册')
    104. # 用户名变量及标签、输入框
    105. new_name = tk.StringVar()
    106. tk.Label(window_sign_up, text='用户名:').place(x=10, y=10)
    107. tk.Entry(window_sign_up, textvariable=new_name).place(x=150, y=10)
    108. # 密码变量及标签、输入框
    109. new_pwd = tk.StringVar()
    110. tk.Label(window_sign_up, text='请输入密码:').place(x=10, y=50)
    111. tk.Entry(window_sign_up, textvariable=new_pwd, show='*').place(x=150, y=50)
    112. # 重复密码变量及标签、输入框
    113. new_pwd_confirm = tk.StringVar()
    114. tk.Label(window_sign_up, text='请再次输入密码:').place(x=10, y=90)
    115. tk.Entry(window_sign_up, textvariable=new_pwd_confirm, show='*').place(x=150, y=90)
    116. # 确认注册按钮及位置
    117. bt_confirm_sign_up = tk.Button(window_sign_up, text='确认注册',
    118. command=signtowcg)
    119. bt_confirm_sign_up.place(x=150, y=130)
    120. # 退出的函数
    121. def usr_sign_quit():
    122. window.destroy()
    123. # 登录 注册按钮
    124. bt_login = tk.Button(window, text='登录', command=usr_log_in)
    125. bt_login.place(x=140, y=150)
    126. bt_logup = tk.Button(window, text='注册', command=usr_sign_up)
    127. bt_logup.place(x=210, y=150)
    128. bt_logquit = tk.Button(window, text='退出', command=usr_sign_quit)
    129. bt_logquit.place(x=280, y=150)
    130. # 主循环
    131. window.mainloop()
    132. import tkinter as tk
    133. import time
    134. # 创建主窗口
    135. window = tk.Tk()
    136. window.title('进度条')
    137. window.geometry('630x150')
    138. # 设置下载进度条
    139. tk.Label(window, text='下载进度:', ).place(x=50, y=60)
    140. canvas = tk.Canvas(window, width=465, height=22, bg="white")
    141. canvas.place(x=110, y=60)
    142. # 显示下载进度
    143. def progress():
    144. # 填充进度条
    145. fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="green")
    146. x = 500 # 未知变量,可更改
    147. n = 465 / x # 465是矩形填充满的次数
    148. for i in range(x):
    149. n = n + 465 / x
    150. canvas.coords(fill_line, (0, 0, n, 60))
    151. window.update()
    152. time.sleep(0.02) # 控制进度条流动的速度
    153. # 清空进度条
    154. fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="white")
    155. x = 500 # 未知变量,可更改
    156. n = 465 / x # 465是矩形填充满的次数
    157. for t in range(x):
    158. n = n + 465 / x
    159. # 以矩形的长度作为变量值更新
    160. canvas.coords(fill_line, (0, 0, n, 60))
    161. window.update()
    162. time.sleep(0) # 时间为0,即飞速清空进度条
    163. btn_download = tk.Button(window, text='开始下载', command=progress)
    164. btn_download.place(x=400, y=105)
    165. window.mainloop()
    166. # 创建主窗口
    167. window = tk.Tk()
    168. window.title('进度条')
    169. window.geometry('630x150')
    170. # 设置下载进度条
    171. tk.Label(window, text='安装进度:', ).place(x=50, y=60)
    172. canvas = tk.Canvas(window, width=465, height=22, bg="white")
    173. canvas.place(x=110, y=60)
    174. # 显示下载进度
    175. def progress():
    176. # 填充进度条
    177. fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="green")
    178. x = 500 # 未知变量,可更改
    179. n = 465 / x # 465是矩形填充满的次数
    180. for i in range(x):
    181. n = n + 465 / x
    182. canvas.coords(fill_line, (0, 0, n, 60))
    183. window.update()
    184. time.sleep(0.02) # 控制进度条流动的速度
    185. # 清空进度条
    186. fill_line = canvas.create_rectangle(1.5, 1.5, 0, 23, width=0, fill="white")
    187. x = 500 # 未知变量,可更改
    188. n = 465 / x # 465是矩形填充满的次数
    189. for t in range(x):
    190. n = n + 465 / x
    191. # 以矩形的长度作为变量值更新
    192. canvas.coords(fill_line, (0, 0, n, 60))
    193. window.update()
    194. time.sleep(0) # 时间为0,即飞速清空进度条
    195. btn_download = tk.Button(window, text='开始安装', command=progress)
    196. btn_download.place(x=400, y=105)
    197. window.mainloop()
    198. import time
    199. import tkinter
    200. import tkinter.simpledialog
    201. import tkinter.colorchooser
    202. import tkinter.filedialog
    203. from PIL import Image, ImageTk, ImageGrab
    204. def center_window(w, h):
    205. app.winfo_screenwidth()
    206. app.winfo_screenheight()
    207. app.geometry('%dx%d' % (w, h))
    208. app = tkinter.Tk()
    209. app.title('画板')
    210. x = 1200
    211. y = 800
    212. center_window(x, y)
    213. yesno = tkinter.IntVar(value=0)
    214. what = tkinter.IntVar(value=1)
    215. X = tkinter.IntVar(value=0)
    216. Y = tkinter.IntVar(value=0)
    217. foreColor = '#000000'
    218. backColor = '#FFFFFF'
    219. image = tkinter.PhotoImage()
    220. canvas = tkinter.Canvas(app, bg='white', width=x, height=y)
    221. canvas.create_image(x, y, image=image)
    222. lastDraw = 0
    223. end = [0]
    224. size = "20"
    225. def getter(widget):
    226. time.sleep(0.5)
    227. x = app.winfo_x() + widget.winfo_x()
    228. y = app.winfo_y() + widget.winfo_y()
    229. if app.winfo_x() < 0:
    230. x = 0
    231. if app.winfo_y() < 0:
    232. y = 0
    233. x1 = x + widget.winfo_width() + 200
    234. y1 = y + widget.winfo_height() + 200
    235. filename = tkinter.filedialog.asksaveasfilename(filetypes=[('.jpg', 'JPG')],
    236. initialdir='C:\\Users\\lin042\\Desktop\\')
    237. ImageGrab.grab().crop((x, y, x1, y1)).save(filename)
    238. def onLeftButtonDown(event):
    239. yesno.set(1)
    240. X.set(event.x)
    241. Y.set(event.y)
    242. if what.get() == 4:
    243. canvas.create_text(event.x, event.y, font=("等线", int(size)), text=text, fill=foreColor)
    244. what.set(1)
    245. def onLeftButtonMove(event):
    246. global lastDraw
    247. if yesno.get() == 0:
    248. return
    249. if what.get() == 1:
    250. lastDraw = canvas.create_line(X.get(), Y.get(), event.x, event.y,
    251. fill=foreColor)
    252. X.set(event.x)
    253. Y.set(event.y)
    254. elif what.get() == 2:
    255. try:
    256. canvas.delete(lastDraw)
    257. except Exception:
    258. pass
    259. lastDraw = canvas.create_line(X.get(), Y.get(), event.x, event.y,
    260. fill=foreColor)
    261. elif what.get() == 3:
    262. try:
    263. canvas.delete(lastDraw)
    264. except Exception:
    265. pass
    266. lastDraw = canvas.create_rectangle(X.get(), Y.get(), event.x, event.y,
    267. outline=foreColor)
    268. elif what.get() == 5:
    269. lastDraw = canvas.create_rectangle(event.x - 10, event.y - 10, event.x + 10, event.y + 10,
    270. outline=backColor)
    271. elif what.get() == 6:
    272. try:
    273. canvas.delete(lastDraw)
    274. except Exception:
    275. pass
    276. lastDraw = canvas.create_oval(X.get(), Y.get(), event.x, event.y,
    277. fill=backColor, outline=foreColor)
    278. def onLeftButtonUp(event):
    279. global lastDraw
    280. if what.get() == 2:
    281. lastDraw = canvas.create_line(X.get(), Y.get(), event.x, event.y, fill=foreColor)
    282. elif what.get() == 3:
    283. lastDraw = canvas.create_rectangle(X.get(), Y.get(), event.x, event.y, outline=foreColor)
    284. elif what.get() == 6:
    285. lastDraw = canvas.create_oval(X.get(), Y.get(), event.x, event.y, outline=foreColor)
    286. yesno.set(0)
    287. end.append(lastDraw)
    288. def onRightButtonUp(event):
    289. menu.post(event.x_root, event.y_root)
    290. canvas.bind('', onLeftButtonDown)
    291. canvas.bind('', onLeftButtonMove)
    292. canvas.bind('', onLeftButtonUp)
    293. canvas.bind('', onRightButtonUp)
    294. canvas.pack(fill=tkinter.BOTH, expand=tkinter.YES)
    295. '''主菜单及其关联的函数'''
    296. menu = tkinter.Menu(app, bg="red")
    297. app.config(menu=menu)
    298. def Open():
    299. filename = tkinter.filedialog.askopenfilename(title='导入图片',
    300. filetypes=[('image', '*.jpg *.png *.gif')])
    301. if filename:
    302. global image
    303. image = Image.open(filename)
    304. image = image.resize((800, 600), Image.ANTIALIAS)
    305. image = ImageTk.PhotoImage(image)
    306. canvas.create_image(400, 300, image=image)
    307. menu.add_command(label='导入', command=Open)
    308. def Save():
    309. getter(canvas)
    310. menu.add_command(label='保存', command=Save)
    311. def Clear():
    312. global lastDraw, end
    313. for item in canvas.find_all():
    314. canvas.delete(item)
    315. end = [0]
    316. lastDraw = 0
    317. menu.add_command(label='清屏', command=Clear)
    318. def Back():
    319. global end
    320. try:
    321. for i in range(end[-2], end[-1] + 1):
    322. canvas.delete(i)
    323. end.pop()
    324. except:
    325. end = [0]
    326. menu.add_command(label='撤销', command=Back)
    327. menu.add_separator()
    328. '''子菜单及其关联的函数'''
    329. menuType = tkinter.Menu(menu, tearoff=0)
    330. def drawCurve():
    331. what.set(1)
    332. menuType.add_command(label='铅笔', command=drawCurve)
    333. def drawLine():
    334. what.set(2)
    335. menuType.add_command(label='直线', command=drawLine)
    336. def drawRectangle():
    337. what.set(3)
    338. menuType.add_command(label='矩形', command=drawRectangle)
    339. def drawCircle():
    340. what.set(6)
    341. menuType.add_command(label='圆形', command=drawCircle)
    342. def drawText():
    343. global text, size
    344. text = tkinter.simpledialog.askstring(title='输入文本', prompt='')
    345. if text is not None:
    346. size = tkinter.simpledialog.askinteger('输入字号', prompt='', initialvalue=20)
    347. if size is None:
    348. size = "20"
    349. what.set(4)
    350. menuType.add_command(label='文本', command=drawText)
    351. def onErase():
    352. what.set(5)
    353. menuType.add_command(label='橡皮擦', command=onErase)
    354. menuType.add_separator()
    355. def chooseForeColor():
    356. global foreColor
    357. foreColor = tkinter.colorchooser.askcolor()[1]
    358. menuType.add_command(label='选择前景色', command=chooseForeColor)
    359. def chooseBackColor():
    360. global backColor
    361. backColor = tkinter.colorchooser.askcolor()[1]
    362. menuType.add_command(label='选择背景色', command=chooseBackColor)
    363. menu.add_cascade(label='工具栏', menu=menuType)
    364. app.mainloop()

    另其版本:

     浔川画板v4.0 :浔川画板v4.0——浔川python科技社-CSDN博客

    浔川画板v3.1 :

    浔川画板v3.1——浔川python社、浔川python科技社-CSDN博客

    浔川画板v3.0 :

    浔川画板v3.0——浔川python社-CSDN博客

     浔川画板v2.0 :

    浔川画板v2.0——浔川python社-CSDN博客

    浔川画板:

    浔川python社画板——浔川总社部-CSDN博客

  • 相关阅读:
    Unity3D XML与Properties配置文件读取详解
    Java 基于 SpringBoot 的在线学习平台
    VSCode:用户自定义模版(通用)
    Unity UI Toolkit学习笔记-Visual Tree
    【推理引擎】如何在 ONNXRuntime 中添加新的算子
    这是什么代码帮我看看
    【项目实战】多租户实现数据库动态切换
    导致MySQL索引失效的场景(随时补充)
    说一说 Backscatter communication
    空间曲面的法向量,法线,切平面
  • 原文地址:https://blog.csdn.net/2401_83104529/article/details/139719169