0

我想在不运行的情况下运行模块:

主要.py:

from ex import x
global x

call ex.py

y = x+2
print y

ex.py:

x=5*3

那是简化的代码。如您所见,我调用 ex.py 并使用变量 x 。但是当我运行 main.py 时, ex.py 也在运行。但我只想得到什么是 x 并在 main.py 中使用

编辑:

主要.py:

import pygtk
pygtk.require20()
import gtk
import subprocess 
from ex import x
global x

类应用程序(对象):

def __init__(self):
    self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    self.window.set_title("Graphics")
    self.window.set_position(gtk.WIN_POS_CENTER)
    self.window.set_border_width(0)    
    self.window.connect("delete_event", gtk.main_quit)


    self.giris=gtk.Label("Fill Up The Required Values Below:")
    self.giris.set_alignment(0.5,0)        

    self.giris1=gtk.Label("Read Values:")
    self.giris1.set_alignment(0.5,0) 

    self.box=gtk.HButtonBox()


    self.entry_a1=gtk.Entry()
    self.entry_a2=gtk.Entry()
    self.entry_a3=gtk.Entry()
    self.entry_a4=gtk.Entry()
    self.entry_a5=gtk.Entry()
    self.entry_a6=gtk.Entry()
    self.entry_a7=gtk.Entry()
    self.entry_a8=gtk.Entry()

    self.entry_b1=gtk.Entry()
    self.entry_b2=gtk.Entry()
    self.entry_b3=gtk.Entry()
    self.entry_b4=gtk.Entry()
    self.entry_b5=gtk.Entry()
    self.entry_b6=gtk.Entry()
    self.entry_b7=gtk.Entry()
    self.entry_b8=gtk.Entry() 

    self.label_a1=gtk.Label("X-Label:")
    self.label_a2=gtk.Label("Y-Label:")
    self.label_a3=gtk.Label("Scale:")
    self.label_a4=gtk.Label("XrangePos:")
    self.label_a5=gtk.Label("XrangeNeg:")
    self.label_a6=gtk.Label("YrangePos:")
    self.label_a7=gtk.Label("YrangeNeg:")
    self.label_a8=gtk.Label("Data:")

    self.label_b1=gtk.Label("X-Label:")
    self.label_b2=gtk.Label("Y-Label:")
    self.label_b3=gtk.Label("Scale:")
    self.label_b4=gtk.Label("XrangePos:")
    self.label_b5=gtk.Label("XrangeNeg:")
    self.label_b6=gtk.Label("YrangePos:")
    self.label_b7=gtk.Label("YrangeNeg:")
    self.label_b8=gtk.Label("Data:")

    self.button=gtk.Button("Write")
    self.button.connect("clicked",self.yaz)
    self.button.set_alignment(0.5,0)
    self.button.set_size_request(10,2)


    self.button1=gtk.Button("Read")
    self.button1.connect("clicked",self.oku)
    self.button1.set_alignment(0.5,0)  

    self.button2=gtk.Button("Open")
    self.button2.connect("clicked",self.ac)
    self.button2.set_alignment(0.5,0)

    self.table =gtk.Table(rows=10,columns=4)
    self.table.set_row_spacings(2)
    self.table.set_col_spacings(5)


    self.table.attach(self.giris,0,2,0,1)
    self.table.attach(self.label_a1,0,1,1,2)
    self.table.attach(self.label_a2,0,1,2,3)
    self.table.attach(self.label_a3,0,1,3,4)
    self.table.attach(self.label_a4,0,1,4,5)
    self.table.attach(self.label_a5,0,1,5,6)
    self.table.attach(self.label_a6,0,1,6,7)
    self.table.attach(self.label_a7,0,1,7,8)
    self.table.attach(self.label_a8,0,1,8,9)
    self.table.attach(self.button, 0,2,9,10)
    self.table.attach(self.entry_a1,1,2,1,2)
    self.table.attach(self.entry_a2,1,2,2,3)
    self.table.attach(self.entry_a3,1,2,3,4)
    self.table.attach(self.entry_a4,1,2,4,5)
    self.table.attach(self.entry_a5,1,2,5,6)
    self.table.attach(self.entry_a6,1,2,6,7)
    self.table.attach(self.entry_a7,1,2,7,8)
    self.table.attach(self.entry_a8,1,2,8,9)

    self.table.attach(self.giris1,2,4,0,1)
    self.table.attach(self.label_b1,2,3,1,2)
    self.table.attach(self.label_b2,2,3,2,3)
    self.table.attach(self.label_b3,2,3,3,4)
    self.table.attach(self.label_b4,2,3,4,5)
    self.table.attach(self.label_b5,2,3,5,6)
    self.table.attach(self.label_b6,2,3,6,7)
    self.table.attach(self.label_b7,2,3,7,8)
    self.table.attach(self.label_b8,2,3,8,9)
    self.table.attach(self.button2,2,3,9,10)

    self.table.attach(self.entry_b1,3,4,1,2)
    self.table.attach(self.entry_b2,3,4,2,3)
    self.table.attach(self.entry_b3,3,4,3,4)
    self.table.attach(self.entry_b4,3,4,4,5)
    self.table.attach(self.entry_b5,3,4,5,6)
    self.table.attach(self.entry_b6,3,4,6,7)
    self.table.attach(self.entry_b7,3,4,7,8)
    self.table.attach(self.entry_b8,3,4,8,9)
    self.table.attach(self.button1,3,4,9,10)




    self.window.add(self.table)
    self.window.show_all()


