bpo-35598: IDLE: Switch config_key dialog to ttk widgets (GH-11365) · python/cpython@4bd79c3
11"""
22Dialog for building Tkinter accelerator key bindings
33"""
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
66from tkinter import messagebox
77import string
88import sys
@@ -67,11 +67,11 @@ def showerror(self, *args, **kwargs):
6767messagebox.showerror(*args, **kwargs)
68686969def 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')
72727373frame_buttons = Frame(self)
74-frame_buttons.pack(side=BOTTOM, fill=X)
74+frame_buttons.pack(side='bottom', fill='x')
75757676self.button_ok = Button(frame_buttons, text='OK',
7777width=8, command=self.ok)
@@ -82,20 +82,20 @@ def create_widgets(self):
82828383# Basic entry key sequence.
8484self.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',
8686padx=5, pady=5)
8787basic_title = Label(self.frame_keyseq_basic,
8888text=f"New keys for '{self.action}' :")
89-basic_title.pack(anchor=W)
89+basic_title.pack(anchor='w')
909091-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',
9393borderwidth=2)
94-basic_keys.pack(ipadx=5, ipady=5, fill=X)
94+basic_keys.pack(ipadx=5, ipady=5, fill='x')
95959696# Basic entry controls.
9797self.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)
9999100100# Basic entry modifiers.
101101self.modifier_checkbuttons = {}
@@ -105,51 +105,51 @@ def create_widgets(self):
105105check = Checkbutton(self.frame_controls_basic,
106106command=self.build_key_string, text=label,
107107variable=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')
109109self.modifier_checkbuttons[modifier] = check
110110column += 1
111111112112# Basic entry help text.
113-help_basic = Label(self.frame_controls_basic, justify=LEFT,
113+help_basic = Label(self.frame_controls_basic, justify='left',
114114text="Select the desired modifier keys\n"+
115115"above, and the final key from the\n"+
116116"list on the right.\n\n" +
117117"Use upper case Symbols when using\n" +
118118"the Shift modifier. (Letters will be\n" +
119119"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')
121121122122# Basic entry key list.
123123self.list_keys_final = Listbox(self.frame_controls_basic, width=15,
124-height=10, selectmode=SINGLE)
124+height=10, selectmode='single')
125125self.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')
127127scroll_keys_final = Scrollbar(self.frame_controls_basic,
128-orient=VERTICAL,
128+orient='vertical',
129129command=self.list_keys_final.yview)
130130self.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')
132132self.button_clear = Button(self.frame_controls_basic,
133133text='Clear Keys',
134134command=self.clear_key_seq)
135135self.button_clear.grid(row=2, column=0, columnspan=4)
136136137137# Advanced entry key sequence.
138138self.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',
140140padx=5, pady=5)
141-advanced_title = Label(self.frame_keyseq_advanced, justify=LEFT,
141+advanced_title = Label(self.frame_keyseq_advanced, justify='left',
142142text=f"Enter new binding(s) for '{self.action}' :\n" +
143143"(These bindings will not be checked for validity!)")
144-advanced_title.pack(anchor=W)
144+advanced_title.pack(anchor='w')
145145self.advanced_keys = Entry(self.frame_keyseq_advanced,
146146textvariable=self.key_string)
147-self.advanced_keys.pack(fill=X)
147+self.advanced_keys.pack(fill='x')
148148149149# Advanced entry help text.
150150self.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',
153153text="Key bindings are specified using Tkinter keysyms as\n"+
154154"in these samples: <Control-f>, <Shift-F2>, <F12>,\n"
155155"<Control-space>, <Meta-less>, <Control-Alt-Shift-X>.\n"
@@ -159,12 +159,12 @@ def create_widgets(self):
159159"is the 'do-nothing' keybinding.\n\n" +
160160"Multiple separate bindings for one action should be\n"+
161161"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')
163163164164# Switch between basic and advanced.
165165self.button_level = Button(frame, command=self.toggle_level,
166166text='<< 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)
168168self.toggle_level()
169169170170def set_modifiers_for_platform(self):
@@ -204,7 +204,7 @@ def final_key_selected(self, event=None):
204204def build_key_string(self):
205205"Create formatted string of modifiers plus the key."
206206keylist = modifiers = self.get_modifiers()
207-final_key = self.list_keys_final.get(ANCHOR)
207+final_key = self.list_keys_final.get('anchor')
208208if final_key:
209209final_key = self.translate_key(final_key, modifiers)
210210keylist.append(final_key)
@@ -217,8 +217,8 @@ def get_modifiers(self):
217217218218def clear_key_seq(self):
219219"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')
222222for variable in self.modifier_vars:
223223variable.set('')
224224self.key_string.set('')
@@ -237,7 +237,7 @@ def load_final_key_list(self):
237237# Make a tuple of most of the useful common 'final' keys.
238238keys = (self.alphanum_keys + self.punctuation_keys + self.function_keys +
239239self.whitespace_keys + self.edit_keys + self.move_keys)
240-self.list_keys_final.insert(END, *keys)
240+self.list_keys_final.insert('end', *keys)
241241242242@staticmethod
243243def translate_key(key, modifiers):
@@ -282,7 +282,7 @@ def keys_ok(self, keys):
282282 Doesn't check the string produced by the advanced dialog because
283283 'modifiers' isn't set.
284284 """
285-final_key = self.list_keys_final.get(ANCHOR)
285+final_key = self.list_keys_final.get('anchor')
286286modifiers = self.get_modifiers()
287287title = self.keyerror_title
288288key_sequences = [key for keylist in self.current_key_sequences