-
Notifications
You must be signed in to change notification settings - Fork 0
/
breezypythongui.py
1142 lines (974 loc) · 46 KB
/
breezypythongui.py
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
File: breezypythongui.py
Version: 1.0
Copyright 2012 by Ken Lambert
Resources for easy Python GUIs.
LICENSE: This is open-source software released under the terms of the
GPL (http://www.gnu.org/licenses/gpl.html). Its capabilities mirror those
of BreezyGUI and BreezySwing, open-source frameworks for writing GUIs in Java,
written by Ken Lambert and Martin Osborne.
PLATFORMS: The package is a wrapper around tkinter (Python 3.X) and should
run on any platform where tkinter is available.
INSTALLATION: Put this file where Python can see it.
"""
import tkinter
import tkinter.simpledialog
N = tkinter.N
S = tkinter.S
E = tkinter.E
W = tkinter.W
CENTER = tkinter.CENTER
END = tkinter.END
NORMAL = tkinter.NORMAL
DISABLED = tkinter.DISABLED
NONE = tkinter.NONE
WORD = tkinter.WORD
VERTICAL = tkinter.VERTICAL
HORIZONTAL = tkinter.HORIZONTAL
RAISED = tkinter.RAISED
SINGLE = tkinter.SINGLE
ACTIVE = tkinter.ACTIVE
class EasyFrame(tkinter.Frame):
"""Represents an application window."""
def __init__(self, title = "", width = None, height = None,
background = "white", resizable = True):
"""Will shrink wrap the window around the widgets if width
and height are not provided."""
tkinter.Frame.__init__(self, borderwidth = 4, relief = "sunken")
if width and height:
self.setSize(width, height)
self.master.title(title)
self.grid()
# Expand the frame within the window
self.master.rowconfigure(0, weight = 1)
self.master.columnconfigure(0, weight = 1)
self.grid(sticky = N+S+E+W)
# Set the background color and resizability
self.setBackground(background)
self.setResizable(resizable)
def setBackground(self, color):
"""Resets the window's background color to color."""
self["background"] = color
def setResizable(self, state):
"""Resets the window's resizable property to True
or False."""
self.master.resizable(state, state)
def setSize(self, width, height):
"""Resets the window's width and height in pixels."""
self.master.geometry(str(width)+ "x" + str(height))
def setTitle(self, title):
"""Resets the window's title to title."""
self.master.title(title)
# Methods to add widgets to the window. The row and column in
# the grid are required arguments.
def addLabel(self, text, row, column,
columnspan = 1, rowspan = 1,
sticky = N+W, font = None,
background = "white", foreground = "black"):
"""Creates and inserts a label at the row and column,
and returns the label."""
label = tkinter.Label(self, text = text, font = font,
background = background,
foreground = foreground)
self.rowconfigure(row, weight = 1)
self.columnconfigure(column, weight = 1)
label.grid(row = row, column = column,
columnspan = columnspan, rowspan = rowspan,
padx = 5, pady = 5, sticky = sticky)
return label
def addButton(self, text, row, column,
columnspan = 1, rowspan = 1,
command = lambda: None,
state = NORMAL):
"""Creates and inserts a button at the row and column,
and returns the button."""
button = tkinter.Button(self, text = text,
command = command, state = state)
self.rowconfigure(row, weight = 1)
self.columnconfigure(column, weight = 1)
button.grid(row = row, column = column,
columnspan = columnspan, rowspan = rowspan,
padx = 5, pady = 5)
return button
def addFloatField(self, value, row, column,
columnspan = 1, rowspan = 1,
width = 20, precision = None,
sticky = N+E, state = NORMAL):
"""Creates and inserts a float field at the row and column,
and returns the float field."""
field = FloatField(self, value, width, precision, state)
self.rowconfigure(row, weight = 1)
self.columnconfigure(column, weight = 1)
field.grid(row = row, column = column,
columnspan = columnspan, rowspan = rowspan,
padx = 5, pady = 5, sticky = sticky)
return field
def addIntegerField(self, value, row, column,
columnspan = 1, rowspan = 1,
width = 10, sticky = N+E, state = NORMAL):
"""Creates and inserts an integer field at the row and column,
and returns the integer field."""
field = IntegerField(self, value, width, state)
self.rowconfigure(row, weight = 1)
self.columnconfigure(column, weight = 1)
field.grid(row = row, column = column,
columnspan = columnspan, rowspan = rowspan,
padx = 5, pady = 5, sticky = sticky)
return field
def addTextField(self, text, row, column,
columnspan = 1, rowspan = 1,
width = 20, sticky = N+E, state = NORMAL):
"""Creates and inserts a text field at the row and column,
and returns the text field."""
field = TextField(self, text, width, state)
self.rowconfigure(row, weight = 1)
self.columnconfigure(column, weight = 1)
field.grid(row = row, column = column,
columnspan = columnspan, rowspan = rowspan,
padx = 5, pady = 5, sticky = sticky)
return field
def addTextArea(self, text, row, column, rowspan = 1, columnspan = 1,
width = 80, height = 5, wrap = NONE):
"""Creates and inserts a multiline text area at the row and column,
and returns the text area. Vertical and horizontal scrollbars are
provided."""
frame = tkinter.Frame(self)
frame.grid(row = row, column = column,
columnspan = columnspan, rowspan = rowspan,
sticky = N+S+E+W)
self.columnconfigure(column, weight = 1)
self.rowconfigure(row, weight = 1)
xScroll = tkinter.Scrollbar(frame, orient = HORIZONTAL)
xScroll.grid(row = 1, column = 0, sticky = E+W)
yScroll = tkinter.Scrollbar(frame, orient = VERTICAL)
yScroll.grid(row = 0, column = 1, sticky = N+S)
area = TextArea(frame, text, width, height,
xScroll.set, yScroll.set, wrap)
area.grid(row = 0, column = 0,
padx = 5, pady = 5, sticky = N+S+E+W)
frame.columnconfigure(0, weight = 1)
frame.rowconfigure(0, weight = 1)
xScroll["command"] = area.xview
yScroll["command"] = area.yview
return area
def addListbox(self, row, column, rowspan = 1, columnspan = 1,
width = 10, height = 5, listItemSelected = lambda index: index):
"""Creates and inserts a scrolling list box at the row and column, with a
width and height in lines and columns of text, and a default item selection
method, and returns the list box."""
frame = tkinter.Frame(self)
frame.grid(row = row, column = column, columnspan = columnspan, rowspan = rowspan,
sticky = N+S+E+W)
self.columnconfigure(column, weight = 1)
self.rowconfigure(row, weight = 1)
yScroll = tkinter.Scrollbar(frame, orient = VERTICAL)
yScroll.grid(row = 0, column = 1, sticky = N+S)
listBox = EasyListbox(frame, width, height, yScroll.set, listItemSelected)
listBox.grid(row = 0, column = 0, sticky = N+S+E+W)
frame.columnconfigure(0, weight = 1)
frame.rowconfigure(0, weight = 1)
yScroll["command"] = listBox.yview
return listBox
def addCanvas(self, canvas = None, row = 0, column = 0,
rowspan = 1, columnspan = 1, width = 200, height = 100,
background = "white"):
"""Creates and inserts a canvas at the row and column,
and returns the canvas."""
if not canvas:
canvas = EasyCanvas(self, width = width, height = height,
background = background)
canvas.grid(row = row, column = column,
rowspan = rowspan, columnspan = columnspan,
sticky = W+E+N+S)
self.columnconfigure(column, weight = 10)
self.rowconfigure(row, weight = 10)
return canvas
def addScale(self, row, column, rowspan = 1, columnspan = 1,
command = lambda value: value, from_ = 0, to = 0,
label = "", length = 100, orient = HORIZONTAL,
resolution = 1, tickinterval = 0):
"""Creates and inserts a scale at the row and column,
and returns the scale."""
scale = tkinter.Scale(self, command = command, from_ = from_, to = to,
label = label, length = length,
orient = orient, resolution = resolution,
tickinterval = tickinterval, relief = "sunken",
borderwidth = 4)
self.rowconfigure(row, weight = 1)
self.columnconfigure(column, weight = 1)
scale.grid(row = row, column = column, columnspan = columnspan,
rowspan = rowspan, sticky = N+S+E+W)
return scale
def addMenuBar(self, row, column, rowspan = 1, columnspan = 1,
orient = "horizontal"):
"""Creates and inserts a menu bar at the row and column,
and returns the menu bar."""
if not orient in ("horizontal", "vertical"):
raise ValueError("orient must be horizontal or vertical")
menuBar = EasyMenuBar(self, orient)
menuBar.grid(row = row, column = column,
rowspan = rowspan, columnspan = columnspan,
sticky = N+W)
return menuBar
def addCheckbutton(self, text, row, column,
rowspan = 1, columnspan = 1,
sticky = N+S+E+W, command = lambda : 0):
"""Creates and inserts check button at the row and column,
and returns the check button."""
cb = EasyCheckbutton(self, text, command)
self.rowconfigure(row, weight = 1)
self.columnconfigure(column, weight = 1)
cb.grid(row = row, column = column,
columnspan = columnspan, rowspan = rowspan,
padx = 5, pady = 5, sticky = sticky)
return cb
def addRadiobuttonGroup(self, row, column,
rowspan = 1, columnspan = 1, orient = VERTICAL):
"""Creates and returns a radio button group."""
return EasyRadiobuttonGroup(self, row, column, rowspan, columnspan, orient)
# Added 12-18-2012
def addPanel(self, row, column,
rowspan = 1, columnspan = 1, background = "white"):
"""Creates and returns a panel."""
return EasyPanel(self, row, column, rowspan, columnspan, background)
# Method to pop up a message box from this window.
def messageBox(self, title = "", message = "", width = 25, height = 5):
"""Creates and pops up a message box, with the given title,
message, and width and height in rows and columns of text."""
dlg = MessageBox(self, title, message, width, height)
return dlg.modified()
# Method to pop up a prompter box from this window.
def prompterBox(self, title = "", promptString = "", inputText = "", fieldWidth = 20):
"""Creates and pops up a prompter box, with the given title, prompt,
input text, and field width in columns of text.
Returns the text entered at the prompt."""
dlg = PrompterBox(self, title, promptString, inputText, fieldWidth)
return dlg.getText()
# Classes for easy widgets
class AbstractField(tkinter.Entry):
"""Represents common features of float fields, integer fields,
and text fields."""
def __init__(self, parent, value, width, state):
self.var = tkinter.StringVar()
self.setValue(value)
tkinter.Entry.__init__(self, parent,
textvariable = self.var,
width = width, state = state)
def setValue(self, value):
self.var.set(value)
def getValue(self):
return self.var.get()
class FloatField(AbstractField):
"""Represents a single line box for I/O of floats."""
def __init__(self, parent, value, width, precision, state):
self.setPrecision(precision)
AbstractField.__init__(self, parent, value, width, state)
def getNumber(self):
"""Returns the float contained in the field.
Raises: ValueError if number format is bad."""
return float(self.getValue())
def setNumber(self, number):
"""Replaces the float contained in the field."""
self.setValue(self._precision % number)
def setPrecision(self, precision):
"""Resets the precision for the display of a float."""
if precision and precision >= 0:
self._precision = "%0." + str(precision) + "f"
else:
self._precision = "%f"
class IntegerField(AbstractField):
"""Represents a single line box for I/O of integers."""
def __init__(self, parent, value, width, state):
AbstractField.__init__(self, parent, value, width, state)
def getNumber(self):
"""Returns the integer contained in the field.
Raises: ValueError if number format is bad."""
return int(self.getValue())
def setNumber(self, number):
"""Replaces the integer contained in the field."""
self.setValue(str(number))
class TextField(AbstractField):
"""Represents a single line box for I/O of strings."""
def __init__(self, parent, value, width, state):
AbstractField.__init__(self, parent, value, width, state)
def getText(self):
"""Returns the string contained in the field."""
return self.getValue()
def setText(self, text):
"""Replaces the string contained in the field."""
self.setValue(text)
class TextArea(tkinter.Text):
"""Represents a box for I/O of multiline text."""
def __init__(self, parent, text, width, height,
xscrollcommand, yscrollcommand, wrap):
tkinter.Text.__init__(self, parent,
width = width,
height = height,
wrap = wrap,
xscrollcommand = xscrollcommand,
yscrollcommand = yscrollcommand)
self.setText(text)
def getText(self):
"""Returns the string contained in the text area."""
return self.get("1.0", END)
def setText(self, text):
"""Replaces the string contained in the text area."""
self.delete("1.0", END)
self.insert("1.0", text)
def appendText(self, text):
"""Inserts the text after the string contained in
the text area."""
self.insert(END, text)
class EasyListbox(tkinter.Listbox):
"""Represents a list box."""
def __init__(self, parent, width, height, yscrollcommand, listItemSelected):
"""Turn off exportselection to support selection in multiple list boxes."""
self._listItemSelected = listItemSelected
tkinter.Listbox.__init__(self, parent,
width = width, height = height,
yscrollcommand = yscrollcommand,
selectmode = SINGLE, exportselection = 0)
self.bind("<<ListboxSelect>>", self.triggerListItemSelected)
def triggerListItemSelected(self, event):
"""Strategy method to respond to an item selection in the list box.
Runs the client's listItemSelected method with the selected index if
there is one."""
if self.size() == 0: return
widget = event.widget
index = widget.curselection()[0]
self._listItemSelected(index)
def getSelectedIndex(self):
"""Returns the index of the selected item or -1 if no item
is selected."""
tup = self.curselection()
if len(tup) == 0:
return -1
else:
return int(tup[0])
def getSelectedItem(self):
"""Returns the selected item or the empty string if no item
is selected."""
index = self.getSelectedIndex()
if index == -1:
return ""
else:
return self.get(index)
def setSelectedIndex(self, index):
"""Selects the item at the index if it's in the range."""
if index < 0 or index >= self.size(): return
self.selection_set(index, index)
def clear(self):
"""Deletes all items from the list box."""
while self.size() > 0:
self.delete(0)
def getIndex(self, item):
"""Returns the index of item if it's in the list box,
or -1 otherwise."""
tup = self.get(0, self.size() - 1)
if item in tup:
return tup.index(item)
else:
return -1
class EasyRadiobuttonGroup(tkinter.Frame):
"""Represents a group of radio buttons, only one of which
is selected at any given time."""
def __init__(self, parent, row, column, rowspan, columnspan, orient):
tkinter.Frame.__init__(self, parent)
self.grid(row = row, column = column,
rowspan = rowspan, columnspan = columnspan,
sticky = N+S+E+W)
self._commonVar = tkinter.StringVar("")
self._buttons = dict()
self._orient = orient
self._buttonRow = self._buttonColumn = 0
def addRadiobutton(self, text, command = lambda : 0):
"""Creates a button with the given text and command, adds it to the group,
and returns the button."""
if text in self._buttons:
raise ValueError("Button with this label already in the group")
button = tkinter.Radiobutton(self, text = text, value = text,
command = command,
variable = self._commonVar)
self._buttons[text] = button
button.grid(row = self._buttonRow, column = self._buttonColumn, sticky = N+W)
if self._orient == VERTICAL:
self.rowconfigure(self._buttonRow, weight = 1)
self._buttonRow += 1
else:
self.columnconfigure(self._buttonColumn, weight = 1)
self._buttonColumn += 1
return button
def getSelectedButton(self):
if not self._commonVar.get() in self._buttons:
raise ValueError("No button has been selected yet.")
return self._buttons[self._commonVar.get()]
def setSelectedButton(self, button):
self._commonVar.set(button["value"])
class EasyCheckbutton(tkinter.Checkbutton):
"""Represents a check button."""
def __init__(self, parent, text, command):
self._variable = tkinter.IntVar()
tkinter.Checkbutton.__init__(self, parent, text = text,
variable = self._variable,
command = command)
def isChecked(self):
"""Returns True if the button is checked or
False otherwise."""
return self._variable.get() != 0
class EasyMenuBar(tkinter.Frame):
"""Represents a menu bar."""
def __init__(self, parent, orient):
self._orient = orient
self._row = self._column = 0
tkinter.Frame.__init__(self, parent, relief = RAISED, borderwidth = 1)
def addMenu(self, text, state = NORMAL):
"""Creates and inserts a menu into the
menubar, and returns the menu."""
menu = EasyMenubutton(self, text, state = state)
menu.grid(row = self._row, column = self._column)
if self._orient == "horizontal":
self._column += 1
else:
self._row += 1
return menu
class EasyMenubutton(tkinter.Menubutton):
"""Represents a menu button."""
def __init__(self, menuBar, text, state):
tkinter.Menubutton.__init__(self, menuBar,
text = text, state = state)
self.menu = tkinter.Menu(self)
self["menu"] = self.menu
self._currentIndex = -1
def addMenuItem(self, text, command, state = NORMAL):
"""Inserts a menu option in the given menu."""
self.menu.add_command(label = text, command = command, state = state)
self._currentIndex += 1
return EasyMenuItem(self, self._currentIndex)
class EasyMenuItem(object):
"""Represents an option in a drop-down menu."""
def __init__(self, menu, index):
self._menu = menu
self._index = index
def setState(self, state):
"""Sets the state of the item to state."""
self._menu.menu.entryconfigure(self._index, state = state)
class EasyCanvas(tkinter.Canvas):
"""Represents a rectangular area for interactive drawing of shapes.
Supports simple commands for drawing lines, rectangles, and ovals,
as well as methods for responding to mouse events in the canvas."""
def __init__(self, parent, width = None, height = None,
background = "white"):
tkinter.Canvas.__init__(self, parent,
width = width, height = height,
background = background)
self.bind("<Double-Button-1>", self.mouseDoubleClicked)
self.bind("<ButtonPress-1>", self.mousePressed)
self.bind("<ButtonRelease-1>", self.mouseReleased)
self.bind("<B1-Motion>", self.mouseDragged)
# Mouse event handling methods. One or more of these methods can
# be overridden in the subclass to implement the required actions.
# The event argument can be used to extract the current mouse
# cursor coordinates (event.x and event.y).
def mouseDoubleClicked(self, event):
"""Triggered when the mouse is
double-clicked in the area of this canvas."""
return
def mousePressed(self, event):
"""Triggered when the mouse is
pressed in the area of this canvas."""
return
def mouseReleased(self, event):
"""Triggered when the mouse is
released in the area of this canvas."""
return
def mouseDragged(self, event):
"""Triggered when the mouse is
dragged in the area of this canvas."""
return
def getWidth(self):
"""Returns the width of the canvas."""
return self["width"]
def getHeight(self):
"""Returns the height of the canvas."""
return self["height"]
def drawLine(self, x0, y0, x1, y1,
fill = "black", width = 1):
item = self.create_line(x0, y0, x1, y1)
self.itemconfig(item, fill = fill, width = width)
return item
def drawRectangle(self, x0, y0, x1, y1,
outline = "black", fill = None):
"""Draws a rectangle with the given corner points,
outline color, and fill color."""
item = self.create_rectangle(x0, y0, x1, y1)
self.itemconfig(item, outline = outline, fill = fill)
return item
def drawOval(self, x0, y0, x1, y1,
outline = "black", fill = None):
"""Draws an ovel within the given corner points,
with the given outline color and fill color."""
item = self.create_oval(x0, y0, x1, y1)
self.itemconfig(item, outline = outline, fill = fill)
return item
def drawText(self, text, x, y, fill = "black"):
"""Draws the given text (a string) at the given coordinates
with the given fill color. The string is centered vertically
and horizontally at the given coordinates."""
item = self.create_text(x, y)
self.itemconfig(item, text = text, fill = fill)
return item
def drawImage(self, image, x, y, anchor = CENTER):
"""Draws the given image (a PhotoImage) at the given coordinates.
The image is centered at the given coordinates by default."""
item = self.create_image(x, y, image = image,
anchor = anchor)
self.itemconfig(item, image = image, anchor = anchor)
return item
def deleteItem(self, item):
"""Removes and erases the shape with the given item
number from the canvas."""
self.delete(item)
# Support classes for dialogs.
class MessageBox(tkinter.simpledialog.Dialog):
"""Represents a message dialog with a scrollable text area."""
@classmethod
def message(cls, title = "", message = "", width = 25, height = 5):
MessageBox(tkinter.Frame(), title, message, width, height)
def __init__(self, parent, title, message, width, height):
"""Set up the window and widgets."""
self._message = message
self._width = width
self._height = height
self._modified = False
tkinter.simpledialog.Dialog.__init__(self, parent, title)
def body(self, master):
self.resizable(0, 0)
yScroll = tkinter.Scrollbar(master, orient = VERTICAL)
yScroll.grid(row = 0, column = 1, sticky = N+S)
output = tkinter.Text(master, width = self._width, height = self._height,
padx = 5, pady = 5, wrap = WORD,
yscrollcommand = yScroll.set)
output.grid(row = 0, column = 0, sticky = N+W+S+E)
output.insert("1.0", self._message)
output["state"] = DISABLED
yScroll["command"] = output.yview
return output
def buttonbox(self):
'''add standard button box.
override if you do not want the standard buttons'''
box = tkinter.Frame(self)
w = tkinter.Button(box, text="OK", width = 10,
command = self.ok, default = ACTIVE)
w.pack()
self.bind("<Return>", self.ok)
box.pack()
def apply(self):
"""Quits the dialog."""
self._modified = True
def modified(self):
return self._modified
class PrompterBox(tkinter.simpledialog.Dialog):
"""Represents an input dialog with a text field."""
@classmethod
def prompt(cls, title = "", promptString = "", inputText = "", fieldWidth = 20):
"""Creates and pops up an input dialog."""
dlg = PrompterBox(tkinter.Frame(), title, promptString, inputText, fieldWidth)
return dlg.getText()
def __init__(self, parent, title, promptString, inputText, fieldWidth):
"""Set up the window and widgets."""
self._prompt = promptString
self._text = inputText
self._width = fieldWidth
self._modified = False
tkinter.simpledialog.Dialog.__init__(self, parent, title)
def body(self, master):
self.resizable(0, 0)
label = tkinter.Label(master, text = self._prompt)
label.grid(row = 0, column = 0, padx = 5, sticky = N+W+S+E)
self._field = TextField(master, self._text, self._width, NORMAL)
self._field.grid(row = 1, column = 0, padx = 5, sticky = N+W+S+E)
return self._field
def buttonbox(self):
'''add standard button box.
override if you do not want the standard buttons'''
box = tkinter.Frame(self)
w = tkinter.Button(box, text="OK", width = 10,
command = self.ok, default = ACTIVE)
w.pack()
self.bind("<Return>", self.ok)
box.pack()
def apply(self):
"""Quits the dialog."""
self._modified = True
def modified(self):
return self._modified
def getText(self):
"""Returns the text currently in the text field."""
return self._field.getText()
class EasyDialog(tkinter.simpledialog.Dialog):
"""Represents a general-purpose dialog. Subclasses should include
body and apply methods."""
def __init__(self, parent, title = ""):
"""Set up the window and widgets."""
self._modified = False
tkinter.simpledialog.Dialog.__init__(self, parent, title)
def modified(self):
"""Returns the modified status of the dialog."""
return self._modified
def setModified(self):
self._modified = True
def addLabel(self, master, text, row, column,
columnspan = 1, rowspan = 1,
sticky = N+W, font = None):
"""Creates and inserts a label at the row and column,
and returns the label."""
label = tkinter.Label(master, text = text, font = font)
master.rowconfigure(row, weight = 1)
master.columnconfigure(column, weight = 1)
label.grid(row = row, column = column,
columnspan = columnspan, rowspan = rowspan,
padx = 5, pady = 5, sticky = sticky)
return label
def addButton(self, master, text, row, column,
columnspan = 1, rowspan = 1,
command = lambda: None,
state = NORMAL):
"""Creates and inserts a button at the row and column,
and returns the button."""
button = tkinter.Button(master, text = text,
command = command, state = state)
master.rowconfigure(row, weight = 1)
master.columnconfigure(column, weight = 1)
button.grid(row = row, column = column,
columnspan = columnspan, rowspan = rowspan,
padx = 5, pady = 5)
return button
def addFloatField(self, master, value, row, column,
columnspan = 1, rowspan = 1,
width = 20, precision = None,
sticky = N+E, state = NORMAL):
"""Creates and inserts a float field at the row and column,
and returns the float field."""
field = FloatField(master, value, width, precision, state)
master.rowconfigure(row, weight = 1)
master.columnconfigure(column, weight = 1)
field.grid(row = row, column = column,
columnspan = columnspan, rowspan = rowspan,
padx = 5, pady = 5, sticky = sticky)
return field
def addIntegerField(self, master, value, row, column,
columnspan = 1, rowspan = 1,
width = 10, sticky = N+E, state = NORMAL):
"""Creates and inserts an integer field at the row and column,
and returns the integer field."""
field = IntegerField(master, value, width, state)
master.rowconfigure(row, weight = 1)
master.columnconfigure(column, weight = 1)
field.grid(row = row, column = column,
columnspan = columnspan, rowspan = rowspan,
padx = 5, pady = 5, sticky = sticky)
return field
def addTextField(self, master, text, row, column,
columnspan = 1, rowspan = 1,
width = 20, sticky = N+E, state = NORMAL):
"""Creates and inserts a text field at the row and column,
and returns the text field."""
field = TextField(master, text, width, state)
master.rowconfigure(row, weight = 1)
master.columnconfigure(column, weight = 1)
field.grid(row = row, column = column,
columnspan = columnspan, rowspan = rowspan,
padx = 5, pady = 5, sticky = sticky)
return field
def addCheckbutton(self, master, text, row, column,
rowspan = 1, columnspan = 1,
sticky = N+S+E+W, command = lambda : 0):
"""Creates and inserts check button at the row and column,
and returns the check button."""
cb = EasyCheckbutton(master, text, command)
master.rowconfigure(row, weight = 1)
master.columnconfigure(column, weight = 1)
cb.grid(row = row, column = column,
columnspan = columnspan, rowspan = rowspan,
padx = 5, pady = 5, sticky = sticky)
return cb
def addRadiobuttonGroup(self, master, row, column,
rowspan = 1, columnspan = 1, orient = VERTICAL):
"""Creates and returns a radio button group."""
return EasyRadiobuttonGroup(master, row, column, rowspan, columnspan, orient)
def addScale(self, master, row, column, rowspan = 1, columnspan = 1,
command = lambda value: value, from_ = 0, to = 0,
label = "", length = 100, orient = HORIZONTAL,
resolution = 1, tickinterval = 0):
"""Creates and inserts a scale at the row and column,
and returns the scale."""
scale = tkinter.Scale(master, command = command, from_ = from_, to = to,
label = label, length = length,
orient = orient, resolution = resolution,
tickinterval = tickinterval, relief = "sunken",
borderwidth = 4)
master.rowconfigure(row, weight = 1)
master.columnconfigure(column, weight = 1)
scale.grid(row = row, column = column, columnspan = columnspan,
rowspan = rowspan, sticky = N+S+E+W)
return scale
def addTextArea(self, master, text, row, column, rowspan = 1, columnspan = 1,
width = 80, height = 5, wrap = NONE):
"""Creates and inserts a multiline text area at the row and column,
and returns the text area. Vertical and horizontal scrollbars are
provided."""
frame = tkinter.Frame(master)
frame.grid(row = row, column = column,
columnspan = columnspan, rowspan = rowspan,
sticky = N+S+E+W)
master.columnconfigure(column, weight = 1)
master.rowconfigure(row, weight = 1)
xScroll = tkinter.Scrollbar(frame, orient = HORIZONTAL)
xScroll.grid(row = 1, column = 0, sticky = E+W)
yScroll = tkinter.Scrollbar(frame, orient = VERTICAL)
yScroll.grid(row = 0, column = 1, sticky = N+S)
area = TextArea(frame, text, width, height,
xScroll.set, yScroll.set, wrap)
area.grid(row = 0, column = 0,
padx = 5, pady = 5, sticky = N+S+E+W)
frame.columnconfigure(0, weight = 1)
frame.rowconfigure(0, weight = 1)
xScroll["command"] = area.xview
yScroll["command"] = area.yview
return area
def addListbox(self, master, row, column, rowspan = 1, columnspan = 1,
width = 10, height = 5, listItemSelected = lambda index: index):
"""Creates and inserts a scrolling list box at the row and column, with a
width and height in lines and columns of text, and a default item selection
method, and returns the list box."""
frame = tkinter.Frame(master)
frame.grid(row = row, column = column, columnspan = columnspan, rowspan = rowspan,
sticky = N+S+E+W)
master.columnconfigure(column, weight = 1)
master.rowconfigure(row, weight = 1)
yScroll = tkinter.Scrollbar(frame, orient = VERTICAL)
yScroll.grid(row = 0, column = 1, sticky = N+S)
listBox = EasyListbox(frame, width, height, yScroll.set, listItemSelected)
listBox.grid(row = 0, column = 0, sticky = N+S+E+W)
frame.columnconfigure(0, weight = 1)
frame.rowconfigure(0, weight = 1)
yScroll["command"] = listBox.yview
return listBox
def addCanvas(self, master, canvas = None, row = 0, column = 0,
rowspan = 1, columnspan = 1, width = 200, height = 100,
background = "white"):
"""Creates and inserts a canvas at the row and column,
and returns the canvas."""
if not canvas:
canvas = EasyCanvas(master, width = width, height = height,
background = background)
canvas.grid(row = row, column = column,
rowspan = rowspan, columnspan = columnspan,
sticky = W+E+N+S)
master.columnconfigure(column, weight = 10)
master.rowconfigure(row, weight = 10)
return canvas
def addMenuBar(self, master, row, column, rowspan = 1, columnspan = 1,
orient = "horizontal"):
"""Creates and inserts a menu bar at the row and column,
and returns the menu bar."""
if not orient in ("horizontal", "vertical"):
raise ValueError("orient must be horizontal or vertical")
menuBar = EasyMenuBar(master, orient)
menuBar.grid(row = row, column = column,
rowspan = rowspan, columnspan = columnspan,
sticky = N+W)
return menuBar
def messageBox(self, title = "", message = "", width = 25, height = 5):
"""Creates and pops up a message box, with the given title,
message, and width and height in rows and columns of text."""
dlg = MessageBox(self, title, message, width, height)
return dlg.modified()
# Added 12-18-2012
def addPanel(self, master, row, column,
rowspan = 1, columnspan = 1, background = "white"):
"""Creates and returns a panel."""
return EasyPanel(master, row, column, rowspan, columnspan, background)
# Added 12-18-2012
class EasyPanel(tkinter.Frame):
"""Organizes a group of widgets in a panel (nested frame)."""
def __init__(self, parent, row, column, rowspan, columnspan, background):
tkinter.Frame.__init__(self, parent)
parent.rowconfigure(row, weight = 1)
parent.columnconfigure(column, weight = 1)
self.grid(row = row, column = column,
rowspan = rowspan, columnspan = columnspan,
sticky = N+S+E+W)
self.setBackground(background)
def setBackground(self, color):
"""Resets the panel's background color to color."""
self["background"] = color
def addButton(self, text, row, column,
columnspan = 1, rowspan = 1,
command = lambda: None,
state = NORMAL):
"""Creates and inserts a button at the row and column,
and returns the button."""
button = tkinter.Button(self, text = text,
command = command, state = state)
self.rowconfigure(row, weight = 1)
self.columnconfigure(column, weight = 1)
button.grid(row = row, column = column,
columnspan = columnspan, rowspan = rowspan,
padx = 5, pady = 5)
return button
def addLabel(self, text, row, column,
columnspan = 1, rowspan = 1,
sticky = N+W, font = None,
background = "white", foreground = "black"):
"""Creates and inserts a label at the row and column,
and returns the label."""
label = tkinter.Label(self, text = text, font = font,
background = background,
foreground = foreground)
self.rowconfigure(row, weight = 1)
self.columnconfigure(column, weight = 1)
label.grid(row = row, column = column,
columnspan = columnspan, rowspan = rowspan,
padx = 5, pady = 5, sticky = sticky)
return label
def addFloatField(self, value, row, column,
columnspan = 1, rowspan = 1,
width = 20, precision = None,
sticky = N+E, state = NORMAL):
"""Creates and inserts a float field at the row and column,
and returns the float field."""
field = FloatField(self, value, width, precision, state)
self.rowconfigure(row, weight = 1)
self.columnconfigure(column, weight = 1)
field.grid(row = row, column = column,
columnspan = columnspan, rowspan = rowspan,
padx = 5, pady = 5, sticky = sticky)
return field
def addIntegerField(self, value, row, column,
columnspan = 1, rowspan = 1,
width = 10, sticky = N+E, state = NORMAL):
"""Creates and inserts an integer field at the row and column,
and returns the integer field."""
field = IntegerField(self, value, width, state)
self.rowconfigure(row, weight = 1)
self.columnconfigure(column, weight = 1)