2013-12-24 46 views
0

新的python,只是試圖在GUI中創建一個簡單的Minecraft服務器屬性文件的創建者,我發現它沒有正確寫入文件並給我這個錯誤 - 「Python:服務器創建器 - 沒有寫入文件

Exception in Tkinter callback 
Traceback (most recent call last): 
    File "D:\Python33\lib\tkinter\__init__.py", line 1475, in __call__ 
    return self.func(*args) 
    File "D:\Python33\Projects\Server\GUI.py", line 26, in top 
    f.write(line1) 
ValueError: I/O operation on closed file. 

任何幫助將受到極大的歡迎!

import pickle, shelve, sys, os 
from tkinter import * 
f = open('server.properties', 'w') 
def start(): 
    os.system("start.bat") 
def top(): 
    val1 = players.get() 
    val1 = str(val1) 
    line1 = ("max-player="+val1+"\n") 
    line1 = str(line1) 
    var2 = pvp.get() 
    var2 = str(var2) 
    line2 = ("pvp="+var2+"\n") 
    var3 = motd.get() 
    var3 = str(var3) 
    line3 = ("motd="+var3+"\n") 
    var4 = monsters.get() 
    var4 = str(var4) 
    line4 = ("spawn-monsters="+var4+"\n") 
    var5 = gamemode.get() 
    var5 = str(var5) 
    line5 = ("gamemode="+var5+"\n") 
    var6 = internalip.get() 
    var6 = str(var6) 
    line6 = ("server-ip="+var6+"\n") 
    f.write(line1) 
    f.write(line2) 
    f.write(line3) 
    f.write(line4) 
    f.write(line5) 
    f.write(line6) 
    f.close() 

gui = Tk() 
gui.minsize(width=500, height=500) 
gui.maxsize(width=500, height=500) 
players = StringVar() 
pvp = StringVar() 
motd = StringVar() 
monsters = StringVar() 
gamemode = StringVar() 
internalip = StringVar() 

gui.geometry("500x500") 
gui.title("Server Creator") 


lbl1 = Label(text="Please enter Y or N for each value given.").pack() 
btn1 = Button(text="OK", command=top).pack() 
lbl2 = Label(text="Enter the player limit below.").pack() 
ent1 = Entry(gui,textvariable=players).pack() 
lbl3 = Label(text="Enter true or false to enable PVP.").pack() 
ent2 = Entry(gui,textvariable=pvp).pack() 
lbl4 = Label(text="Enter the message of the day.").pack() 
ent3 = Entry(gui,textvariable=motd).pack() 
lbl5 = Label(text="Enter true or false for spawning monsters.").pack() 
ent4 = Entry(gui,textvariable=monsters).pack() 
lbl6 = Label(text="Enter 2 for adventure, 1 for creaive or 0 for survival").pack() 
ent5 = Entry(gui,textvariable=gamemode).pack() 
lbl7 = Label(text="Enter the internal server IP.").pack() 
ent6 = Entry(gui,textvariable=internalip).pack() 

lblfinal = Label(text="Click the Start button below to being the server,\n make sure you have clicked the OK button above to save your settings.").pack() 
btnstart = Button(text="Start", command=start).pack() 


gui.mainloop() 
+0

這只是表示您正在嘗試對已關閉的文件執行輸入/輸出操作。爲了獲取此錯誤,您需要打開一個文件。 –

回答

0

當你得到那個錯誤時,這意味着你正在嘗試使用一個沒有意義的封閉文件。 所以請確保該文件在執行任何操作之前都處於打開狀態......

這可能意味着您的一行代碼正在執行最終的f.write()之前關閉文件。我可以給出的最好建議是在f.write()之前撥打f = open('file','w')

+0

我該如何打開文件才能使用它?據我所知,使用f = open('server.properties','w')打開文件。 – user2980960

+0

是的,這是正確的,中間的一些代碼行將關閉你的文件,以確保在f.write之前使用'f = open('server.properties','w')'再次打開文件 –

+0

@ user2980960如果它幫助你,接受這個答案.. –