我正在尝试编写代码,当按下按钮(uzdaryti porta)时,文本框中的写入端口将关闭。我得到的错误是
myg2 = Button(virsus,text =“Uzdaryti porta”,command = lambda:uzdaras()) NameError:在封闭范围
中赋值之前引用的自由变量'uzdaras'
是因为我对两个按钮使用相同的文本变量吗?
def portas():
root = Tk()
root.title("Atidaryti/uzdaryti portus")
root.geometry("300x100")
tekstas = Text(root, height=1, width=15)
tekstas.pack(side=TOP)
virsus = Frame(root)
virsus.pack()
apacia = Frame(root)
apacia.pack(side=BOTTOM)
myg1 = Button(virsus, text="Atidaryti porta", command=lambda: atidaryti())
myg2 = Button(virsus, text="Uzdaryti porta", command=lambda: uzdaras())
myg1.pack(side=LEFT)
myg2.pack(side=LEFT)
def atidaryti():
imti = tekstas.get("1.0", "end-1c")
subprocess.call("echo linas123 | sudo -S ufw allow '{imti}'".format(imti=imti), shell=True)
root.mainloop()
def uzdaras():
imti = tekstas.get("1.0", "end-1c")
subprocess.call("echo linas123 | sudo -S ufw deny '{imti}'".format(imti=imti), shell=True)
root.mainloop()