python gui tkinter快速入门教程 | python tkinter tutorial

本文首发于个人博客https://kezunlin.me/post/d5c57f56/,欢迎阅读最新内容!

python tkinter tutorial

Guide

main ui

messagebox
- showinfo()
- showwarning()
- showerror()
- askquestion()
- askokcancel()
- askyesno()
- askretrycancel()
- askyesnocancel()


filedialog
- asksaveasfilename() 
- asksaveasfile()
- askopenfilename()
- askopenfile()
- askdirectory()
- askopenfilenames()
- askopenfiles()

demo


from numpy.random import seed, uniform
from numpy import uint8, uint16, load, save

from cv2 import imread, imwrite
from os import listdir, makedirs
from os.path import exists, basename

# for python 3
from tkinter import Tk, Frame, messagebox, filedialog, Button, Label, StringVar

class MyGUI():
    def __init__(self):

        self.root = Tk()

        sw = self.root.winfo_screenwidth()
        sh = self.root.winfo_screenheight()
       
        ww = 700
        wh = 200
        x = (sw-ww) / 2
        y = (sh-wh) / 2
        self.root.title('Image Compress Tool')
        # center
        self.root.geometry("%dx%d+%d+%d" % (ww, wh, x, y))

        # frame1
        frame1 = Frame(self.root) 
        frame1.grid(row=0, column=0, sticky='w')

        self.input_btn = Button(frame1, text="Input Folder", width=10, height=3, command=self.set_input_folder)
        self.input_btn.pack(side='left')

        self.input_label_text = StringVar()
        self.input_label_text.set("Input Folder")

        self.input_label = Label(frame1, textvariable=self.input_label_text, width=70, height=3)
        self.input_label.pack(side='left') 

        # frame2
        frame2 = Frame(self.root) 
        frame2.grid(row=1, column=0, sticky='w')
        
        self.output_btn = Button(frame2, text="Output Folder", width=10, height=3, command=self.set_output_folder)
        self.output_btn.pack(side='left')

        self.output_label_text = StringVar()
        self.output_label_text.set("Output Folder")

        self.output_label = Label(frame2, textvariable = self.output_label_text, width=70, height=3)
        self.output_label.pack(side='left') 

        # frame3
        frame3 = Frame(self.root)
        frame3.grid(row=2, column=0, sticky='nw')
        
        self.run_btn = Button(frame3, text="执行加密", width=10, height=3, command=self.run_task)
        self.run_btn.pack(side='left')

        self.run_label_text = StringVar()
        self.run_label_text.set("Ready")

        self.run_label = Label(frame3, textvariable = self.run_label_text, width=70, height=3)
        self.run_label.pack(side='left') 

    def mainloop(self):
        self.root.mainloop() 

    def set_input_folder(self):
        result = filedialog.askdirectory()
        self.input_label_text.set(result)
        
    def set_output_folder(self):
        result = filedialog.askdirectory()
        self.output_label_text.set(result)
    
    def run_task(self):
        input_folder = self.input_label_text.get()
        output_folder = self.output_label_text.get()
        #print("input_folder: "+input_folder)
        #print("output_folder: "+output_folder)
        if exists(input_folder):
            #batch_compress(input_folder, output_folder)
            self.run_label_text.set("Compress OK.")
            messagebox.showinfo("Info", "Compress OK.")
        else:
            messagebox.showwarning("Warn", "Please input folder")

def gui():
    app = MyGUI()
    app.mainloop()

def main():
    gui()

if __name__ =="__main__":
    main()

snapshots

Reference

  • tkinter demo
  • tkinter docs

History

  • 20190411: created.

Copyright

  • Post author: kezunlin
  • Post link: https://kezunlin.me/post/d5c57f56/
  • Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.

你可能感兴趣的:(python)