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

Named widget has NoneType after single line creation #89134

Closed
rcrosser mannequin opened this issue Aug 21, 2021 · 2 comments
Closed

Named widget has NoneType after single line creation #89134

rcrosser mannequin opened this issue Aug 21, 2021 · 2 comments
Labels
3.9 only security fixes topic-tkinter type-bug An unexpected behavior, bug, or error

Comments

@rcrosser
Copy link
Mannequin

rcrosser mannequin commented Aug 21, 2021

BPO 44971
Nosy @stevendaprano
Files
  • test_pack.py: python code. Works with the top 3 buttons, crashes with the lower 3 buttons.
  • 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 2021-08-21.16:00:34.153>
    created_at = <Date 2021-08-21.14:55:34.791>
    labels = ['invalid', 'type-bug', 'expert-tkinter', '3.9']
    title = 'Named widget has NoneType after single line creation'
    updated_at = <Date 2021-08-21.16:00:34.151>
    user = 'https://bugs.python.org/rcrosser'

    bugs.python.org fields:

    activity = <Date 2021-08-21.16:00:34.151>
    actor = 'steven.daprano'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-08-21.16:00:34.153>
    closer = 'steven.daprano'
    components = ['Tkinter']
    creation = <Date 2021-08-21.14:55:34.791>
    creator = 'rcrosser'
    dependencies = []
    files = ['50228']
    hgrepos = []
    issue_num = 44971
    keywords = []
    message_count = 2.0
    messages = ['400032', '400034']
    nosy_count = 2.0
    nosy_names = ['steven.daprano', 'rcrosser']
    pr_nums = []
    priority = 'normal'
    resolution = 'not a bug'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue44971'
    versions = ['Python 3.9']

    @rcrosser
    Copy link
    Mannequin Author

    rcrosser mannequin commented Aug 21, 2021

    Declaring a widget in the following form:
    ...
    label2 = ttk.Label(root, text='Show2 Label').pack()
    ...
    leaves the widget with a NoneType, and unable to be assigned to (for instance to assign new text). If giving a widget a name, I expect to use it later in the program.
    This declaration works correctly:
    ...
    label2 = ttk.Label(root, text='Show2 Label')
    label2.pack()
    ...
    Simple tkinter program attached. Only tested with 3.9.6 on Win 10.

    @rcrosser rcrosser mannequin added type-crash A hard crash of the interpreter, possibly with a core dump 3.9 only security fixes topic-tkinter labels Aug 21, 2021
    @stevendaprano
    Copy link
    Member

    I'm sorry if you don't like the design of the pack() method, but all the examples in the documentation show how it behaves. It is behaving as documented and designed.

    https://docs.python.org/3/library/tkinter.html

    The bug here is in your own code. You already know the correct way to write this, as you already stated:

        label2 = ttk.Label(root, text='Show2 Label')
        label2.pack()

    Writing it as ttk.Label(root, text='Show2 Label').pack() returns None, as designed, which then consequently fails when you try to operate on None.

    You say:

    "If giving a widget a name, I expect to use it later in the program."

    But you don't give the widget a name. What you are doing is the equivalent of this:

        temp = ttk.Label(root, text='Show2 Label')  # hidden temp object
        label = temp.pack()  # Returns None
        del temp  # hidden temp object is garbage collected

    except that temp is never directly accessible by you.

    Or if you prefer another analogy:

        number = (1 + 2) - 3

    and then get surprised that number is zero rather than 3 because "I gave (1+2) a name". No, you gave a name to the *whole expression*, which evaluates to 0, not 3. And in the Label case, the *whole expression* evaluates to None.

    Also, the code doesn't crash. It raises an exception, which is the expected behaviour for trying to access non-existent attributes.

    @stevendaprano stevendaprano added invalid type-bug An unexpected behavior, bug, or error and removed type-crash A hard crash of the interpreter, possibly with a core dump labels Aug 21, 2021
    @stevendaprano stevendaprano added invalid type-bug An unexpected behavior, bug, or error and removed type-crash A hard crash of the interpreter, possibly with a core dump labels Aug 21, 2021
    @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
    3.9 only security fixes topic-tkinter type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant