所以这就是问题所在。我已经开始使用 python 一段时间了,我遇到了一个问题。虽然一开始看起来很简单,但它让我连续忙了几个小时。*运行程序时我没有收到任何语法错误,尽管程序无法将新 IP 写入文件。
我正在为一个程序创建一个函数,该函数要求客户端提供一个新的 IP 地址,因为服务器(我的 IP)当前不是静态的。由于我的 IP 更改非常频繁,我想为客户(试图与我建立连接)提供更改他们尝试连接的 IP 的选项。
所以这里是函数:
#CONFIGURE NEW IP
def IpConfigNew():
#Creates new window
IpConfig = Tk()
IpConfig.configure(background = 'white')
IpConfig.title('Configure IP')
IpConfig.geometry('300x60+260+380')
IpNew = StringVar()
Label(IpConfig, font = ('Helvetica',12), text = 'Please enter the IP address of the server', bg = 'white').place(x=0,y=4)
#Creates box for user to type IP in.
Entry(IpConfig,textvariable=IpNew, bg = "light blue").place(x=4,y=35)
#Store New Ip NOTE that it is nested within IpConfigNew
def IpStore():
#Retrieves new IP from text box and stores it in variable
GetIpNew = IpNew.get()
mypath = str('Latest Server')
#Creates directory to write new IP to.
if not os.path.isdir(mypath):
os.makedirs(mypath)
StoreLatestServer = open('Latest Server\NewIp.txt', 'w')
#Writes new IP
StoreLatestServer.write("%s"%(GetIpNew))
StoreLatestServer.close()
IpConfig.destroy()#Closes window
#Calls on function IpStore in order to store the new IP
Button(IpConfig,text = 'Done', command = IpStore).place(x=150,y=30)
IpConfig.mainloop()
def PromptIpReconfig():
confirm = tkMessageBox.askyesno(title = "Configure Server IP", message = "Are you sure?")
#Checks to see if the user chose to change IP
if confirm >0:
#In the event that the user said yes go to IpConfigNew
IpConfigNew()
else:
return
#Configure Menu Bar #Sets up Menu Bar for parent Window(app)
menubar = Menu(app)
filemenu = Menu(menubar,tearoff = 0)
# Goes to PromptIpReconfig (Prompts user to Reconfigure the IP after clicking button)
filemenu.add_command(label="Configure IP", command = PromptIpReconfig)
filemenu.add_command(label="Quit", command=app.destroy)
menubar.add_cascade(label='Options',menu = filemenu)
app.config(menu=menubar)#Draws menubar on parent window(app)
我不知道为什么它不工作,因为我之前做过这个,但期望略有不同。当我尝试将新 IP 写入文件时,没有任何内容写入文件。新目录已创建,因此我知道该功能正在运行。我对比了我之前制作的那个程序和这个程序。我发现,如果我这样做,效果很好:
#CONFIGURE NEW IP
#Creates new window
IpConfig = Tk()
IpConfig.configure(background = 'white')
IpConfig.title('Configure IP')
IpConfig.geometry('300x60+260+380')
IpNew = StringVar()
Label(IpConfig, font = ('Helvetica',12), text = 'Please enter the IP address of the server', bg = 'white').place(x=0,y=4)
#Creates box for user to type IP in.
Entry(IpConfig,textvariable=IpNew, bg = "light blue").place(x=4,y=35)
#Store New Ip
def IpStore():
#Retrieves new IP from text box and stores it in variable GetIpNew
GetIpNew = IpNew.get()
mypath = str('Latest Server')
#Creates directory to write new IP to.
if not os.path.isdir(mypath):
os.makedirs(mypath)
StoreLatestServer = open('Latest Server\NewIp.txt', 'w')
#Writes new IP
StoreLatestServer.write("%s"%(GetIpNew))
StoreLatestServer.close()
#Closes window
IpConfig.destroy()
#Calls on function IpStore in order to store the new IP
Button(IpConfig,text = 'Done', command = IpStore).place(x=150,y=30)
IpConfig.mainloop()
我发现如果我在不使用菜单栏的情况下启动它,而只是在程序启动时启动它,它工作正常。我不确定问题是从菜单栏中调用 IpConfigNew 函数还是与我正在嵌套函数的事实有关。
如果有人可以在这里帮助我,我会很高兴的,因为它已经困扰了我好几天了!