文章目录
上篇基础部分: 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 =