Python如何将用户提供的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函数还是与我嵌套函数有关。

如果有人可以在这里困扰我好几天了,我会喜欢的!

姆吉耶尔

我将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)

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Python-如何将用户输入写入Excel文件?

来自分类Dev

makefile如何将用户定义的变量写入yaml文件以支持vagrantfile fetch参数

来自分类Dev

如何将用户数据写入根分区?

来自分类Dev

也将用户凭据写入文件

来自分类Dev

将用户输入写入文件

来自分类Dev

Python 2.7,将用户输入写入新的输出文件

来自分类Dev

如何将用户放入超级用户文件?

来自分类Dev

如何将用户添加到sudoers文件?

来自分类Dev

如何将用户文件选择链接到 StringSplit

来自分类Dev

使用tee将用户输入写入文件

来自分类Dev

如何将用户重定向到返回的URL python

来自分类Dev

我如何将用户空间中的缓冲区地址传递给内核

来自分类Dev

如何将用户帐户锁定到尽可能少的读取/写入/执行状态?

来自分类Dev

如何将用户的输入值提供给程序中运行的进程

来自分类Dev

如何将本地IP地址读入.htaccess文件?

来自分类Dev

Scrapy:将用户代理更改与IP地址更改同步

来自分类Dev

使用Express / Mongoose将用户IP地址保存到DB

来自分类Dev

Scrapy:将用户代理更改与IP地址更改同步

来自分类Dev

如何使用熊猫将用户定义的列名称写入CSV文件?

来自分类Dev

如何使用熊猫将用户定义的列名称写入CSV文件?

来自分类Dev

如何将用户关联到IPN

来自分类Dev

如何将用户锁定到目录?

来自分类Dev

如何将用户定义的函数作为python中的用户输入?

来自分类Dev

Shell脚本将用户定义的字符串写入文件

来自分类Dev

WIX将用户数据写入配置文件

来自分类Dev

将用户输入的字符串写入C中的文件

来自分类Dev

WIX将用户数据写入配置文件

来自分类Dev

用循环将用户输入写入文本文件

来自分类Dev

C(基本内容)-将用户输入写入文件

Related 相关文章

  1. 1

    Python-如何将用户输入写入Excel文件?

  2. 2

    makefile如何将用户定义的变量写入yaml文件以支持vagrantfile fetch参数

  3. 3

    如何将用户数据写入根分区?

  4. 4

    也将用户凭据写入文件

  5. 5

    将用户输入写入文件

  6. 6

    Python 2.7,将用户输入写入新的输出文件

  7. 7

    如何将用户放入超级用户文件?

  8. 8

    如何将用户添加到sudoers文件?

  9. 9

    如何将用户文件选择链接到 StringSplit

  10. 10

    使用tee将用户输入写入文件

  11. 11

    如何将用户重定向到返回的URL python

  12. 12

    我如何将用户空间中的缓冲区地址传递给内核

  13. 13

    如何将用户帐户锁定到尽可能少的读取/写入/执行状态?

  14. 14

    如何将用户的输入值提供给程序中运行的进程

  15. 15

    如何将本地IP地址读入.htaccess文件?

  16. 16

    Scrapy:将用户代理更改与IP地址更改同步

  17. 17

    使用Express / Mongoose将用户IP地址保存到DB

  18. 18

    Scrapy:将用户代理更改与IP地址更改同步

  19. 19

    如何使用熊猫将用户定义的列名称写入CSV文件?

  20. 20

    如何使用熊猫将用户定义的列名称写入CSV文件?

  21. 21

    如何将用户关联到IPN

  22. 22

    如何将用户锁定到目录?

  23. 23

    如何将用户定义的函数作为python中的用户输入?

  24. 24

    Shell脚本将用户定义的字符串写入文件

  25. 25

    WIX将用户数据写入配置文件

  26. 26

    将用户输入的字符串写入C中的文件

  27. 27

    WIX将用户数据写入配置文件

  28. 28

    用循环将用户输入写入文本文件

  29. 29

    C(基本内容)-将用户输入写入文件

热门标签

归档