rsj0315 发表于 2021-9-24 20:40:34

tkinter制作一个工卡牌的小软件

# import uuid
import datetime
from PIL import Image, ImageDraw,ImageFont,ImageTk
import qrcode
import tkinter as tk
import tkinter.ttk as ttk

class Application(tk.Tk):
    def __init__(self):
      super().__init__()
      self.title("工卡制作")
      self.geometry("300x300")
      self.init_widgets()

    def draw_pic(self):
      image = Image.new("RGB", (402, 603), (240, 240, 240))
      draw = ImageDraw.Draw(image)

      head_font = ImageFont.truetype('simsun.ttc', 25, encoding='unic')
      color = 'black'
      font = ImageFont.truetype('simsun.ttc', 20, encoding='unic')

      logo = Image.open('11.jpg')
      image.paste(logo, (0, 0))

      company = "XXXXXXXXXX有限公司"
      draw.text((25, 100), company, fill=color, font=head_font)

      photo = Image.open('33.jpg')
      image.paste(photo, (60, 150))

      # uid = uuid.uuid4().hex[:8].upper()
      uid = datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')
      # print(uid)
      name = self.txt_username.get()
      draw.text((60, 400), f"姓名:{name}", fill=color, font=font)

      gender = self.var.get()
      draw.text((60, 450), f"性别:{gender}", fill=color, font=font)

      dept = self.txt_dept.get()
      draw.text((60, 500), f"部门:{dept}", fill=color, font=font)

      title = self.txt_title.get()
      draw.text((60, 550), f"职位:{title}", fill=color, font=font)

      qr = qrcode.make(f"{uid}~{name}")
      qr = qr.resize((120, 120))
      image.paste(qr, (250, 400))

      draw.rectangle(((0, 0), (400, 600)), fill=None, outline='#337ab7', width=5)

      image.show()
      image.save(f"{uid}.png")

    def init_widgets(self):
      photo = ImageTk.PhotoImage(Image.open("bg.jpg"))
      self.lbl_img = tk.Label(self, image=photo, width=300, height=50)
      self.lbl_img.image = photo
      self.lbl_img.pack()

      self.frame = tk.Frame(self)

      self.lbl_username = tk.Label(self.frame, text="姓名:", anchor=tk.W)
      self.lbl_username.pack(fill=tk.X)
      self.txt_username = tk.Entry(self.frame)
      self.txt_username.pack(fill=tk.X)

      self.lbl_gender = tk.Label(self.frame, text="性别:", anchor=tk.W)
      self.lbl_gender.pack(fill=tk.X)
      self.var = tk.StringVar()
      self.txt_gender = ttk.Combobox(self.frame,textvariable=self.var,value=('男','女'))
      self.txt_gender.pack(fill=tk.X)

      self.lbl_dept = tk.Label(self.frame, text="部门:", anchor=tk.W)
      self.lbl_dept.pack(fill=tk.X)
      self.txt_dept = tk.Entry(self.frame)
      self.txt_dept.pack(fill=tk.X)

      self.lbl_title = tk.Label(self.frame, text="职位:", anchor=tk.W)
      self.lbl_title.pack(fill=tk.X)
      self.txt_title = tk.Entry(self.frame)
      self.txt_title.pack(fill=tk.X)

      self.btn_login = tk.Button(self.frame, text="制作", width=16,command=self.draw_pic)
      self.btn_login.pack(side=tk.LEFT, pady=10)

      self.btn_cancel = tk.Button(self.frame, text="取消", width=16, command=lambda: self.destroy())
      self.btn_cancel.pack(side=tk.RIGHT, pady=10)
      self.frame.pack(padx=20, pady=5, fill=tk.X)


if __name__ == "__main__":
    app = Application()
    app.mainloop()
页: [1]
查看完整版本: tkinter制作一个工卡牌的小软件