Skip to content
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

Tkinter Variables require a proper master #62331

Closed
terryjreedy opened this issue Jun 4, 2013 · 5 comments
Closed

Tkinter Variables require a proper master #62331

terryjreedy opened this issue Jun 4, 2013 · 5 comments
Labels
topic-tkinter type-bug An unexpected behavior, bug, or error

Comments

@terryjreedy
Copy link
Member

BPO 18131
Nosy @terryjreedy, @vadmium, @serhiy-storchaka

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2020-12-21.09:13:31.370>
created_at = <Date 2013-06-04.01:12:59.951>
labels = ['type-bug', 'expert-tkinter']
title = 'Tkinter Variables require a proper master'
updated_at = <Date 2020-12-21.09:13:31.369>
user = 'https://github.com/terryjreedy'

bugs.python.org fields:

activity = <Date 2020-12-21.09:13:31.369>
actor = 'serhiy.storchaka'
assignee = 'none'
closed = True
closed_date = <Date 2020-12-21.09:13:31.370>
closer = 'serhiy.storchaka'
components = ['Tkinter']
creation = <Date 2013-06-04.01:12:59.951>
creator = 'terry.reedy'
dependencies = []
files = []
hgrepos = []
issue_num = 18131
keywords = []
message_count = 5.0
messages = ['190574', '228416', '228523', '228557', '383495']
nosy_count = 4.0
nosy_names = ['terry.reedy', 'gpolo', 'martin.panter', 'serhiy.storchaka']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue18131'
versions = ['Python 2.7', 'Python 3.4', 'Python 3.5']

@terryjreedy
Copy link
Member Author

The signature for tkinter class Variable and its subclasses StringVar, IntVar, DoubleVar, BooleanVar is
def __init__(self, master=None, value=None, name=None):
However, the None default is invalid because of
self._tk = master.tk
The preceding lines
if not master:
master = _default_root
might suggest that None is acceptible. But _default_root is set to None at the top of the file, so that just replaces None with None.

If Variables must have a gui widget parent, then they cannot be used in gui-free tests, even though they have no graphic representation.

Moveover, not everything is a proper master. configSectionNameDialog.py . GetCfgSectionNameDialog(Toplevel) . Create.Widgets(self) has these lines:
self.name = StringVar(self)
self.fontSize = StringVar(self)
However, these are not (always) garbage collectable.

After fixing the dialog test at the bottom of the file, and running it with 3.3 python_d -m idlelib.configSectionNameDialog (bpo-18130), and adding the gc flag, I repeatedly got
[sys:1: ResourceWarning: gc: 2 uncollectable objects at shutdown [<tkinter.StringVar object at 0x02C597E0>, <tkinter.StringVar ...]]
(This only showed when running from the console, not when running within Idle started within the interactive debug interpreter.)

Running the test multiple times within a session, by repeatedly creating new dialogs with the [dialog] button in the test window, multiplied the uncollected objects.

Replacing 'self' with 'self.parent' solved the problem, at least in the test situation.

@terryjreedy terryjreedy added topic-tkinter type-bug An unexpected behavior, bug, or error labels Jun 4, 2013
@BreamoreBoy
Copy link
Mannequin

BreamoreBoy mannequin commented Oct 4, 2014

Just a gentle reminder.

@terryjreedy
Copy link
Member Author

My first message discussed two different issues. The first, generalized, is that unconditionally using tkinter._default_root, which can be either None or absent, as a backup for master=None in __init__ functions, seems a bit sloppy. Image.__init__ does this check
if not master:
master = _default_root
if not master:
raise RuntimeError('Too early to create image')
but that will fail if _default_root has been deleted. Also, the message is wrong in that it is not 'too early' if an explicit master is passed. I am thinking that all uses of _default_root should raise something like WhateverError("An explicit master is required when _default_root is None or deleted.") Serhiy, what do you think?

The second issue noted that while widgets often have a container widget as master, the non-graphics classes like Variable and Font should have a Tk and not a widget as master. I have corrected idlelib.configHandler and there is a tkinter patch on another issue (applied yet?) to automatically replace master=widget by master-widget.tk. So Variable calls are no longer part of this issue.

@serhiy-storchaka
Copy link
Member

I am thinking that all uses of _default_root should raise something like
WhateverError("An explicit master is required when _default_root is None or
deleted.") Serhiy, what do you think?

Technically all works (raises some exception), but I agree that error message
can be more friendly. But _default_root is implementation detail. This name
doesn't mean anything for non-experienced user (and experienced user already
knows about these mystical RuntimeError and NameError). The message should say
something like "No default root window" if _default_root is None and "The
master argument is mandatory after calling NoDefautRoot()" if _default_root
isn't set.

And now we came to the way how to create default root window. bpo-4343 looks
related (I haven't looked at it close however).

Second issue was fixed in bpo-22068.

@serhiy-storchaka
Copy link
Member

And the first issue was fixed in bpo-42630.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-tkinter type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants