python tkinter 制作九宫格

import tkinter as tk
import tkinter.messagebox as msgbox
from tkinter.filedialog import askopenfilename
from image_slicer import slice


class Application(tk.Tk):
    def __init__(self):
        super().__init__()
        
        self.img_path = tk.StringVar()
        self.init_ui()
        
        
    def init_ui(self):
        self.title("制作九宫格")
        self.geometry("300x100")
        self.iconbitmap("favicon.ico")

        self.root = tk.Frame(self)
        
        self.lbl_path = tk.Label(self.root, text="图片:")
        self.lbl_path.grid(row=0, column=0)
        
        self.txt_path = tk.Entry(self.root, width=21, textvariable=self.img_path)
        self.txt_path.grid(row=0, column=1, padx=5, pady=5, sticky=tk.W)
        
        self.btn_sel = tk.Button(self.root, text="选择文件", command=self.sel_img_file)
        self.btn_sel.grid(row=0, column=2, padx=5, pady=5, sticky=tk.W)
        
        self.lbl_tiles = tk.Label(self.root, text="数量:")
        self.lbl_tiles.grid(row=1, column=0)
        
        self.spn_tiles = tk.Spinbox(self.root, values=(4, 6, 9))
        self.spn_tiles.grid(row=1, column=1, padx=5, pady=5, sticky=tk.W)
        
        self.btn_slice = tk.Button(self.root, text="切割图片", command=self.slice_img)
        self.btn_slice.grid(row=1, column=2, padx=5, pady=5, sticky=tk.W)

        self.root.pack(fill=tk.BOTH, expand=True, padx=10, pady=10)
        
    def sel_img_file(self):
        img_file = askopenfilename(title="选择图片", initialdir=".", filetypes=(('JPG 图片', '*.jpg'), ('PNG 图片', '*.png'), ('所有文件', '*.*')))
        self.img_path.set(img_file)
        
    def slice_img(self):
        if self.img_path:
            slice(self.img_path.get(), int(self.spn_tiles.get()))


if __name__ == "__main__":
    app = Application()
    app.mainloop()

你可能感兴趣的:(python,python,开发语言)