2

Tkinter GUI を介してファイルを開く方法を考えています。たとえば、インターフェイスに .txt ファイルを開くボタンがあります。テキストボックスに読み込まれるか、テキストエディターで開くかは関係ありません。テキストエディタで開くことをお勧めします。

def openInstruktion():
    f= open("instruktioner.txt")

instruktionBtn = Button(root, text='Spelinstruktioner', command=openInstruktion)
instruktionBtn.grid(row=6, column=0)

Web で回答を検索しましたが、ほとんどの人はメニューバーを使用しています。上記のボタンから開きたいです。

4

4 に答える 4

2

したがって、ファイルに対して何かをしたい場合、操作は function で行われますopenInstruktion

def openInstrucktion():
    f= open("instruktioner.txt")
    #t is a Text widget
    t.insert(1.0, f.read())

または、エディターで開きたい場合:

def openInstrucktion():
    os.system('emacs instrucktioner.txt')
于 2013-07-09T16:17:57.537 に答える
2

デフォルトのプログラムでファイルを開きたい場合は、os モジュールを使用できます。

def openInstruktion():
    from os import startfile
    startfile("c:\\path\\to\\file")

instruktionBtn = Button(root, text='Spelinstruktioner', command=openInstruktion)
instruktionBtn.grid(row=6, column=0)

または、特定のプログラムで開きたい場合は、サブプロセス モジュールを試してください。

def openInstruktion():
    from subprocess import call
    call("notepad c:\\path\\to\\file")

instruktionBtn = Button(root, text='Spelinstruktioner', command=openInstruktion)
instruktionBtn.grid(row=6, column=0)

ただし、テキストボックスで開きたい場合は、次のようにすることができます。

file = open("c:\\path\\to\\file").read()
textbox.insert(0.0, file)

あなたの最善の策は、おそらくデフォルトのエディタで開くことです(別のプログラムで開くことは人々が望むものではないかもしれませんし、テキストボックスで開くとグラフィックが貧弱になります).

于 2013-07-09T17:19:54.370 に答える
0
def help():
    readme = "F:\\yourpath\\readme.txt"
    os.startfile(readme)

この関数を使用するだけですが、最初import osに上部で実行してください..

于 2020-06-03T08:18:16.990 に答える