python学习笔记(二)--高级部分

这篇博客涵盖了Python的高级主题,包括数据库操作(sqlite和MySQL)、Tkinter窗口应用(如按钮、图片和对话框),网络编程(UDP和TCP)以及多线程和图像处理。讲解了如何使用SQLite进行数据回滚,Tkinter中的各种控件用法,爬虫技术(如设置HTTP头、代理和异常处理),以及socket编程中的UDP和TCP通信。同时介绍了多线程中的Condition对象和Pillow库进行图像处理。
摘要由CSDN通过智能技术生成


上篇基础部分: https://blog.csdn.net/wang342626/article/details/88904281


数据库

sqlite数据库

# 不用安装,自带模块
import sqlite3

# 创建连接对象,并且直接生成db文件,可用Navicat打开
connection = sqlite3.connect('D:/example.db')

# 执行sql语句
connection.execute("""create table student(sid int,name varchar(50) )""")
connection.execute("""insert into student values (1,'wang')""")
connection.execute("""insert into student values (?,?)""",('2','zhang'))  # 占位符

# 提交之后才能修改数据库
connection.commit()

# 查询语句
for row in connection.execute("""select * from student"""):
    print(row)

connection.close()

connection.rollback() 回滚

还可以使用cursor对象执行sql语句

import sqlite3

# 创建连接对象和cursor对象
connection = sqlite3.connect('D:/example.db')
cursor = connection.cursor()

# cursor执行sql语句
cursor.execute("""create table student(sid int,name varchar(50) )""")
cursor.execute("""insert into student values (1,'wang')""")
cursor.execute("""insert into student values (?,?)""",('2','zhang'))  # 占位符使用问号,不是%s

# 提交之后才能修改数据库
connection.commit()

# 查询语句
cursor.execute("""select * from student""")
res = cursor.fetchall()
for row in res:
    for col in row:
        print(col,end=' ')
    print('')

connection.close()

MySQL数据库

import pymysql

# 连接对象,必须要使用cursor执行sql语句
conn = pymysql.connect(host='localhost',port=3306,user='root',passwd='你的密码',db='data')
cursor =conn.cursor()

# 执行sql语句
cursor.execute(""" create table student(sid int,name varchar(50) )""")
num = cursor.execute("""insert into student values (1,'wang')""")
print(num)

# 格式化sql语句
sql = " insert into student values({},{}) ".format(1,'zhang')
print(sql)

num = cursor.execute(" insert into student values(%s,%s) ",(1,'zhang') )  # 占位符一律用%s,不用%d,也不用问号
print(num)

num = cursor.executemany("insert into student values (%s,%s)",[(3,'lisi'),(4,'xiaoming')]) #多个插入语句,使用列表
print(num)

conn.commit()


# 执行查询
res = cursor.execute("select * from student")
one = cursor.fetchone()
print(one)
many = cursor.fetchmany(2) #获取前2个结果
print(many)
all = cursor.fetchall() #获取剩下的所有结果
print(all)

conn.close()

窗口tkinter

更多详细教程: https://blog.csdn.net/jacky_zhuyuanlu/article/details/77475635

import tkinter as tk

# 生成顶层窗口root
root = tk.Tk()
root.title("这是标题")

# 生成一个标签
lablel = tk.Label(root,text="标签1")
lablel.pack()

# 显示窗口
root.mainloop()

设置文字左对齐, 文字左边距10
label = tk.Label(root,text="标签",justify=tk.LEFT,padx=10)

调整显示位置(上下左右), 调整边距
label.pack(side=tk.RIGHT,padx=10,pady=10)

按钮

import tkinter as tk

def fun1():
    print("按钮点击了,这是函数1")

# 顶层窗口root
root = tk.Tk()
root.title("这是标题")

# 按钮,点击事件使用cammand调用函数fun1
btn = tk.Button(root,text="打招呼",fg="blue",command=fun1)
btn.pack()

root.mainloop()

点击按钮修改标签文字

import tkinter as tk

def fun1():
    var.set("修改文字")

# 顶层窗口root
root = tk.Tk()
root.title("这是标题")

# 标签
var = tk.StringVar() # 修改的文字变量
var.set("标签的文字")
label = tk.Label(root,textvariable=var)
label.pack()

# 按钮,点击调用函数fun1
btn = tk.Button(root,text="修改标签的文字",fg="blue",command=fun1)
btn.pack()

root.mainloop()

图片

初始化加载图片 (仅支持gif,其他格式会报错couldn't recognize data in image file "D://1.jpg")

import tkinter as tk

# 顶层窗口root
root = tk.Tk()
root.title("标题")

# 图片包含在label里
img = tk.PhotoImage(file="D://1.gif") #只支持gif
label = tk.Label(root,image=img)
label.pack(side=tk.LEFT)

root.mainloop()

点击按钮加载图片 (仅支持gif,其他格式会报错couldn't recognize data in image file "D://1.jpg")

import tkinter as tk
import tkinter.filedialog

def choosepic():
    path =tk.filedialog.askopenfilename()
    varPath.set(path)

    # 动态给label设置图片,仅限于gif格式图片
    img_gif = tk.PhotoImage(file=path)  # file=不能少
    l1.config(image=img_gif)
    l1.image=img_gif



root = tk.Tk()
root.geometry('200x300')  # 不能限定窗口大小

varPath = tk.StringVar()
btn = tk.Button(root, text='选择图片', command=choosepic).pack() #点击按钮,选择图片
e1 =
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值