在Python 3和Tkinter中本地设置和获取Cookie
我一直在寻找一种方法,想在Python 3中通过Tkinter图形界面本地设置一个cookie,但找到的都是httplib2的结果,这些都不管用。
基本上,我有一个简单的登录界面,会生成一个SimpleCookie:
def signIn(self):
user = self.login_var.get()
passwd = self.password_var.get()
C = cookies.SimpleCookie()
C['user'] = user
C['passwd'] = passwd
print(C.output(attrs=[], header='Cookie:'))
self.confirm()
...但是我无法把这个cookie取出来或者传递给下一个命令:
def confirm(self):
self.top = Toplevel()
self.top.title('Congrats!')
self.top_frame = Frame(self.top)
self.top_frame.grid()
self.lbl = Label(self.top_frame, text='Hello ' + C['user'].value + '!')
self.lbl.grid()
我肯定我漏掉了什么(可能很多东西?),因为我对Python还是超级新手>.<
1 个回答
1
你可以像这样添加一个参数来进行确认
def confirm(self, C)
然后用下面的方式调用它
self.confirm(C)
在Python中,变量可以是任何对象,字面量(比如整数、字节等),甚至可以是函数。
另外,你可以把变量C改成一个类的全局变量,方法是使用
self.C = cookies.SimpleCookie() # Prefixing self. attributes with an underscore _ is commonly
# used to declare a soft private variable.
# Other classes and modules can still access it but it generally
# means it is not supposed to be accessed from outside the class.
现在你可以通过以下方式在类内部访问C:
self.C
而在类外部则可以通过以下方式访问:
ClassName.C