This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Python IDLE fails to start (tk font issue?)
Type: behavior Stage:
Components: IDLE Versions: Python 3.11, Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: terry.reedy Nosy List: cfeck, epaine, mcepl, serhiy.storchaka, taleinat, terry.reedy
Priority: normal Keywords:

Created on 2021-12-21 23:39 by mcepl, last changed 2022-04-11 14:59 by admin.

Files
File name Uploaded Description Edit
tem.py terry.reedy, 2021-12-22 00:32 Test tk character widths
Messages (9)
msg409003 - (view) Author: Matej Cepl (mcepl) * Date: 2021-12-21 23:39
(originally filed as https://bugzilla.suse.com/1193409)

When trying to start "idle3.8", I get a ZeroDivisionError in
editor.py. Trying to patch it, I noticed that the complete UI of
IDLE shows no text. Since the same also happens with "idle3.9",
I guess it is a bug in tk.

The error happens also with a fresh user account.

The complete error message is as follows:

> idle3.8
Traceback (most recent call last):
  File "/usr/bin/idle3.8", line 5, in <module>
    main()
  File "/usr/lib64/python3.8/idlelib/pyshell.py", line 1522, in main
    shell = flist.open_shell()
  File "/usr/lib64/python3.8/idlelib/pyshell.py", line 329, in open_shell
    self.pyshell = PyShell(self)
  File "/usr/lib64/python3.8/idlelib/pyshell.py", line 874, in __init__
    OutputWindow.__init__(self, flist, None, None)
  File "/usr/lib64/python3.8/idlelib/outwin.py", line 79, in __init__
    EditorWindow.__init__(self, *args)
  File "/usr/lib64/python3.8/idlelib/editor.py", line 218, in __init__
    self.set_width()
  File "/usr/lib64/python3.8/idlelib/editor.py", line 366, in set_width
    self.width = pixel_width // zero_char_width
ZeroDivisionError: integer division or modulo by zero
>
msg409004 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-12-22 00:32
(This is behavior, not crash issue, as the latter is when there is no traceback.)

The editor code was added 3 years ago in #37929, PR-15452, to correct using the configured char width after font and window size changes make that invalid.  It is based on 
https://www.tcl.tk/man/tcl8.6/TkCmd/text.html#M21
"If the font does not have a uniform width then the width of the character “0” [zero] is used in translating from character units to screen units."  The implies that said char width is non-zero.  It would seem like a font bug if not.  This is first report I know of where width is 0.

The current code implements this as
       zero_char_width = \
            Font(text, font=text.cget('font')).measure('0')

Matej, what OS and font gives this error?  Please run the following, also uploaded, with the offending font, replacing the name I used.

import tkinter as tk
from tkinter.font import Font
r = tk.Tk()
t = tk.Text(r, font=('Source Code Pro', 10, 'normal'))
t.pack()
s = '0oO !*}'
t.insert('1.0', s) # Only to check that all above is valid.
for c in s:
    print(Font(t, font=t['font']).measure(c))

For fixed 10 pitch Source Code Pro, all widths are 8.
---

We could, of course, catch ZeroDivisionError, but when then?  This is why I want to know what OS, font, and a test with multiple characters.

On possibility is to check whenever font is set, but still, what then?
msg409007 - (view) Author: Matej Cepl (mcepl) * Date: 2021-12-22 01:03
> Matej, what OS and font gives this error?  Please run the
> following, also uploaded, with the offending font, replacing the
> name I used.

I am a packager at SUSE maintaining Python packages both for
openSUSE and SLE (our commercial offering), even though
unfortunately cannot reproduce the issue myself. GUI programming is
just part of the Python libraries where I am really lost, so I filed
this bug (which was originally filed by somebody else at our
Bugzilla) here asking for help.

I will ask our reporter for testing your suggested reproducer.
msg409008 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-12-22 01:32
On a system where IDLE will not start, the code has to be run directly with 'python3', however that is done there.  One will need to add the line 'r.mainloop()' at the end of the script to see the tk window.  However, this is not needed to see the printed width results.

What tcl/tk version do you distribute?  Is the person seeing the bug using that?  Add "print(tk.call('info', 'patchlevel'))" to see.

I presume that when IDLE worked for you, you used the default font.  Specific tests to run:
python3 -m test -ugui test_tk test_tcl test_ttk_guionly test_ttk_textonly test_idle
msg409018 - (view) Author: Matej Cepl (mcepl) * Date: 2021-12-22 10:42
$ rpm -qR python38-idle
/usr/bin/python3
/usr/bin/python3.8
config(python38-idle) = 3.8.12-3.1
python(abi) = 3.8
python38 = 3.8.12
python38-tk
rpmlib(CompressedFileNames) <= 3.0.4-1
rpmlib(FileDigests) <= 4.6.0-1
rpmlib(PartialHardlinkSets) <= 4.0.4-1
rpmlib(PayloadFilesHavePrefix) <= 4.0-1
rpmlib(PayloadIsZstd) <= 5.4.18-1
$ rpm -qR python38-tk
libc.so.6()(64bit)
libc.so.6(GLIBC_2.2.5)(64bit)
libc.so.6(GLIBC_2.4)(64bit)
libtcl8.6.so()(64bit)
libtk8.6.so()(64bit)
python(abi) = 3.8
python38 = 3.8.12
rpmlib(CompressedFileNames) <= 3.0.4-1
rpmlib(FileDigests) <= 4.6.0-1
rpmlib(PartialHardlinkSets) <= 4.0.4-1
rpmlib(PayloadFilesHavePrefix) <= 4.0-1
rpmlib(PayloadIsZstd) <= 5.4.18-1
$ rpm -q tk
tk-8.6.12-1.1.x86_64
$
msg409020 - (view) Author: Matej Cepl (mcepl) * Date: 2021-12-22 10:44
And I am not sure what’s going on, but when I run the test suite you
indicated, it kills my Wayland session immediately. How is Tk doing
with X11->Wayland switch?
msg409022 - (view) Author: Matej Cepl (mcepl) * Date: 2021-12-22 10:45
And yes, idle (both with 3.10 and 3.8) works for me just fine all the time (it doesn't for my reporter, but that's another point).
msg409053 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-12-22 22:21
Serhiy: Matej Cepl (SUSE) asks about tk and Wayland switch.  Do you know anything?
msg409054 - (view) Author: Christoph Feck (cfeck) Date: 2021-12-22 22:28
(original bug reporter replying, thanks Matej for pointing me here!)

> what OS and font gives this error?

OS is openSUSE Tumbleweed 20211202, which has these versions:
- python 3.8.12
- tcl/tk 8.6.12
- fontconfig 2.13.1
- adobe-sourcecodepro-fonts 2.030

Previously installed (working) version was 20210114, which had:
- python 3.8.6
- tcl/tk 8.6.10
- fontconfig 2.13.1
- adobe-sourcecodepro-fonts 2.030

I am not sure if tk uses fontconfig or what other libraries tk uses to load fonts. I can give versions for all packages.

> Please run the following, also uploaded, with the offending font, replacing the name I used.

Result: 7 lines with "0" (zero)

(regardless of which font name I use, either installed, such as 'Cousine', 'Source Code Pro', or not installed, such as 'XYZ')

> print(tk.call('info', 'patchlevel'))

Result: "8.6.12"

> add the line 'r.mainloop()' at the end of the script

Result: A tiny window without text

I also tested Python version 3.9.9 on the updated system, which had the same issue.
History
Date User Action Args
2022-04-11 14:59:53adminsetgithub: 90304
2021-12-23 02:39:33terry.reedysetnosy: + serhiy.storchaka, epaine
2021-12-22 22:28:28cfecksetnosy: + cfeck, - serhiy.storchaka
messages: + msg409054
2021-12-22 22:21:25terry.reedysetnosy: + serhiy.storchaka
messages: + msg409053
2021-12-22 10:45:25mceplsetmessages: + msg409022
2021-12-22 10:44:04mceplsetmessages: + msg409020
2021-12-22 10:42:51mceplsetmessages: + msg409018
2021-12-22 01:32:25terry.reedysetmessages: + msg409008
2021-12-22 01:03:59mceplsetmessages: + msg409007
2021-12-22 00:32:00terry.reedysetfiles: + tem.py
versions: + Python 3.11, - Python 3.8
nosy: + taleinat

messages: + msg409004

type: crash -> behavior
2021-12-21 23:39:10mceplcreate