Python如何将用户提供的IP地址写入文件?

时间:2013-10-01 11:22:16

标签: python tkinter ip

所以这就是问题所在。我已经开始使用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函数还是与嵌套函数有关。

我很乐意,如果有人可以帮助我,因为它已经困扰我好几天了!

2 个答案:

答案 0 :(得分:0)

我会在IpConfigNew内移动这两行,最后:

#Calls on function IpStore in order to store the new IP    
Button(IpConfig,text = 'Done', command = IpStore).place(x=150,y=30)
IpConfig.mainloop()

编辑:似乎存在多种事件循环的错误 StringVar,请参阅Tkinter Folklore。无论如何,在你的情况下,你想要显示一个对话框,你 不需要另一个Tk循环。所以改变你的代码:

def IpConfigNew():
    #Creates a Toplevel window instead of a new "main" window in a new loop
    IpConfig = Toplevel(app) 

    ...

    #IpConfig.mainloop()
    app.wait_window(IpConfig)

这解决了我测试中的问题。

答案 1 :(得分:0)

我事先没有让我的程序布局非常清晰,我现在会尝试解释它。

我的主窗口和主窗口(称为“app”)我已经设置了一个菜单栏。此菜单栏有一个名为“options”的级联和一个名为“PromptIpReconfig”的命令:

app = Tk() 
filemenu.add_command(label="Configure IP", command = PromptIpReconfig)
filemenu.add_command(label="Quit", command=app.destroy)
menubar.add_cascade(label='Options',menu = filemenu)

当我调用“PromptIpReconfig”函数时,会发生以下情况:

def PromptIpReconfig():
   confirm = tkMessageBox.askyesno(title = "Configure Server IP", message = "Are you sure?")
   if confirm >0:
      IpConfigNew()
   else:
      return

如果用户决定说是,则调用IpConfigNew()

#CONFIGURE NEW IP
def IpConfigNew():
    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)
    Entry(IpConfig,textvariable=IpNew, bg = "light blue").place(x=4,y=35)

    #Store New Ip
    def IpStore():
        GetIpNew = IpNew.get()
        mypath = str('Latest Server')
        if not os.path.isdir(mypath):
            os.makedirs(mypath)
        StoreLatestServer = open('Latest Server\NewIp.txt', 'w')
        StoreLatestServer.write("%s"%(GetIpNew))
        StoreLatestServer.close()
        IpConfig.destroy()

    Button(IpConfig,text = 'Done', command = IpStore).place(x=150,y=30)
    IpConfig.mainloop()

IpConfigNew在主窗口中打开子窗口IpConfig = Tk()。输入新IP后,子窗口提供单击按钮。单击按钮后,它将调用命令“IpStore”:

#Store New Ip
def IpStore():
    GetIpNew = IpNew.get()
    mypath = str('Latest Server')
    if not os.path.isdir(mypath):
        os.makedirs(mypath)
    StoreLatestServer = open('Latest Server\NewIp.txt', 'w')
    StoreLatestServer.write("%s"%(GetIpNew))
    StoreLatestServer.close()
    IpConfig.destroy()

所以基本上我的程序的整体布局如下:

from Tkinter import *
import os
import tkMessageBox
#Create parent window
app = Tk()

def IpConfigNew():
   #Creates new window 
   IpConfig = Tk()
   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?")
   if confirm >0:
      #In the case that the user says yes
      IpConfigNew()
   else:
      return


#Create menu bar
filemenu.add_command(label="Configure IP", command = PromptIpReconfig)
filemenu.add_command(label="Quit", command=app.destroy)
menubar.add_cascade(label='Options',menu = filemenu)

app.mainloop()

我很确定问题是IpConfigNew函数,因为它似乎有效:

from Tkinter import *
import tkMessageBox
import os


#Creates new window   
IpConfig = Tk()
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()

这应该让你们更清楚地了解我正在处理的事情。 感谢