ไม่สามารถนำเข้า tkinter ใน Python3 แต่ใช้งานได้ใน python2 (Python3.7 macOS Catalina)

Oct 28 2020

วิ่งpython3เพื่อนำฉันเข้าสู่ python จากนั้นก็เรียกใช้import tkinterผลลัพธ์ในข้อผิดพลาดต่อไปนี้

File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 36, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'

อย่างไรก็ตามเมื่อเข้าไปในpythonเชลล์ (ไม่ใช่ python3) import tkinterทำงานได้ดีตามที่คาดไว้ ฉันจะให้ tkinter ทำงานกับ Python3 ได้อย่างไร

โปรดทราบสิ่งต่อไปนี้:

  • ฉันใช้ MacOS Catalina
  • ฉันกำลังทำงานกับ Python 3.7.3 (ปิดpython3 --version)
  • ดูเหมือนว่าไฟล์ปฏิบัติการ python3 ของฉันคือ ( /usr/bin/python3)
  • เส้นทางสำหรับ tkinter คือ /Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/tkinter/
  • ฉันสามารถวิ่งidleใน Terminal ได้ แต่วิ่งไม่ได้idle3
  • ฉันไม่คิดว่าฉันกำลังใช้virtualenvหรือpyenvหรืออะไรอย่างนั้น

คำตอบ

MarkRoseman Oct 28 2020 at 06:07

คุณต้องติดตั้ง Tcl / Tk แยกต่างหาก ลองใช้ตัวติดตั้งไบนารี Python3 เวอร์ชันใหม่กว่าจาก python.org ซึ่งรวม Tcl / Tk

LucasUrban Oct 31 2020 at 07:40

ไม่ได้ติดตั้งผ่านtkinter idleลองidle3. หากไม่มีอะไรให้ติดตั้งpython3-tkโดยใช้โปรแกรมติดตั้งระบบปฏิบัติการของคุณ (ฉันมีaptฉันใช้ Debian) โชคดี!

LearningProgramming Nov 03 2020 at 13:01

อย่าใช้ _ ค่ะ import _tkinter

ใช้ import tkinter

ฉันมั่นใจว่าจะได้ผล ถ้ามันใช้งานได้ให้ทำเครื่องหมายคำตอบของฉัน