2017-11-08 15 views
0

tk filedialogで開いて、選択したファイルの内容を読みたいと思います。ファイルを選択して「開く」ボタンをクリックすると、ファイルは開きませんが、ダイアログボックスは閉じます。選択したファイルをメモ帳で開くと、ファイル内のコンテンツを読み取ることができます。 @PM 2RINGと@Erikからヒントを使用してtkfiledialogでファイルを開き、メモ帳でコンテンツを読む方法

from tkinter import * 
from tkinter import filedialog 


def my_file(): 
    filename = filedialog.askopenfile(mode="r", initialdir="/", title="select file", 
             filetypes=(("text files", "*.txt"), ("all files", "*.*"))) 



root = Tk() 
root.geometry("300x300") 
           #open the selected txt file with notepad to read the content 
b = Button(root, text="open text file", command = my_file).pack() 

root.mainloop() 

EDIT 私は、ファイルを選択したときにnotepad.exeをして開くためにそれを返すためにfiledialog.askopenfilenameするfiledialog.askopenfileを変更しました。

from tkinter import * 
from tkinter import filedialog 
import os 


def my_file(): 
    filename = filedialog.askopenfilename(initialdir="C:/", title="select 
file", filetypes=(("text files", "*.txt"), ("all files", "*.*"))) 
    for f in filename: 
     return f 
    os.system(r"C:/notepad.exe" + f) 


root = Tk() 
root.geometry("300x300") 
          #open the selected txt file with notepad to read 
the content 
b = Button(root, text="open text file", command = my_file).pack() 

root.mainloop() 

その出力このエラー: これはコードです

Blockquote'C:/notepad.exet' is not recognized as an internal or external command, operable program or batch file. Blockquote

が、私はそれがterminal.Iにディレクトリを印刷する印刷するリターンを変更したときにサブプロセスを開こうとした

subprocess.Popen([r'C:\Program Files (x86)\Notepad.exe' + f]) 

また、これで開くこともありません。

+4

メモ帳でファイルを読みたい場合は、メモ帳で開いてみてください。 'filedialog.askopenfile'はTkinterプログラムの中でそれらを使うことができるようにファイルを開くためのものです。例えばTextウィジェットの中にテキストを表示します。私はメモ帳でファイルを開くには、関連する 'filedialog.askopenfilename'と' subprocess'モジュール関数または 'os.system'を使うことができますが、それはちょっと奇妙なIMHOでしょう。 –

答えて

0

ここではいくつか修正する必要があります。

まず、C:/notepad.exeはメモ帳の場所ではありません(少なくともデフォルト設定のWindowsマシンではありません)。notepad.exeを使用すれば、メモ帳を別の場所に移動したシステム引用が必要)。

第2に、実行中です。 。 。

for f in filename: 
    return f 
os.system(r"C:/notepad.exe" + f) 

あなたが思っていると思われることはしません。ここで実際に起こっていることは、プログラムが最初の文字(おそらく "C")を評価した後、返された値を受け取らないButtonウィジェットに文字列を返すことです。これはあなたの関数を破るので、実際にはあなたの宣言はos.system(r"C:/notepad.exe" + f)には決して達しません。

またそうでなければ、あなたにエラーをスローするように起こっているnotepad.exeC:/text.txtのようなものを実行している、メモ帳notepad.exeを開くために使用する文と、実際のファイル宣言fの間にスペースを含める必要があります。私はあなたがこれをやっている理由は見当もつかないことを追加したい

from tkinter import * 
from tkinter import filedialog 
import os 

def my_file(): 
    filename = filedialog.askopenfilename(initialdir="C:/", title="select file", filetypes=(("text files", "*.txt"), ("all files", "*.*"))) 
    os.system(r"notepad.exe " + filename) 

root = Tk() 
root.geometry("300x300") 
b = Button(root, text="open text file", command = my_file).pack() 

root.mainloop() 

は、なぜ単に Textウィジェット内のテキストを表示しない:あなたがやるべきことは何

は、以下のようなものです?あなたがここでやっていることは、ファイルエクスプローラを開くのではなく、メモ帳でファイルを開くための追加ステップを追加することです。ファイルを見つけて開き、プログラムを開き、ファイルエクスプローラを開き、それを開く。これは、プログラムの読み込み時間はもちろんのこと、少なくとも1つの余分なクリックを追加しています。

+0

なので、 notepad.exeのexcelとcsvのために私はエクセルエクステンションをxlsに変更したときにこのExcel.exeを開こうとしたので開けません。 –

+0

これは別の質問です。新しい質問をしてください。 –

+0

学習目的のために別のアプリケーションで開こうとしています –

0

次のコードは、ボタンとテキストウィジェットを表示します。ボタンがファイルに読み込まれ、テキストウィジェットに表示されます。これがあなたが念頭に置いたものだと思いますか?

from tkinter import * 
from tkinter import filedialog 

def my_file(): 
    file = filedialog.askopenfile(mode="r", initialdir="/", title="select file", 
            filetypes=(("text files", "*.txt"), ("all files", "*.*"))) 
    t.insert(END, file.read()) 
    file.close() 

root = Tk() 
root.geometry("300x300") 
          #open the selected txt file with notepad to read the content 
t = Text(root) 
t.grid(row=0, column=0) 
b = Button(root, text="open text file", command = my_file) 
b.grid(row=1, column=0) 

root.mainloop() 
+0

AttributeError: 'NoneType'オブジェクトに属性 'grid'がありません。ウィジェットと同時にpackとgridを使用しています。 –

+0

申し訳ありませんが、私はそこに追加の「パック」を残すつもりはありませんでした。 –

+0

あなたは私の悪いところです。私は何年もの間、グローバル変数を使ってコードを書いていないので、あまりにもあいまいで、安全にプレイすることに決めました。 –

1

PM2Ringで述べたように、私はos.system機能を使用します。 「os.system(command)」で説明したように、コマンドプロンプトでコマンドを実行したかのようにコマンドを実行します。その場合、os.system("Notepad c:/users/yourName/junk.txt)はjunk.txtという名前のファイルを開きます。言い換えれば

、あなたはfiledialog.askopenfilename呼び出しからファイル名を持っていたら、このような何か:あなたのコードに

import os 
os.system("Notepad " + filename) # where filename is the string that filedialog.askopenfilename returns 

実装することは、あまりにも悪いことが、あなたは、より完全な例が必要な場合はなりません私に知らせてください。

+0

os.system( "notepad.exe" + f)を呼び出すと、notepad.exeの後ろにスペースがありません--- os.system( "notepad.exe" + f) – Erik

関連する問題