lockks
lockks

Reputation: 65

Problem with using Tkinter in Visual Studio Code

I'm trying to use Tkinter with Python in Visual Studio Code and its working fine except I get this yellow underline here:

(code)

and when I hover on it it shows a lot of stuff here:

hovered

Upvotes: 1

Views: 731

Answers (2)

Steven-MSFT
Steven-MSFT

Reputation: 8421

It's the design logic of pylint. If you want to solve it, you can take these methods:

  1. You can switch to another linter, such as mypy,pylama,flake8 and so on. But you will still encounter the warning of unable to detect undefined names.

  2. Explicit the thing you want to import, such as: from tkinter import Tk, Label. This is recommended.

  3. Add this in the settings.json file:

    "python.linting.pylintArgs": [ "--disable=all --enable=F,E,unreachable,duplicate-key,unnecessary-semicolon,global-variable-not-assigned,unused-variable,binary-op-exception,bad-format-string,anomalous-backslash-in-string,bad-open-mode,unused-wildcard-import,wildcard-import" ],

    The beginning of the settings was the default setting of pylint in the VSCode, unused-wildcard-import,wildcard-import was set to remove warning of wildcard.

Upvotes: 1

gx9KHwqh
gx9KHwqh

Reputation: 11

In python there are different ways to import packages.

  1. from ... import ... [as ...]
  2. import ... [as ...]

In your example you use the first variant with a so-called wildcard. This means you’ll put all the names from the tkinter package into your global namespace. It would be better and is common to import tkinter with import tkinter as tk in order to maintain a certain encapsulation.

You then address objects from tkinter with tk.object_name.

Upvotes: 1

Related Questions