Skip to content

bpo-29446: Improve tkinter 'import *' situation #14864

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

Merged
merged 5 commits into from
Jul 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion Lib/tkinter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@

import enum
import sys
import types

import _tkinter # If this fails your Python may not be configured for Tk
TclError = _tkinter.TclError
from tkinter.constants import *
import re


wantobjects = 1

TkVersion = float(_tkinter.TK_VERSION)
Expand Down Expand Up @@ -4569,5 +4569,9 @@ def _test():
root.mainloop()


__all__ = [name for name, obj in globals().items()
if not name.startswith('_') and not isinstance(obj, types.ModuleType)
and name not in {'wantobjects'}]

if __name__ == '__main__':
_test()
2 changes: 2 additions & 0 deletions Lib/tkinter/colorchooser.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

from tkinter.commondialog import Dialog

__all__ = ["Chooser", "askcolor"]


#
# color chooser class
Expand Down
8 changes: 5 additions & 3 deletions Lib/tkinter/commondialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@
# written by Fredrik Lundh, May 1997
#

from tkinter import *
__all__ = ["Dialog"]

from tkinter import Frame


class Dialog:

command = None
command = None

def __init__(self, master=None, **options):
self.master = master
self.master = master
self.options = options
if not master and options.get('parent'):
self.master = options['parent']
Expand Down
5 changes: 3 additions & 2 deletions Lib/tkinter/dialog.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# dialog.py -- Tkinter interface to the tk_dialog script.

from tkinter import *
from tkinter import _cnfmerge
from tkinter import _cnfmerge, Widget, TclError, Button, Pack

__all__ = ["Dialog"]

DIALOG_ICON = 'questhead'

Expand Down
3 changes: 2 additions & 1 deletion Lib/tkinter/dnd.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@

"""


import tkinter

__all__ = ["dnd_start", "DndHandler"]


# The factory function

Expand Down
14 changes: 10 additions & 4 deletions Lib/tkinter/filedialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@
directory dialogue available in Tk 8.3 and newer.
These interfaces were written by Fredrik Lundh, May 1997.
"""
__all__ = ["FileDialog", "LoadFileDialog", "SaveFileDialog",
"Open", "SaveAs", "Directory",
"askopenfilename", "asksaveasfilename", "askopenfilenames",
"askopenfile", "askopenfiles", "asksaveasfile", "askdirectory"]

from tkinter import *
import fnmatch
import os
from tkinter import (
Frame, LEFT, YES, BOTTOM, Entry, TOP, Button, Tk, X,
Toplevel, RIGHT, Y, END, Listbox, BOTH, Scrollbar,
)
from tkinter.dialog import Dialog
from tkinter import commondialog

import os
import fnmatch


dialogstates = {}

Expand Down
5 changes: 3 additions & 2 deletions Lib/tkinter/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
# written by Fredrik Lundh, February 1998
#

__version__ = "0.9"

import itertools
import tkinter

__version__ = "0.9"
Copy link
Author

@flavianh flavianh Jul 20, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had to move __version__ here to abide to PEP8 also

__all__ = ["NORMAL", "ROMAN", "BOLD", "ITALIC",
"nametofont", "Font", "families", "names"]

# weight/slant
NORMAL = "normal"
Expand Down
4 changes: 4 additions & 0 deletions Lib/tkinter/messagebox.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@

from tkinter.commondialog import Dialog

__all__ = ["showinfo", "showwarning", "showerror",
"askquestion", "askokcancel", "askyesno",
"askyesnocancel", "askretrycancel"]

#
# constants

Expand Down
4 changes: 2 additions & 2 deletions Lib/tkinter/scrolledtext.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
Place methods are redirected to the Frame widget however.
"""

__all__ = ['ScrolledText']

from tkinter import Frame, Text, Scrollbar, Pack, Grid, Place
from tkinter.constants import RIGHT, LEFT, Y, BOTH

__all__ = ['ScrolledText']


class ScrolledText(Text):
def __init__(self, master=None, **kw):
Expand Down
14 changes: 14 additions & 0 deletions Lib/tkinter/test/test_tkinter/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@

class MiscTest(AbstractTkTest, unittest.TestCase):

def test_all(self):
self.assertIn("Widget", tkinter.__all__)
# Check that variables from tkinter.constants are also in tkinter.__all__
self.assertIn("CASCADE", tkinter.__all__)
self.assertIsNotNone(tkinter.CASCADE)
# Check that sys, re, and constants are not in tkinter.__all__
self.assertNotIn("re", tkinter.__all__)
self.assertNotIn("sys", tkinter.__all__)
self.assertNotIn("constants", tkinter.__all__)
# Check that an underscored functions is not in tkinter.__all__
self.assertNotIn("_tkerror", tkinter.__all__)
# Check that wantobjects is not in tkinter.__all__
self.assertNotIn("wantobjects", tkinter.__all__)

def test_repr(self):
t = tkinter.Toplevel(self.root, name='top')
f = tkinter.Frame(t, name='child')
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,7 @@ Zac Hatfield-Dodds
Shane Hathaway
Michael Haubenwallner
Janko Hauser
Flavian Hautbois
Rycharde Hawkes
Ben Hayden
Jochen Hayek
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Make `from tkinter import *` import only the expected objects.