2

In Qgis when I add the following code in a .py file and try to reload it in QGIS:

import tkinter as tk
from tkinter import filedialog

root = tk.Tk()
root.withdraw()

file_path = filedialog.askopenfilename()

print(file_path)

throws this: ModuleNotFoundError: No module named '_tkinter'

But when I create a .py file with the same instructions and then I execute the program, there is no issue, the program open a window. Why? I have been searching but none of the answers works for me.

  • QGIS incorporates its own stand-alone Python which appears not to include Tkinter support. I can [find quite a few useful links about this with a simple web search](https://duckduckgo.com/?q=qgis+tkinter). – Karl Knechtel Apr 28 '23 at 21:03

1 Answers1

-1

You probably need to install it

sudo apt-get install python3-tk 

I have been using Tkinter for a while now. Why don't you try this and let me know if it worked?

try:
    # for Python2
    from Tkinter import *   ## notice capitalized T in Tkinter 
except ImportError:
    # for Python3
    from tkinter import *   ## notice lowercase 't' in tkinter here

for more detail link