2017-08-01 4 views
-2

私は将来のプログラムであるこのコードを持っています。これは非常に単純なプログラムで、リセットと実際にあなたの将来をもたらすボタンの2つのボタンがあります。それはあなたの未来が出力されるラベルを持っています。私はテキストファイルからテキストをインポートしますが、プログラムを実行するとテキストファイルから空白になることがありますが、どうやってこれを止めますか?プログラムがテキストファイルから空白を出力することがありますが、これをどうやって止めるのですか?

私はpythonとtkinterに新しいので、コードは非常に簡単で、自分の知識を構築するための小さなプログラムを構築しています。

def pick(): 
    with open("Future.txt") as f: 
     lines = filter(None, (line.rstrip() for line in f)) 
     test = (random.choice(lines)) 
     print(test) 
    if test ==(): 
     test = ("You wil get old and die alone") 
    Label1.configure(text = test) 
    FutureButton.configure(state = DISABLED) 
    ResetButton.configure(state = NORMAL) 

内のすべての文字列のコピーを返しrstrip()方法:この問題に関するすべてのヘルプは、あなたが行を読んだときに感謝

from tkinter import* 
import random 

window =Tk() 



#static properties 
window.title("Your Future") 
window.resizable(0,0) # this stop the window from being able to be resized 

#Things in the application 
Label1 = Label(window, relief = 'groove', width = 100, height = 2) 
FutureButton = Button(window) 
ResetButton = Button(window) 

#text in button 
FutureButton.configure(text = "Get your Furture") 
ResetButton.configure(text = "Reset") 
ResetButton.configure(state = DISABLED) 

#dynamic properties 
def pick(): 
    with open("Future.txt") as f: 
     lines = f.readlines() 
     test = (random.choice(lines)) 
     print(test) 
    if test ==(): 
     test = ("You wil get old and die alone") 
    Label1.configure(text = test) 
    FutureButton.configure(state = DISABLED) 
    ResetButton.configure(state = NORMAL) 

def reset(): 
    Label1.configure(text = "..........") 
    FutureButton.configure(state = NORMAL) 
    ResetButton.configure(state = DISABLED) 

FutureButton.configure(command = pick) 
ResetButton.configure(command = reset) 

#Placing shit 
Label1.grid(row = 1, column = 1, columnspan = 10,) 
FutureButton.grid(row = 2, column = 5, rowspan = 3) 
ResetButton.grid(row = 3, column = 6, rowspan = 2,) 

window.mainloop() 
+3

テキストファイルに内容がない行がありますか(末尾に改行などありますか?) –

+0

"test"変数には空白行が含まれるため、このコードでは空白行が追加されます。試してみてください。 'test = random.choice(lines).strip()' – Enfenion

答えて

0

rstrip()を使用して空白行を無視し、多くのappretiactedだろう文字列の最後から削除されています(デフォルトの空白文字)。

+0

助けてくれてありがとう。 –

+2

助けてくれてありがとう。回答が正しければ、その答えにチェックを入れてください。 –