这就是问题所在。我已经开始使用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] 删除。
我来说两句