1
1
"""
2
2
Dialog for building Tkinter accelerator key bindings
3
3
"""
4
- from tkinter import *
5
- from tkinter .ttk import Scrollbar
4
+ from tkinter import Toplevel , Listbox , Text , StringVar , TclError
5
+ from tkinter .ttk import Button , Checkbutton , Entry , Frame , Label , Scrollbar
6
6
from tkinter import messagebox
7
7
import string
8
8
import sys
@@ -67,11 +67,11 @@ def showerror(self, *args, **kwargs):
67
67
messagebox .showerror (* args , ** kwargs )
68
68
69
69
def create_widgets (self ):
70
- self .frame = frame = Frame (self , borderwidth = 2 , relief = SUNKEN )
71
- frame .pack (side = TOP , expand = True , fill = BOTH )
70
+ self .frame = frame = Frame (self , borderwidth = 2 , relief = 'sunken' )
71
+ frame .pack (side = 'top' , expand = True , fill = 'both' )
72
72
73
73
frame_buttons = Frame (self )
74
- frame_buttons .pack (side = BOTTOM , fill = X )
74
+ frame_buttons .pack (side = 'bottom' , fill = 'x' )
75
75
76
76
self .button_ok = Button (frame_buttons , text = 'OK' ,
77
77
width = 8 , command = self .ok )
@@ -82,20 +82,20 @@ def create_widgets(self):
82
82
83
83
# Basic entry key sequence.
84
84
self .frame_keyseq_basic = Frame (frame , name = 'keyseq_basic' )
85
- self .frame_keyseq_basic .grid (row = 0 , column = 0 , sticky = NSEW ,
85
+ self .frame_keyseq_basic .grid (row = 0 , column = 0 , sticky = 'nsew' ,
86
86
padx = 5 , pady = 5 )
87
87
basic_title = Label (self .frame_keyseq_basic ,
88
88
text = f"New keys for '{ self .action } ' :" )
89
- basic_title .pack (anchor = W )
89
+ basic_title .pack (anchor = 'w' )
90
90
91
- basic_keys = Label (self .frame_keyseq_basic , justify = LEFT ,
92
- textvariable = self .key_string , relief = GROOVE ,
91
+ basic_keys = Label (self .frame_keyseq_basic , justify = 'left' ,
92
+ textvariable = self .key_string , relief = 'groove' ,
93
93
borderwidth = 2 )
94
- basic_keys .pack (ipadx = 5 , ipady = 5 , fill = X )
94
+ basic_keys .pack (ipadx = 5 , ipady = 5 , fill = 'x' )
95
95
96
96
# Basic entry controls.
97
97
self .frame_controls_basic = Frame (frame )
98
- self .frame_controls_basic .grid (row = 1 , column = 0 , sticky = NSEW , padx = 5 )
98
+ self .frame_controls_basic .grid (row = 1 , column = 0 , sticky = 'nsew' , padx = 5 )
99
99
100
100
# Basic entry modifiers.
101
101
self .modifier_checkbuttons = {}
@@ -105,51 +105,51 @@ def create_widgets(self):
105
105
check = Checkbutton (self .frame_controls_basic ,
106
106
command = self .build_key_string , text = label ,
107
107
variable = variable , onvalue = modifier , offvalue = '' )
108
- check .grid (row = 0 , column = column , padx = 2 , sticky = W )
108
+ check .grid (row = 0 , column = column , padx = 2 , sticky = 'w' )
109
109
self .modifier_checkbuttons [modifier ] = check
110
110
column += 1
111
111
112
112
# Basic entry help text.
113
- help_basic = Label (self .frame_controls_basic , justify = LEFT ,
113
+ help_basic = Label (self .frame_controls_basic , justify = 'left' ,
114
114
text = "Select the desired modifier keys\n " +
115
115
"above, and the final key from the\n " +
116
116
"list on the right.\n \n " +
117
117
"Use upper case Symbols when using\n " +
118
118
"the Shift modifier. (Letters will be\n " +
119
119
"converted automatically.)" )
120
- help_basic .grid (row = 1 , column = 0 , columnspan = 4 , padx = 2 , sticky = W )
120
+ help_basic .grid (row = 1 , column = 0 , columnspan = 4 , padx = 2 , sticky = 'w' )
121
121
122
122
# Basic entry key list.
123
123
self .list_keys_final = Listbox (self .frame_controls_basic , width = 15 ,
124
- height = 10 , selectmode = SINGLE )
124
+ height = 10 , selectmode = 'single' )
125
125
self .list_keys_final .bind ('<ButtonRelease-1>' , self .final_key_selected )
126
- self .list_keys_final .grid (row = 0 , column = 4 , rowspan = 4 , sticky = NS )
126
+ self .list_keys_final .grid (row = 0 , column = 4 , rowspan = 4 , sticky = 'ns' )
127
127
scroll_keys_final = Scrollbar (self .frame_controls_basic ,
128
- orient = VERTICAL ,
128
+ orient = 'vertical' ,
129
129
command = self .list_keys_final .yview )
130
130
self .list_keys_final .config (yscrollcommand = scroll_keys_final .set )
131
- scroll_keys_final .grid (row = 0 , column = 5 , rowspan = 4 , sticky = NS )
131
+ scroll_keys_final .grid (row = 0 , column = 5 , rowspan = 4 , sticky = 'ns' )
132
132
self .button_clear = Button (self .frame_controls_basic ,
133
133
text = 'Clear Keys' ,
134
134
command = self .clear_key_seq )
135
135
self .button_clear .grid (row = 2 , column = 0 , columnspan = 4 )
136
136
137
137
# Advanced entry key sequence.
138
138
self .frame_keyseq_advanced = Frame (frame , name = 'keyseq_advanced' )
139
- self .frame_keyseq_advanced .grid (row = 0 , column = 0 , sticky = NSEW ,
139
+ self .frame_keyseq_advanced .grid (row = 0 , column = 0 , sticky = 'nsew' ,
140
140
padx = 5 , pady = 5 )
141
- advanced_title = Label (self .frame_keyseq_advanced , justify = LEFT ,
141
+ advanced_title = Label (self .frame_keyseq_advanced , justify = 'left' ,
142
142
text = f"Enter new binding(s) for '{ self .action } ' :\n " +
143
143
"(These bindings will not be checked for validity!)" )
144
- advanced_title .pack (anchor = W )
144
+ advanced_title .pack (anchor = 'w' )
145
145
self .advanced_keys = Entry (self .frame_keyseq_advanced ,
146
146
textvariable = self .key_string )
147
- self .advanced_keys .pack (fill = X )
147
+ self .advanced_keys .pack (fill = 'x' )
148
148
149
149
# Advanced entry help text.
150
150
self .frame_help_advanced = Frame (frame )
151
- self .frame_help_advanced .grid (row = 1 , column = 0 , sticky = NSEW , padx = 5 )
152
- help_advanced = Label (self .frame_help_advanced , justify = LEFT ,
151
+ self .frame_help_advanced .grid (row = 1 , column = 0 , sticky = 'nsew' , padx = 5 )
152
+ help_advanced = Label (self .frame_help_advanced , justify = 'left' ,
153
153
text = "Key bindings are specified using Tkinter keysyms as\n " +
154
154
"in these samples: <Control-f>, <Shift-F2>, <F12>,\n "
155
155
"<Control-space>, <Meta-less>, <Control-Alt-Shift-X>.\n "
@@ -159,12 +159,12 @@ def create_widgets(self):
159
159
"is the 'do-nothing' keybinding.\n \n " +
160
160
"Multiple separate bindings for one action should be\n " +
161
161
"separated by a space, eg., <Alt-v> <Meta-v>." )
162
- help_advanced .grid (row = 0 , column = 0 , sticky = NSEW )
162
+ help_advanced .grid (row = 0 , column = 0 , sticky = 'nsew' )
163
163
164
164
# Switch between basic and advanced.
165
165
self .button_level = Button (frame , command = self .toggle_level ,
166
166
text = '<< Basic Key Binding Entry' )
167
- self .button_level .grid (row = 2 , column = 0 , stick = EW , padx = 5 , pady = 5 )
167
+ self .button_level .grid (row = 2 , column = 0 , stick = 'ew' , padx = 5 , pady = 5 )
168
168
self .toggle_level ()
169
169
170
170
def set_modifiers_for_platform (self ):
@@ -204,7 +204,7 @@ def final_key_selected(self, event=None):
204
204
def build_key_string (self ):
205
205
"Create formatted string of modifiers plus the key."
206
206
keylist = modifiers = self .get_modifiers ()
207
- final_key = self .list_keys_final .get (ANCHOR )
207
+ final_key = self .list_keys_final .get ('anchor' )
208
208
if final_key :
209
209
final_key = self .translate_key (final_key , modifiers )
210
210
keylist .append (final_key )
@@ -217,8 +217,8 @@ def get_modifiers(self):
217
217
218
218
def clear_key_seq (self ):
219
219
"Clear modifiers and keys selection."
220
- self .list_keys_final .select_clear (0 , END )
221
- self .list_keys_final .yview (MOVETO , '0.0' )
220
+ self .list_keys_final .select_clear (0 , 'end' )
221
+ self .list_keys_final .yview ('moveto' , '0.0' )
222
222
for variable in self .modifier_vars :
223
223
variable .set ('' )
224
224
self .key_string .set ('' )
@@ -237,7 +237,7 @@ def load_final_key_list(self):
237
237
# Make a tuple of most of the useful common 'final' keys.
238
238
keys = (self .alphanum_keys + self .punctuation_keys + self .function_keys +
239
239
self .whitespace_keys + self .edit_keys + self .move_keys )
240
- self .list_keys_final .insert (END , * keys )
240
+ self .list_keys_final .insert ('end' , * keys )
241
241
242
242
@staticmethod
243
243
def translate_key (key , modifiers ):
@@ -282,7 +282,7 @@ def keys_ok(self, keys):
282
282
Doesn't check the string produced by the advanced dialog because
283
283
'modifiers' isn't set.
284
284
"""
285
- final_key = self .list_keys_final .get (ANCHOR )
285
+ final_key = self .list_keys_final .get ('anchor' )
286
286
modifiers = self .get_modifiers ()
287
287
title = self .keyerror_title
288
288
key_sequences = [key for keylist in self .current_key_sequences
0 commit comments