-
-
Notifications
You must be signed in to change notification settings - Fork 34.7k
gh-77578: IDLE Help - let users control font size #6665
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
066bcb7
42a1a74
d25899d
2fd7820
ff08ebf
a23cb1c
e88f488
90cb041
e03e4b2
645a0e0
5c1d58a
98295cb
d6f80e8
dab3eb5
26b3342
100bdea
803800d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,17 +28,19 @@ | |
| from os.path import abspath, dirname, isfile, join | ||
| from platform import python_version | ||
|
|
||
| from tkinter import Toplevel, Text, Menu | ||
| from tkinter import Toplevel, Text, Menu, EventType | ||
| from tkinter.ttk import Frame, Menubutton, Scrollbar, Style | ||
| from tkinter import font as tkfont | ||
|
|
||
| from idlelib.config import idleConf | ||
| from idlelib.textview import FontSizer | ||
|
|
||
| ## About IDLE ## | ||
|
|
||
|
|
||
| ## IDLE Help ## | ||
|
|
||
|
|
||
| class HelpParser(HTMLParser): | ||
| """Render help.html into a text widget. | ||
|
|
||
|
|
@@ -67,7 +69,7 @@ def __init__(self, text): | |
| def indent(self, amt=1): | ||
| "Change indent (+1, 0, -1) and tags." | ||
| self.level += amt | ||
| self.tags = '' if self.level == 0 else 'l'+str(self.level) | ||
| self.tags = '' if self.level == 0 else f'l{self.level}' | ||
|
|
||
| def handle_starttag(self, tag, attrs): | ||
| "Handle starttags in help.html." | ||
|
|
@@ -175,26 +177,37 @@ def __init__(self, parent, filename): | |
| Text.__init__(self, parent, wrap='word', highlightthickness=0, | ||
| padx=5, borderwidth=0, width=uwide, height=uhigh) | ||
|
|
||
| normalfont = self.findfont(['TkDefaultFont', 'arial', 'helvetica']) | ||
| fixedfont = self.findfont(['TkFixedFont', 'monaco', 'courier']) | ||
| self['font'] = (normalfont, 12) | ||
| self.tag_configure('em', font=(normalfont, 12, 'italic')) | ||
| self.tag_configure('h1', font=(normalfont, 20, 'bold')) | ||
| self.tag_configure('h2', font=(normalfont, 18, 'bold')) | ||
| self.tag_configure('h3', font=(normalfont, 15, 'bold')) | ||
| self.tag_configure('pre', font=(fixedfont, 12), background='#f6f6ff') | ||
| self.tag_configure('preblock', font=(fixedfont, 10), lmargin1=25, | ||
| borderwidth=1, relief='solid', background='#eeffcc') | ||
| self.tag_configure('l1', lmargin1=25, lmargin2=25) | ||
| self.tag_configure('l2', lmargin1=50, lmargin2=50) | ||
| self.tag_configure('l3', lmargin1=75, lmargin2=75) | ||
| self.tag_configure('l4', lmargin1=100, lmargin2=100) | ||
| self.create_fonts() | ||
| self.configure_tags() | ||
|
|
||
| self.parser = HelpParser(self) | ||
| with open(filename, encoding='utf-8') as f: | ||
| contents = f.read() | ||
| self.parser.feed(contents) | ||
|
|
||
| self['state'] = 'disabled' | ||
| self.focus_set() | ||
|
|
||
| def create_fonts(self): | ||
| "Create fonts to be used with tags." | ||
| base_size = idleConf.GetOption('main', 'EditorWindow', | ||
| 'font-size', type='int') | ||
| normalfont = self.findfont(['TkDefaultFont', 'arial', 'helvetica']) | ||
| fixedfont = self.findfont(['TkFixedFont', 'monaco', 'courier']) | ||
|
|
||
| self.base_font = tkfont.Font(self, (normalfont, base_size)) | ||
| self['font'] = self.base_font | ||
|
|
||
| # Define styling for each font tag used in html. | ||
| self.fonts = fonts = {} | ||
| fonts['em'] = tkfont.Font(self, family=normalfont, slant='italic') | ||
| for tag in ('h3', 'h2', 'h1'): | ||
| fonts[tag] = tkfont.Font(self, family=normalfont, weight='bold') | ||
| for tag in ('pre', 'preblock'): | ||
| fonts[tag] = tkfont.Font(self, family=fixedfont) | ||
| self.scale_tagfonts(base_size) | ||
|
|
||
| FontSizer(self) | ||
|
|
||
| def findfont(self, names): | ||
| "Return name of first font family derived from names." | ||
|
|
@@ -206,6 +219,24 @@ def findfont(self, names): | |
| for x in tkfont.families(root=self)): | ||
| return name | ||
|
|
||
| def configure_tags(self): | ||
| "Configure tags used in parsing." | ||
| for tag in ('em', 'h1', 'h2', 'h3', 'pre', 'preblock'): | ||
| self.tag_configure(tag, font=self.fonts[tag]) | ||
| self.tag_configure('pre', background='#f6f6ff') | ||
| self.tag_configure('preblock', lmargin1=25, borderwidth=1, | ||
| relief='solid', background='#eeffcc') | ||
| for level in range(1, 5): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is one more level than before. I don't know if we use more than l1. I may add some prints to the parser to fine out. We do use everything else.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure what you mean by 'one more level' than before? Before, it did levels l1, l2, l3, and l4. Debugging shows that down to l3 is used. |
||
| self.tag_configure(f'l{level}', lmargin1=25*level, lmargin2=25*level) | ||
|
|
||
| def scale_tagfonts(self, base): | ||
| "Scale tag sizes based on the size of normal text." | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No default. Set self.base_size to base passed in.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With the refactoring of the class for FontSizer, I removed self.base_size. |
||
| # Scale percentages are from Sphinx classic.css. | ||
| scale = {'h3': 1.2, 'h2': 1.4, 'h1': 1.6, | ||
| 'em': 1.0, 'pre': 1.0, 'preblock': 0.9} | ||
| for tag in self.fonts: | ||
| self.fonts[tag]['size'] = int(base * scale[tag]) | ||
|
|
||
|
|
||
| class HelpFrame(Frame): | ||
| "Display html text, scrollbar, and toc." | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Use user preferences to set font size in IDLE Textview and Help and allow dynamic font | ||
| sizing using keybindings or mouse wheel. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really like the doubling of the configuration code, but I have not thought of anything better yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't make any changes for this.