[英]Python\Tkinter: Use the variable stored in a function of a class, outside the class
I'm trying to make a GUI version of my program. 我正在尝试制作程序的GUI版本。 This is the first time I use a GUI manager, specifically Tkinter.
这是我第一次使用GUI管理器,特别是Tkinter。 Basically the user insert a text (url) in an Entry widget, click a button and then the program does things.
基本上,用户在Entry小部件中插入文本(url),单击一个按钮,然后程序执行操作。 Consider the following code:
考虑以下代码:
import Tkinter as tk
import urllib2
class Application(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.grid()
self.createWidgets()
def createWidgets(self):
self.EntryText = tk.Entry(self, bg='red')
self.GetButton = tk.Button(self, text='Print',
command=self.GetURL)
self.GetButton.grid(row=0, column=1)
self.EntryText.grid(row=0, column=0)
def GetURL(self):
url_target = ("http://www." + self.EntryText.get())
req = urllib2.urlopen(url_target)
print req.getcode()
app = Application()
app.master.title('App')
app.mainloop()
when I enter a valid url and click the button, I can get the text inserted and create the real url to pass to urllib2. 当我输入有效的URL并单击按钮时,可以插入文本并创建真实的URL传递给urllib2。 However, how can I use the variable "req" anywhere in my program outside the function and the class?
但是,如何在程序中函数和类之外的任何地方使用变量“ req”?
Store the variable in the Application
object: 将变量存储在
Application
对象中:
def GetURL(self):
url_target = ("http://www." + self.EntryText.get())
self.req = urllib2.urlopen(url_target)
So you can use it in other methods of the class, for example 因此,您可以在类的其他方法中使用它,例如
def do_something_with_req(self):
print self.req.getcode()
How the method do_something_with_req
is invoked is up to you (perhaps via another event listener callback). 如何调用
do_something_with_req
方法取决于您(也许通过另一个事件侦听器回调)。
Use a global variable (or if you want persistent storage the pickle or shelve modules): 使用全局变量(或者,如果您要持久存储pickle或shelve模块):
""" main.py """
import Tkinter as tk
import urllib2
from testreq import fromTestreq, fromTestreq2
reqtext = "" # Declaration of global variable
class Application(tk.Frame):
def __init__(self, master=None):
tk.Frame.__init__(self, master)
self.grid()
self.createWidgets()
def createWidgets(self):
self.EntryText = tk.Entry(self, bg='red')
self.EntryText.insert(0,"google.com")
self.GetButton = tk.Button(self, text='Print', command=self.GetURLApp)
self.GetButton.grid(row=0, column=1)
self.EntryText.grid(row=0, column=0)
def GetURLApp(self):
global reqtext # Declaration of local variable as the global one
url_target = "http://www." + self.EntryText.get()
req = urllib2.urlopen(url_target)
print req.geturl()
print req.getcode()
reqUrlStr = str(req.geturl())
reqCodeStr = str(req.getcode())
reqtext = reqUrlStr
#reqtext = reqCodeStr
fromTestreq(reqtext)
#print reqtext
if __name__ == '__main__':
app = Application()
app.master.title('App')
app.mainloop()
fromTestreq2(reqtext)
""" testreq.py """
def fromTestreq(text):
print("From testreq: " + text)
def fromTestreq2(text):
print("From testreq2: " + text)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.