-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtkImageSelectionButtonWithClear.py
More file actions
105 lines (91 loc) · 3.08 KB
/
tkImageSelectionButtonWithClear.py
File metadata and controls
105 lines (91 loc) · 3.08 KB
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
99
100
101
102
103
104
from Tkinter import *
from PIL import Image, ImageTk, ImageChops
class tkImageSelectionButtonWithClear(Frame):
def __init__(self, parent, **options):
self.framekeys = ['bd', 'borderwidth', 'class', 'relief', 'background', 'bg', 'colormap', 'container', 'cursor', 'height', 'highlightbackground', 'highlightcolor', 'highlightthickness', 'padx', 'pady', 'takefocus', 'visual', 'width']
frameoptions = {}
self.otheroptions = {}
for item in options.keys():
if item in self.framekeys:
frameoptions[item] = options[item]
else:
self.otheroptions[item] = options[item]
if "command" in self.otheroptions.keys():
self.command = self.otheroptions["command"]
else:
command = lambda: self.actnull()
self.command = command
if "xcommand" in self.otheroptions.keys():
self.xcommand = self.otheroptions["xcommand"]
else:
xcommand = lambda: self.actnull()
self.xcommand = xcommand
if "image" in self.otheroptions.keys():
self.image = self.otheroptions["image"]
else:
self.notexturehandle = Image.open("notexture.png")
self.notex = self.notexturehandle.resize((40, 40), Image.BICUBIC)
self.notexture = ImageTk.PhotoImage(self.notex)
self.image = self.notexture
if "text" in self.otheroptions.keys():
self.buttontxt = self.otheroptions["text"]
else:
self.buttontxt = "defaulttxt"
Frame.__init__(self, parent, **frameoptions)
self.draw_widget()
def draw_widget(self):
self.frame = Frame(self)
self.mainbutton = Button(self.frame, text=self.buttontxt, image=self.image, compound=TOP, command=self.mcallback, height = 64, width = 64)
self.mainbutton.pack()
self.xbutton = Button(self.frame, text="x", command=self.xcallback)
self.frame.bind("<Enter>", self.packxbutton)
self.frame.bind("<Leave>", self.unpackxbutton)
self.frame.pack()
def mcallback(self):
self.command()
def xcallback(self):
self.xcommand()
def actnull(self):
return
def packxbutton(self, mouse):
if self.image != self.notexture:
self.xbutton.place(in_=self.mainbutton, x=53, y=0)
def unpackxbutton(self, mouse):
self.xbutton.place_forget()
def onmodcommand(self, value):
self.mainbutton.config(text=self.buttontxt, image=self.image)
def config(self, **options):
frameoptions = {}
self.otheroptions = {}
for item in options.keys():
if item in self.framekeys:
frameoptions[item] = options[item]
else:
self.otheroptions[item] = options[item]
#self.config(**frameoptions)
if "command" in self.otheroptions.keys():
self.command = self.otheroptions["command"]
if "xcommand" in self.otheroptions.keys():
self.xcommand = self.otheroptions["xcommand"]
if "image" in self.otheroptions.keys():
self.image = self.otheroptions["image"]
if "text" in self.otheroptions.keys():
self.buttontxt = self.otheroptions["text"]
if self.otheroptions != {}:
self.onmodcommand(0)
#class App:
#
# def __init__(self, master):
#
# frame = Frame(master)
# frame.pack()
#
# f = tkImageSelectionButtonWithClear(frame, text="sometext", blah="garg")
# #f.config(test="Blah", var1="bulge")
# f.pack()
#
#root = Tk()
#
#app = App(root)
#
#root.mainloop()