def yaz(self,penar):

    yaz_grup =(self.label_a1.get_label(),self.label_a2.get_label(),
               self.label_a3.get_label(),self.label_a4.get_label(),
               self.label_a5.get_label(),self.label_a6.get_label(),
               self.label_a7.get_label(),self.label_a8.get_label())

    yaz_grup1=(self.entry_a1.get_text(),self.entry_a2.get_text(),
                self.entry_a3.get_text(),self.entry_a4.get_text(),
                self.entry_a5.get_text(),self.entry_a6.get_text(),
                self.entry_a7.get_text(),self.entry_a8.get_text())
    f = open("x","w")  

    for k in range(8):
        self.veri = yaz_grup[k]
        self.veri1=yaz_grup1[k]
        f.write(" %s %s \n" %(self.veri,self.veri1))           
    f.close 

def oku(self,penar): 
    x = subprocess.Popen(["python","/home/emeks/workspace/ex/ex.py"])
    print x

ex.py:

#!/usr/bin/env python

import pygtk
pygtk.require('2.0')
import gtk


global x

dialog = gtk.FileChooserDialog("Open..",None,gtk.FILE_CHOOSER_ACTION_OPEN,
                                 (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                  gtk.STOCK_OPEN, gtk.RESPONSE_OK))
dialog.set_default_response(gtk.RESPONSE_OK)



def foo():       

    response = dialog.run()    
    if response == gtk.RESPONSE_OK:
        dialog.get_filename(), 'selected'
    elif response == gtk.RESPONSE_CANCEL:
        print 'Closed, no files selected'
        x =dialog.get_filename()
        print x

if __name__=='__main__':
    foo()

对话.destroy()

这就是整个代码,正如我所说,我想从 ex 中获得“X”:

4

1 回答 1

0

正确的语法是

if __name__=='__main__':
    ex()

编辑:为了更完整的回答,这里有一个例子。

ex.py:

# some lines of code
print ("Hello World")
x=3*5

# A function
def mysum(a,b):
    return a+b

# Protect execution
if __name__=='__main__':
    print ("I am main")
    print ("2 + 2 is %d" % mysum(2,2))

“Hello World” 将始终被打印,但“I am main” 仅在 ex.py 直接运行时才会打印,而不是在导入时打印。

x可从导入中获得。

编辑 2:使用问题中的完整 ex.py 代码:

#!/usr/bin/env python

import pygtk
pygtk.require('2.0')
import gtk

global x

def foo():       

    response = dialog.run()    
    if response == gtk.RESPONSE_OK:
        dialog.get_filename(), 'selected'
    elif response == gtk.RESPONSE_CANCEL:
        print 'Closed, no files selected'
        x =dialog.get_filename()
        print x

if __name__=='__main__':

    # dialog related calls shall be protected against execution
    dialog = gtk.FileChooserDialog("Open..",None,gtk.FILE_CHOOSER_ACTION_OPEN,
                                 (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
                                  gtk.STOCK_OPEN, gtk.RESPONSE_OK))
    dialog.set_default_response(gtk.RESPONSE_OK)

    foo()
    dialog.destroy()
于 2013-08-20T09:08:46.267 回答