Ubuntu 14.04 上的 Tkinter 似乎無法運作

Ubuntu 14.04 上的 Tkinter 似乎無法運作

我收到以下回溯:

Traceback (most recent call last):
  File "tkinter_basic_frame.py", line 4, in <module>
    from Tkinter import Tk, Frame, BOTH
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 42, in 
    raise ImportError, str(msg) + ', please install the python-tk package'
ImportError: No module named _tkinter, please install the python-tk package

這是我嘗試運行的演示腳本:

#!/usr/bin/python
# -*- coding: utf-8 -*-

from Tkinter import Tk, Frame, BOTH


class Example(Frame):

    def __init__(self, parent):
        Frame.__init__(self, parent, background="white")   

        self.parent = parent

        self.initUI()

    def initUI(self):

        self.parent.title("Simple")
        self.pack(fill=BOTH, expand=1)


def main():

    root = Tk()
    root.geometry("250x150+300+300")
    app = Example(root)
    root.mainloop()  


if __name__ == '__main__':
    main()  

據我所知,Tkinter 應該包含在 Python 2.7 中。為什麼我會收到回溯? ubuntu 不包含標準 python 發行版嗎?

這已經解決了。我必須在突觸中手動安裝它(同時從另一個論壇獲得提示),請參見此處:

在此輸入影像描述

維基百科說:「Tkinter 是與 Tk GUI 工具包的 Python 綁定。它是 Tk GUI 工具包的標準 Python 介面1是Python事實上的標準GUI,2並且包含在標準 Windows 和 Mac OS X 安裝的 Python 中。

維基百科上的 Tkinter

答案1

只需安裝tkinter

sudo apt-get install python-tk

或者如果你選擇python3

sudo apt-get install python3-tk

http://tkinter.unpythonic.net/wiki/How_to_install_Tkinter

答案2

按照腳本所說的去做:

ImportError:沒有名為 _tkinter 的模組,請安裝 python-tk 套件

Tkinter 不是標準 python 的一部分在基於 Linux 的作業系統上。它是 GUI 創建的小部件擴充。來自Python 維基:

Tkinter 是 Python 事實上的標準 GUI(圖形使用者介面)套件。它是 Tcl/Tk 之上的一個薄的物件導向層。

頂部通常表示額外的包。無論如何,這裡有一個鏈接python-tk 包

相關內容