Python:Server Creator - 不写入文件

时间:2013-12-24 02:03:28

标签: python user-interface tkinter pickle

是python的新手,只是想在GUI中创建一个简单的Minecraft服务器属性文件创建者,我发现它没有正确写入文件并给我这个错误 - “

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()

1 个答案:

答案 0 :(得分:0)

当你收到错误时,这意味着你正试图玩一个没有意义的关闭文件。  所以在做任何事之前确保文件是打开的......

这可能意味着您的一行代码在最终f.write()执行之前关闭了该文件。我可以提供的最佳建议是在f = open('file','w')

之前致电f.write()