0%

python tkinter tutorial

Guide

main ui

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
messagebox
- showinfo()
- showwarning()
- showerror()
- askquestion()
- askokcancel()
- askyesno()
- askretrycancel()
- askyesnocancel()


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

demo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

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
tkinter demo

Reference

History

  • 20190411: created.