保存这个按钮怎么没用


from tkinter import *   #引入tkinter库
from PIL import Image    #引入PIL库的Imgae子模块
from PIL import ImageTk    #引入PIL库的ImageTk子模块
import qrcode   #引入qrcode库
 
def create_qrcode(text, filename):
    """
    生成二维码图片
    """
    qr = qrcode.QRCode(
        version=None,
        error_correction=qrcode.ERROR_CORRECT_H,
        box_size=5,
        border=1
    )
    qr.make(fit=True)
    qr.add_data(text)
    # fill_color和back_color参数改变生成图片的格子颜色和背景颜色
    img = qr.make_image(fill_color="Tan", back_color="white")
    img.save(filename + '.png')
    
def callback():
    # 获取文本框内的内容
    text_input = t.get(0.0, "end")
    if text_input=='\n':
        text_input = 'http://www.baidu.com'
    # 定义图片的名字为输入内容的第一个字符
    img_name = text_input[0:1]
    #生成二维码图片
    create_qrcode(text_input, img_name)
    #保存二维码图片至本地
    img = Image.open(img_name + '.png')
    #设定图片大小
    img = img.resize((320, 320))
    #重新在窗体中显示新生成的二维码图片,覆盖刚才的图片
    img = ImageTk.PhotoImage(img)
    label.configure(image=img)
    label.image = img
 
if __name__ == "__main__":
     
     root = Tk() #新建窗口
     root.geometry("800x600") #窗口大小
     root.resizable(False,False) #禁止改变窗口大小
     root.title('二维码生成器')  #窗口标题
     img = qrcode.make('https://www.baidu.com')  #将这个网站转换为二维码图片
     img.save('d:\\hello.png') #保存二维码图片到本地  【这句话运行不了】
     img = Image.open('d:\\hello.png') #打开本地二维码图片
     img = ImageTk.PhotoImage(img)  #根据本地图片生成窗体图片
 
     lbl = Label(root, text = '名称:', font=("黑体", 14), width=5, height=1,foreground='red')  #名称标签,前景色设红色
     lbl.place(x=15,y=10)  #二维码图片位置
 
     t = Text(root,width=36, height=15, font=("黑体", 16)) #在窗体嵌入输入框
     t.place(x=15, y=45) #输入框的位置
 
     btn1 = Button(root, text='点我生成二维码', width=20, font=("黑体", 14), command=callback)  #按钮,按钮相应函数 callback
     btn1.place(x=120, y=480)  #按钮位置
 
     btn2 = Button(root, text='保存', width=20, font=("黑体", 14))  #按钮2相应函数command=yourfunc请自己编制
     btn2.place(x=480, y=480)  #按钮位置
     
     label = Label(root, image=img, width=400, height=400) #嵌入二维码图片
     label.place(x=400,y=0)  #二维码图片位置
 
     root.mainloop()
 

按钮2 保存 这个按钮该如何编写!?

img


from tkinter import *  # 引入tkinter库
from tkinter import filedialog
from PIL import Image  # 引入PIL库的Imgae子模块
from PIL import ImageTk  # 引入PIL库的ImageTk子模块
import qrcode  # 引入qrcode库
 
 
def create_qrcode(text, filename):
    """
    生成二维码图片
    """
    qr = qrcode.QRCode(
        version=None,
        error_correction=qrcode.ERROR_CORRECT_H,
        box_size=5,
        border=1
    )
    qr.make(fit=True)
    qr.add_data(text)
    # fill_color和back_color参数改变生成图片的格子颜色和背景颜色
    img = qr.make_image(fill_color="Tan", back_color="white")
    img.save(filename + '.png')
 
def save_qr():
    # 获取文本框内的内容
    text_input = t.get(0.0, "end")
    # print(text_input)
    if text_input == '\n':
        text_input = 'http://www.baidu.com'
    # 定义图片的名字为输入内容的第一个字符
    img_name = filedialog.asksaveasfilename()
    # print(img_name)
    # 生成二维码图片
    create_qrcode(text_input, img_name)
 
def callback():
    # 获取文本框内的内容
    text_input = t.get(0.0, "end")
    if text_input == '\n':
        text_input = 'http://www.baidu.com'
    # 定义图片的名字为输入内容的第一个字符
    img_name = text_input[0:1]
    # 生成二维码图片
    create_qrcode(text_input, img_name)
    # 保存二维码图片至本地
    img = Image.open(img_name + '.png')
    # 设定图片大小
    img = img.resize((320, 320))
    # 重新在窗体中显示新生成的二维码图片,覆盖刚才的图片
    img = ImageTk.PhotoImage(img)
    label.configure(image=img)
    label.image = img
 
 
if __name__ == "__main__":
    root = Tk()  # 新建窗口
    root.geometry("800x600")  # 窗口大小
    root.resizable(False, False)  # 禁止改变窗口大小
    root.title('二维码生成器')  # 窗口标题
    img = qrcode.make('https://www.baidu.com')  # 将这个网站转换为二维码图片
    img.save('d:\\hello.png')  # 保存二维码图片到本地
    img = Image.open('d:\\hello.png')  # 打开本地二维码图片
    img = ImageTk.PhotoImage(img)  # 根据本地图片生成窗体图片
    lbl = Label(root, text='名称:', font=("黑体", 14), width=5, height=1, foreground='red')  # 名称标签,前景色设红色
    lbl.place(x=15, y=10)  # 二维码图片位置
    t = Text(root, width=36, height=15, font=("黑体", 16))  # 在窗体嵌入输入框
    t.place(x=15, y=45)  # 输入框的位置
    btn1 = Button(root, text='点我生成二维码', width=20, font=("黑体", 14), command=callback)  # 按钮,按钮相应函数 callback
    btn1.place(x=120, y=480)  # 按钮位置
 
    btn2 = Button(root, text='保存', width=20, font=("黑体", 14), command=save_qr)  # 按钮2
    btn2.place(x=480, y=480)  # 按钮位置
 
    label = Label(root, image=img, width=400, height=400)  # 嵌入二维码图片
    label.place(x=400, y=0)  # 二维码图片位置
    root.mainloop()
 
 
 

给这个保存按钮绑定命令commind,然后你这个命令对应一个函数,你这个函数然后就把这个生成的二维码保存就行了呀,with open就行了


btn1 = Button(root, text='点我生成二维码并保存', width=20, font=("黑体", 14), command=callback)  # 按钮,按钮相应函数 callback
    btn1.place(x=120, y=480)  # 按钮位置

    btn2 = Button(root, text='退出', width=20, font=("黑体", 14),command=root.quit)  
    btn2.place(x=480, y=480)  # 按钮位置

img